|
10 | 10 | from django.db.utils import IntegrityError
|
11 | 11 | from django.test import TransactionTestCase
|
12 | 12 | from polymorphic import query_translate
|
| 13 | +from polymorphic.query import convert_to_polymorphic_queryset |
13 | 14 | from polymorphic.managers import PolymorphicManager
|
14 | 15 | from polymorphic.models import PolymorphicTypeInvalid, PolymorphicTypeUndefined
|
15 | 16 | from polymorphic.tests.models import (
|
|
101 | 102 | UUIDPlainC,
|
102 | 103 | UUIDProject,
|
103 | 104 | UUIDResearchProject,
|
| 105 | + VanillaPlainModel, |
104 | 106 | )
|
105 | 107 |
|
106 | 108 |
|
@@ -1428,6 +1430,74 @@ def test_select_related_on_poly_classes_simple(self):
|
1428 | 1430 | self.assertIsInstance(obj_list[2].relation, AltChildModel)
|
1429 | 1431 | self.assertIsInstance(obj_list[3].relation, AltChildModel)
|
1430 | 1432 |
|
| 1433 | + def test_we_can_upgrade_a_query_set_to_polymorphic_supports_already_ploy_qs(self): |
| 1434 | + base_qs = RefPlainModel.poly_objects.get_queryset() |
| 1435 | + self.assertIs(convert_to_polymorphic_queryset(base_qs), base_qs) |
| 1436 | + |
| 1437 | + def test_we_can_upgrade_a_query_set_to_polymorphic_supports_non_ploy_qs_on_ploy_object(self): |
| 1438 | + base_qs = RefPlainModel.objects.get_queryset() |
| 1439 | + self.assertIsNot(convert_to_polymorphic_queryset(base_qs), base_qs) |
| 1440 | + self.assertIsInstance(convert_to_polymorphic_queryset(base_qs), PolymorphicRelatedQuerySetMixin) |
| 1441 | + |
| 1442 | + def test_we_can_upgrade_a_query_set_to_polymorphic_supports_non_ploy_managers_on_ploy_object(self): |
| 1443 | + base_qs = RefPlainModel.objects |
| 1444 | + self.assertIsNot(convert_to_polymorphic_queryset(base_qs), base_qs) |
| 1445 | + self.assertIsInstance(convert_to_polymorphic_queryset(base_qs), PolymorphicRelatedQuerySetMixin) |
| 1446 | + |
| 1447 | + def test_we_can_upgrade_a_query_set_to_polymorphic(self): |
| 1448 | + # can we fetch the related object but only the minimal 'common' values |
| 1449 | + plain_a_obj_1 = PlainA.objects.create(field1="f1") |
| 1450 | + plain_a_obj_2 = PlainA.objects.create(field1="f2") |
| 1451 | + extra_obj = ModelExtraExternal.objects.create(topic="t1") |
| 1452 | + obj_p = ParentModel.objects.create(name="p1") |
| 1453 | + obj_c = ChildModel.objects.create(name="c1", other_name="c1name", link_on_child=extra_obj) |
| 1454 | + obj_ac1 = AltChildModel.objects.create( |
| 1455 | + name="ac1", other_name="ac1name", link_on_altchild=plain_a_obj_1 |
| 1456 | + ) |
| 1457 | + obj_ac2 = AltChildModel.objects.create( |
| 1458 | + name="ac2", other_name="ac2name", link_on_altchild=plain_a_obj_2 |
| 1459 | + ) |
| 1460 | + obj_p_1 = VanillaPlainModel.objects.create(relation=obj_p) |
| 1461 | + obj_p_2 = VanillaPlainModel.objects.create(relation=obj_c) |
| 1462 | + obj_p_3 = VanillaPlainModel.objects.create(relation=obj_ac1) |
| 1463 | + obj_p_4 = VanillaPlainModel.objects.create(relation=obj_ac2) |
| 1464 | + |
| 1465 | + with self.assertNumQueries(1): |
| 1466 | + # pos 3 if i cannot do optimized select_related |
| 1467 | + obj_list = list( |
| 1468 | + VanillaPlainModel.objects.order_by("pk") |
| 1469 | + ) |
| 1470 | + |
| 1471 | + with self.assertNumQueries(7): |
| 1472 | + self.assertEqual(obj_list[0].relation.name, "p1") |
| 1473 | + self.assertEqual(obj_list[1].relation.name, "c1") |
| 1474 | + self.assertEqual(obj_list[2].relation.name, "ac1") |
| 1475 | + self.assertEqual(obj_list[3].relation.name, "ac2") |
| 1476 | + |
| 1477 | + with self.assertNumQueries(1): |
| 1478 | + # pos 3 if i cannot do optimized select_related |
| 1479 | + obj_list = list( |
| 1480 | + convert_to_polymorphic_queryset( |
| 1481 | + VanillaPlainModel.objects |
| 1482 | + ).select_related( |
| 1483 | + "relation", |
| 1484 | + "relation__childmodel", |
| 1485 | + "relation__altchildmodel", |
| 1486 | + ) |
| 1487 | + .order_by("pk") |
| 1488 | + ) |
| 1489 | + |
| 1490 | + with self.assertNumQueries(0): |
| 1491 | + self.assertEqual(obj_list[0].relation.name, "p1") |
| 1492 | + self.assertEqual(obj_list[1].relation.name, "c1") |
| 1493 | + self.assertEqual(obj_list[2].relation.name, "ac1") |
| 1494 | + self.assertEqual(obj_list[3].relation.name, "ac2") |
| 1495 | + |
| 1496 | + self.assertIsInstance(obj_list[0].relation, ParentModel) |
| 1497 | + self.assertIsInstance(obj_list[1].relation, ChildModel) |
| 1498 | + self.assertIsInstance(obj_list[2].relation, AltChildModel) |
| 1499 | + self.assertIsInstance(obj_list[3].relation, AltChildModel) |
| 1500 | + |
1431 | 1501 | def test_select_related_on_poly_classes_indirect_related(self):
|
1432 | 1502 | # can we fetch the related object but only the minimal 'common' values
|
1433 | 1503 | plain_a_obj_1 = PlainA.objects.create(field1="f1")
|
@@ -1457,7 +1527,7 @@ def test_select_related_on_poly_classes_indirect_related(self):
|
1457 | 1527 | with self.assertNumQueries(1):
|
1458 | 1528 | # pos 3 if i cannot do optimized select_related
|
1459 | 1529 | obj_list = list(
|
1460 |
| - RefPlainModel.objects.select_related( |
| 1530 | + RefPlainModel.poly_objects.select_related( |
1461 | 1531 | # "plainobj__relation",
|
1462 | 1532 | "plainobj__relation",
|
1463 | 1533 | "plainobj__relation__childmodel__link_on_child",
|
@@ -1504,7 +1574,7 @@ def test_select_related_on_poly_classes_indirect_related(self):
|
1504 | 1574 | with self.assertNumQueries(1):
|
1505 | 1575 | # pos 3 if i cannot do optimized select_related
|
1506 | 1576 | obj_list = list(
|
1507 |
| - RefPlainModel.objects.select_related( |
| 1577 | + RefPlainModel.poly_objects.select_related( |
1508 | 1578 | # "plainobj__relation",
|
1509 | 1579 | "plainobj__relation",
|
1510 | 1580 | "plainobj__relation__childmodel__link_on_child",
|
@@ -1554,7 +1624,7 @@ def test_select_related_fecth_all_poly_classes_indirect_related(self):
|
1554 | 1624 | with self.assertNumQueries(1):
|
1555 | 1625 | # pos 3 if i cannot do optimized select_related
|
1556 | 1626 | obj_list = list(
|
1557 |
| - RefPlainModel.objects.select_related( |
| 1627 | + RefPlainModel.poly_objects.select_related( |
1558 | 1628 | # "plainobj__relation",
|
1559 | 1629 | "plainobj__relation",
|
1560 | 1630 | "plainobj__relation__*",
|
|
0 commit comments