| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING |
| 4 |
|
| 5 |
from django.db import models |
| 6 |
from django.test import TestCase |
| 7 |
|
| 8 |
from model_utils.managers import InheritanceManager |
| 9 |
from tests.models import ( |
| 10 |
InheritanceManagerTestChild1, |
| 11 |
InheritanceManagerTestChild2, |
| 12 |
InheritanceManagerTestChild3, |
| 13 |
InheritanceManagerTestChild3_1, |
| 14 |
InheritanceManagerTestChild4, |
| 15 |
InheritanceManagerTestGrandChild1, |
| 16 |
InheritanceManagerTestGrandChild1_2, |
| 17 |
InheritanceManagerTestParent, |
| 18 |
InheritanceManagerTestRelated, |
| 19 |
TimeFrame, |
| 20 |
) |
| 21 |
|
| 22 |
if TYPE_CHECKING: |
| 23 |
from django.db.models.fields.related_descriptors import RelatedManager |
| 24 |
|
| 25 |
|
| 26 |
class InheritanceManagerTests(TestCase): |
| 27 |
def setUp(self) -> None: |
| 28 |
self.child1 = InheritanceManagerTestChild1.objects.create() |
| 29 |
self.child2 = InheritanceManagerTestChild2.objects.create() |
| 30 |
self.grandchild1 = InheritanceManagerTestGrandChild1.objects.create() |
| 31 |
self.grandchild1_2 = \ |
| 32 |
InheritanceManagerTestGrandChild1_2.objects.create() |
| 33 |
|
| 34 |
def get_manager(self) -> InheritanceManager[InheritanceManagerTestParent]: |
| 35 |
return InheritanceManagerTestParent.objects |
| 36 |
|
| 37 |
def test_normal(self) -> None: |
| 38 |
children = { |
| 39 |
InheritanceManagerTestParent(pk=self.child1.pk), |
| 40 |
InheritanceManagerTestParent(pk=self.child2.pk), |
| 41 |
InheritanceManagerTestParent(pk=self.grandchild1.pk), |
| 42 |
InheritanceManagerTestParent(pk=self.grandchild1_2.pk), |
| 43 |
} |
| 44 |
self.assertEqual(set(self.get_manager().all()), children) |
| 45 |
|
| 46 |
def test_select_all_subclasses(self) -> None: |
| 47 |
children = {self.child1, self.child2} |
| 48 |
children.add(self.grandchild1) |
| 49 |
children.add(self.grandchild1_2) |
| 50 |
self.assertEqual( |
| 51 |
set(self.get_manager().select_subclasses()), children) |
| 52 |
|
| 53 |
def test_select_subclasses_invalid_relation(self) -> None: |
| 54 |
""" |
| 55 |
If an invalid relation string is provided, we can provide the user |
| 56 |
with a list which is valid, rather than just have the select_related() |
| 57 |
raise an AttributeError further in. |
| 58 |
""" |
| 59 |
regex = '^.+? is not in the discovered subclasses, tried:.+$' |
| 60 |
with self.assertRaisesRegex(ValueError, regex): |
| 61 |
self.get_manager().select_subclasses('user') |
| 62 |
|
| 63 |
def test_select_specific_subclasses(self) -> None: |
| 64 |
children = { |
| 65 |
self.child1, |
| 66 |
InheritanceManagerTestParent(pk=self.child2.pk), |
| 67 |
InheritanceManagerTestChild1(pk=self.grandchild1.pk), |
| 68 |
InheritanceManagerTestChild1(pk=self.grandchild1_2.pk), |
| 69 |
} |
| 70 |
self.assertEqual( |
| 71 |
set( |
| 72 |
self.get_manager().select_subclasses( |
| 73 |
"inheritancemanagertestchild1") |
| 74 |
), |
| 75 |
children, |
| 76 |
) |
| 77 |
|
| 78 |
def test_select_specific_grandchildren(self) -> None: |
| 79 |
children = { |
| 80 |
InheritanceManagerTestParent(pk=self.child1.pk), |
| 81 |
InheritanceManagerTestParent(pk=self.child2.pk), |
| 82 |
self.grandchild1, |
| 83 |
InheritanceManagerTestParent(pk=self.grandchild1_2.pk), |
| 84 |
} |
| 85 |
self.assertEqual( |
| 86 |
set( |
| 87 |
self.get_manager().select_subclasses( |
| 88 |
"inheritancemanagertestchild1__inheritancemanagertestgrandchild1" |
| 89 |
) |
| 90 |
), |
| 91 |
children, |
| 92 |
) |
| 93 |
|
| 94 |
def test_children_and_grandchildren(self) -> None: |
| 95 |
children = { |
| 96 |
self.child1, |
| 97 |
InheritanceManagerTestParent(pk=self.child2.pk), |
| 98 |
self.grandchild1, |
| 99 |
InheritanceManagerTestChild1(pk=self.grandchild1_2.pk), |
| 100 |
} |
| 101 |
self.assertEqual( |
| 102 |
set( |
| 103 |
self.get_manager().select_subclasses( |
| 104 |
"inheritancemanagertestchild1", |
| 105 |
"inheritancemanagertestchild1__inheritancemanagertestgrandchild1" |
| 106 |
) |
| 107 |
), |
| 108 |
children, |
| 109 |
) |
| 110 |
|
| 111 |
def test_get_subclass(self) -> None: |
| 112 |
self.assertEqual( |
| 113 |
self.get_manager().get_subclass(pk=self.child1.pk), |
| 114 |
self.child1) |
| 115 |
|
| 116 |
def test_get_subclass_on_queryset(self) -> None: |
| 117 |
self.assertEqual( |
| 118 |
self.get_manager().all().get_subclass(pk=self.child1.pk), |
| 119 |
self.child1) |
| 120 |
|
| 121 |
def test_prior_select_related(self) -> None: |
| 122 |
with self.assertNumQueries(1): |
| 123 |
obj = self.get_manager().select_related( |
| 124 |
"inheritancemanagertestchild1").select_subclasses( |
| 125 |
"inheritancemanagertestchild2").get(pk=self.child1.pk) |
| 126 |
obj.inheritancemanagertestchild1 |
| 127 |
|
| 128 |
def test_manually_specifying_parent_fk_including_grandchildren(self) -> None: |
| 129 |
""" |
| 130 |
given a Model which inherits from another Model, but also declares |
| 131 |
the OneToOne link manually using `related_name` and `parent_link`, |
| 132 |
ensure that the relation names and subclasses are obtained correctly. |
| 133 |
""" |
| 134 |
child3 = InheritanceManagerTestChild3.objects.create() |
| 135 |
qs = InheritanceManagerTestParent.objects.all() |
| 136 |
results = qs.select_subclasses().order_by('pk') |
| 137 |
|
| 138 |
expected_objs = [ |
| 139 |
self.child1, |
| 140 |
self.child2, |
| 141 |
self.grandchild1, |
| 142 |
self.grandchild1_2, |
| 143 |
child3 |
| 144 |
] |
| 145 |
self.assertEqual(list(results), expected_objs) |
| 146 |
|
| 147 |
expected_related_names = [ |
| 148 |
'inheritancemanagertestchild1__inheritancemanagertestgrandchild1', |
| 149 |
'inheritancemanagertestchild1__inheritancemanagertestgrandchild1_2', |
| 150 |
'inheritancemanagertestchild1', |
| 151 |
'inheritancemanagertestchild2', |
| 152 |
'manual_onetoone', # this was set via parent_link & related_name |
| 153 |
'inheritancemanagertestchild3_1', |
| 154 |
'child4_onetoone', |
| 155 |
] |
| 156 |
self.assertEqual(set(results.subclasses), |
| 157 |
set(expected_related_names)) |
| 158 |
|
| 159 |
def test_manually_specifying_parent_fk_single_subclass(self) -> None: |
| 160 |
""" |
| 161 |
Using a string related_name when the relation is manually defined |
| 162 |
instead of implicit should still work in the same way. |
| 163 |
""" |
| 164 |
related_name = 'manual_onetoone' |
| 165 |
child3 = InheritanceManagerTestChild3.objects.create() |
| 166 |
qs = InheritanceManagerTestParent.objects.all() |
| 167 |
results = qs.select_subclasses(related_name).order_by('pk') |
| 168 |
|
| 169 |
expected_objs = [InheritanceManagerTestParent(pk=self.child1.pk), |
| 170 |
InheritanceManagerTestParent(pk=self.child2.pk), |
| 171 |
InheritanceManagerTestParent(pk=self.grandchild1.pk), |
| 172 |
InheritanceManagerTestParent(pk=self.grandchild1_2.pk), |
| 173 |
child3] |
| 174 |
self.assertEqual(list(results), expected_objs) |
| 175 |
expected_related_names = [related_name] |
| 176 |
self.assertEqual(set(results.subclasses), |
| 177 |
set(expected_related_names)) |
| 178 |
|
| 179 |
def test_filter_on_values_queryset(self) -> None: |
| 180 |
queryset = InheritanceManagerTestChild1.objects.values('id').filter(pk=self.child1.pk) |
| 181 |
self.assertEqual(list(queryset), [{'id': self.child1.pk}]) |
| 182 |
|
| 183 |
def test_values_list_on_select_subclasses(self) -> None: |
| 184 |
""" |
| 185 |
Using `select_subclasses` in conjunction with `values_list()` raised an |
| 186 |
exception in `_get_sub_obj_recurse()` because the result of `values_list()` |
| 187 |
is either a `tuple` or primitive objects if `flat=True` is specified, |
| 188 |
because no type checking was done prior to fetching child nodes. |
| 189 |
""" |
| 190 |
|
| 191 |
# Querysets are cast to lists to force immediate evaluation. |
| 192 |
# No exceptions must be thrown. |
| 193 |
|
| 194 |
# No argument to select_subclasses |
| 195 |
objs_1 = list( |
| 196 |
self.get_manager() |
| 197 |
.select_subclasses() |
| 198 |
.values_list('id') |
| 199 |
) |
| 200 |
|
| 201 |
# String argument to select_subclasses |
| 202 |
objs_2 = list( |
| 203 |
self.get_manager() |
| 204 |
.select_subclasses( |
| 205 |
"inheritancemanagertestchild2" |
| 206 |
) |
| 207 |
.values_list('id') |
| 208 |
) |
| 209 |
|
| 210 |
# String argument to select_subclasses |
| 211 |
objs_3 = list( |
| 212 |
self.get_manager() |
| 213 |
.select_subclasses( |
| 214 |
InheritanceManagerTestChild2 |
| 215 |
).values_list('id') |
| 216 |
) |
| 217 |
|
| 218 |
assert all(( |
| 219 |
isinstance(objs_1, list), |
| 220 |
isinstance(objs_2, list), |
| 221 |
isinstance(objs_3, list), |
| 222 |
)) |
| 223 |
|
| 224 |
assert objs_1 == objs_2 == objs_3 |
| 225 |
|
| 226 |
|
| 227 |
class InheritanceManagerUsingModelsTests(TestCase): |
| 228 |
def setUp(self) -> None: |
| 229 |
self.parent1 = InheritanceManagerTestParent.objects.create() |
| 230 |
self.child1 = InheritanceManagerTestChild1.objects.create() |
| 231 |
self.child2 = InheritanceManagerTestChild2.objects.create() |
| 232 |
self.grandchild1 = InheritanceManagerTestGrandChild1.objects.create() |
| 233 |
self.grandchild1_2 = InheritanceManagerTestGrandChild1_2.objects.create() |
| 234 |
|
| 235 |
def test_select_subclass_by_child_model(self) -> None: |
| 236 |
""" |
| 237 |
Confirm that passing a child model works the same as passing the |
| 238 |
select_related manually |
| 239 |
""" |
| 240 |
objs = InheritanceManagerTestParent.objects.select_subclasses( |
| 241 |
"inheritancemanagertestchild1").order_by('pk') |
| 242 |
objsmodels = InheritanceManagerTestParent.objects.select_subclasses( |
| 243 |
InheritanceManagerTestChild1).order_by('pk') |
| 244 |
self.assertEqual(objs.subclasses, objsmodels.subclasses) |
| 245 |
self.assertEqual(list(objs), list(objsmodels)) |
| 246 |
|
| 247 |
def test_select_subclass_by_grandchild_model(self) -> None: |
| 248 |
""" |
| 249 |
Confirm that passing a grandchild model works the same as passing the |
| 250 |
select_related manually |
| 251 |
""" |
| 252 |
objs = InheritanceManagerTestParent.objects.select_subclasses( |
| 253 |
"inheritancemanagertestchild1__inheritancemanagertestgrandchild1") \ |
| 254 |
.order_by('pk') |
| 255 |
objsmodels = InheritanceManagerTestParent.objects.select_subclasses( |
| 256 |
InheritanceManagerTestGrandChild1).order_by('pk') |
| 257 |
self.assertEqual(objs.subclasses, objsmodels.subclasses) |
| 258 |
self.assertEqual(list(objs), list(objsmodels)) |
| 259 |
|
| 260 |
def test_selecting_all_subclasses_specifically_grandchildren(self) -> None: |
| 261 |
""" |
| 262 |
A bare select_subclasses() should achieve the same results as doing |
| 263 |
select_subclasses and specifying all possible subclasses. |
| 264 |
This test checks grandchildren, so only works on 1.6>= |
| 265 |
""" |
| 266 |
objs = InheritanceManagerTestParent.objects.select_subclasses().order_by('pk') |
| 267 |
objsmodels = InheritanceManagerTestParent.objects.select_subclasses( |
| 268 |
InheritanceManagerTestChild1, InheritanceManagerTestChild2, |
| 269 |
InheritanceManagerTestChild3, |
| 270 |
InheritanceManagerTestChild3_1, |
| 271 |
InheritanceManagerTestChild4, |
| 272 |
InheritanceManagerTestGrandChild1, |
| 273 |
InheritanceManagerTestGrandChild1_2).order_by('pk') |
| 274 |
self.assertEqual(set(objs.subclasses), set(objsmodels.subclasses)) |
| 275 |
self.assertEqual(list(objs), list(objsmodels)) |
| 276 |
|
| 277 |
def test_selecting_all_subclasses_specifically_children(self) -> None: |
| 278 |
""" |
| 279 |
A bare select_subclasses() should achieve the same results as doing |
| 280 |
select_subclasses and specifying all possible subclasses. |
| 281 |
|
| 282 |
Note: This is sort of the same test as |
| 283 |
`test_selecting_all_subclasses_specifically_grandchildren` but it |
| 284 |
specifically switches what models are used because that happens |
| 285 |
behind the scenes in a bare select_subclasses(), so we need to |
| 286 |
emulate it. |
| 287 |
""" |
| 288 |
objs = InheritanceManagerTestParent.objects.select_subclasses().order_by('pk') |
| 289 |
|
| 290 |
models = (InheritanceManagerTestChild1, |
| 291 |
InheritanceManagerTestChild2, |
| 292 |
InheritanceManagerTestChild3, |
| 293 |
InheritanceManagerTestChild3_1, |
| 294 |
InheritanceManagerTestChild4, |
| 295 |
InheritanceManagerTestGrandChild1, |
| 296 |
InheritanceManagerTestGrandChild1_2) |
| 297 |
|
| 298 |
objsmodels = InheritanceManagerTestParent.objects.select_subclasses( |
| 299 |
*models).order_by('pk') |
| 300 |
# order shouldn't matter, I don't think, as long as the resulting |
| 301 |
# queryset (when cast to a list) is the same. |
| 302 |
self.assertEqual(set(objs.subclasses), set(objsmodels.subclasses)) |
| 303 |
self.assertEqual(list(objs), list(objsmodels)) |
| 304 |
|
| 305 |
def test_select_subclass_just_self(self) -> None: |
| 306 |
""" |
| 307 |
Passing in the same model as the manager/queryset is bound against |
| 308 |
(ie: the root parent) should have no effect on the result set. |
| 309 |
""" |
| 310 |
objsmodels = InheritanceManagerTestParent.objects.select_subclasses( |
| 311 |
InheritanceManagerTestParent).order_by('pk') |
| 312 |
self.assertEqual([], objsmodels.subclasses) |
| 313 |
self.assertEqual(list(objsmodels), [ |
| 314 |
InheritanceManagerTestParent(pk=self.parent1.pk), |
| 315 |
InheritanceManagerTestParent(pk=self.child1.pk), |
| 316 |
InheritanceManagerTestParent(pk=self.child2.pk), |
| 317 |
InheritanceManagerTestParent(pk=self.grandchild1.pk), |
| 318 |
InheritanceManagerTestParent(pk=self.grandchild1_2.pk), |
| 319 |
]) |
| 320 |
|
| 321 |
def test_select_subclass_invalid_related_model(self) -> None: |
| 322 |
""" |
| 323 |
Confirming that giving a stupid model doesn't work. |
| 324 |
""" |
| 325 |
regex = '^.+? is not a subclass of .+$' |
| 326 |
with self.assertRaisesRegex(ValueError, regex): |
| 327 |
InheritanceManagerTestParent.objects.select_subclasses( |
| 328 |
TimeFrame).order_by('pk') |
| 329 |
|
| 330 |
def test_mixing_strings_and_classes_with_grandchildren(self) -> None: |
| 331 |
""" |
| 332 |
Given arguments consisting of both strings and model classes, |
| 333 |
ensure the right resolutions take place, accounting for the extra |
| 334 |
depth (grandchildren etc) 1.6> allows. |
| 335 |
""" |
| 336 |
objs = InheritanceManagerTestParent.objects.select_subclasses( |
| 337 |
"inheritancemanagertestchild2", |
| 338 |
InheritanceManagerTestGrandChild1_2).order_by('pk') |
| 339 |
expecting = ['inheritancemanagertestchild1__inheritancemanagertestgrandchild1_2', |
| 340 |
'inheritancemanagertestchild2'] |
| 341 |
self.assertEqual(set(objs.subclasses), set(expecting)) |
| 342 |
expecting2 = [ |
| 343 |
InheritanceManagerTestParent(pk=self.parent1.pk), |
| 344 |
InheritanceManagerTestParent(pk=self.child1.pk), |
| 345 |
InheritanceManagerTestChild2(pk=self.child2.pk), |
| 346 |
InheritanceManagerTestParent(pk=self.grandchild1.pk), |
| 347 |
InheritanceManagerTestGrandChild1_2(pk=self.grandchild1_2.pk), |
| 348 |
] |
| 349 |
self.assertEqual(list(objs), expecting2) |
| 350 |
|
| 351 |
def test_mixing_strings_and_classes_with_children(self) -> None: |
| 352 |
""" |
| 353 |
Given arguments consisting of both strings and model classes, |
| 354 |
ensure the right resolutions take place, walking down as far as |
| 355 |
children. |
| 356 |
""" |
| 357 |
objs = InheritanceManagerTestParent.objects.select_subclasses( |
| 358 |
"inheritancemanagertestchild2", |
| 359 |
InheritanceManagerTestChild1).order_by('pk') |
| 360 |
expecting = ['inheritancemanagertestchild1', |
| 361 |
'inheritancemanagertestchild2'] |
| 362 |
|
| 363 |
self.assertEqual(set(objs.subclasses), set(expecting)) |
| 364 |
expecting2 = [ |
| 365 |
InheritanceManagerTestParent(pk=self.parent1.pk), |
| 366 |
InheritanceManagerTestChild1(pk=self.child1.pk), |
| 367 |
InheritanceManagerTestChild2(pk=self.child2.pk), |
| 368 |
InheritanceManagerTestChild1(pk=self.grandchild1.pk), |
| 369 |
InheritanceManagerTestChild1(pk=self.grandchild1_2.pk), |
| 370 |
] |
| 371 |
self.assertEqual(list(objs), expecting2) |
| 372 |
|
| 373 |
def test_duplications(self) -> None: |
| 374 |
""" |
| 375 |
Check that even if the same thing is provided as a string and a model |
| 376 |
that the right results are retrieved. |
| 377 |
""" |
| 378 |
# mixing strings and models which evaluate to the same thing is fine. |
| 379 |
objs = InheritanceManagerTestParent.objects.select_subclasses( |
| 380 |
"inheritancemanagertestchild2", |
| 381 |
InheritanceManagerTestChild2).order_by('pk') |
| 382 |
self.assertEqual(list(objs), [ |
| 383 |
InheritanceManagerTestParent(pk=self.parent1.pk), |
| 384 |
InheritanceManagerTestParent(pk=self.child1.pk), |
| 385 |
InheritanceManagerTestChild2(pk=self.child2.pk), |
| 386 |
InheritanceManagerTestParent(pk=self.grandchild1.pk), |
| 387 |
InheritanceManagerTestParent(pk=self.grandchild1_2.pk), |
| 388 |
]) |
| 389 |
|
| 390 |
def test_child_doesnt_accidentally_get_parent(self) -> None: |
| 391 |
""" |
| 392 |
Given a Child model which also has an InheritanceManager, |
| 393 |
none of the returned objects should be Parent objects. |
| 394 |
""" |
| 395 |
objs = InheritanceManagerTestChild1.objects.select_subclasses( |
| 396 |
InheritanceManagerTestGrandChild1).order_by('pk') |
| 397 |
self.assertEqual([ |
| 398 |
InheritanceManagerTestChild1(pk=self.child1.pk), |
| 399 |
InheritanceManagerTestGrandChild1(pk=self.grandchild1.pk), |
| 400 |
InheritanceManagerTestChild1(pk=self.grandchild1_2.pk), |
| 401 |
], list(objs)) |
| 402 |
|
| 403 |
def test_manually_specifying_parent_fk_only_specific_child(self) -> None: |
| 404 |
""" |
| 405 |
given a Model which inherits from another Model, but also declares |
| 406 |
the OneToOne link manually using `related_name` and `parent_link`, |
| 407 |
ensure that the relation names and subclasses are obtained correctly. |
| 408 |
""" |
| 409 |
child3 = InheritanceManagerTestChild3.objects.create() |
| 410 |
results = InheritanceManagerTestParent.objects.all().select_subclasses( |
| 411 |
InheritanceManagerTestChild3).order_by('pk') |
| 412 |
|
| 413 |
expected_objs = [ |
| 414 |
InheritanceManagerTestParent(pk=self.parent1.pk), |
| 415 |
InheritanceManagerTestParent(pk=self.child1.pk), |
| 416 |
InheritanceManagerTestParent(pk=self.child2.pk), |
| 417 |
InheritanceManagerTestParent(pk=self.grandchild1.pk), |
| 418 |
InheritanceManagerTestParent(pk=self.grandchild1_2.pk), |
| 419 |
child3 |
| 420 |
] |
| 421 |
self.assertEqual(list(results), expected_objs) |
| 422 |
|
| 423 |
expected_related_names = ['manual_onetoone'] |
| 424 |
self.assertEqual(set(results.subclasses), |
| 425 |
set(expected_related_names)) |
| 426 |
|
| 427 |
def test_extras_descend(self) -> None: |
| 428 |
""" |
| 429 |
Ensure that extra(select=) values are copied onto sub-classes. |
| 430 |
""" |
| 431 |
results = InheritanceManagerTestParent.objects.select_subclasses().extra( |
| 432 |
select={'foo': 'id + 1'} |
| 433 |
) |
| 434 |
self.assertTrue(all(result.foo == (result.id + 1) for result in results)) |
| 435 |
|
| 436 |
def test_limit_to_specific_subclass(self) -> None: |
| 437 |
child3 = InheritanceManagerTestChild3.objects.create() |
| 438 |
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestChild3) |
| 439 |
|
| 440 |
self.assertEqual([child3], list(results)) |
| 441 |
|
| 442 |
def test_limit_to_specific_subclass_with_custom_db_column(self) -> None: |
| 443 |
item = InheritanceManagerTestChild3_1.objects.create() |
| 444 |
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestChild3_1) |
| 445 |
|
| 446 |
self.assertEqual([item], list(results)) |
| 447 |
|
| 448 |
def test_limit_to_specific_grandchild_class(self) -> None: |
| 449 |
grandchild1 = InheritanceManagerTestGrandChild1.objects.get() |
| 450 |
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestGrandChild1) |
| 451 |
|
| 452 |
self.assertEqual([grandchild1], list(results)) |
| 453 |
|
| 454 |
def test_limit_to_child_fetches_grandchildren_as_child_class(self) -> None: |
| 455 |
# Not sure if this is the desired behaviour...? |
| 456 |
children = InheritanceManagerTestChild1.objects.all() |
| 457 |
|
| 458 |
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestChild1) |
| 459 |
|
| 460 |
self.assertEqual(set(children), set(results)) |
| 461 |
|
| 462 |
def test_can_fetch_limited_class_grandchildren(self) -> None: |
| 463 |
# Not sure if this is the desired behaviour...? |
| 464 |
children = InheritanceManagerTestChild1.objects.select_subclasses() |
| 465 |
|
| 466 |
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestChild1).select_subclasses() |
| 467 |
|
| 468 |
self.assertEqual(set(children), set(results)) |
| 469 |
|
| 470 |
def test_selecting_multiple_instance_classes(self) -> None: |
| 471 |
child3 = InheritanceManagerTestChild3.objects.create() |
| 472 |
children1 = InheritanceManagerTestChild1.objects.all() |
| 473 |
|
| 474 |
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestChild3, InheritanceManagerTestChild1) |
| 475 |
|
| 476 |
self.assertEqual(set([child3] + list(children1)), set(results)) |
| 477 |
|
| 478 |
def test_selecting_multiple_instance_classes_including_grandchildren(self) -> None: |
| 479 |
child3 = InheritanceManagerTestChild3.objects.create() |
| 480 |
grandchild1 = InheritanceManagerTestGrandChild1.objects.get() |
| 481 |
|
| 482 |
results = InheritanceManagerTestParent.objects.instance_of(InheritanceManagerTestChild3, InheritanceManagerTestGrandChild1).select_subclasses() |
| 483 |
|
| 484 |
self.assertEqual({child3, grandchild1}, set(results)) |
| 485 |
|
| 486 |
def test_select_subclasses_interaction_with_instance_of(self) -> None: |
| 487 |
child3 = InheritanceManagerTestChild3.objects.create() |
| 488 |
|
| 489 |
results = InheritanceManagerTestParent.objects.select_subclasses(InheritanceManagerTestChild1).instance_of(InheritanceManagerTestChild3) |
| 490 |
|
| 491 |
self.assertEqual({child3}, set(results)) |
| 492 |
|
| 493 |
|
| 494 |
class InheritanceManagerRelatedTests(InheritanceManagerTests): |
| 495 |
def setUp(self) -> None: |
| 496 |
self.related = InheritanceManagerTestRelated.objects.create() |
| 497 |
self.child1 = InheritanceManagerTestChild1.objects.create( |
| 498 |
related=self.related) |
| 499 |
self.child2 = InheritanceManagerTestChild2.objects.create( |
| 500 |
related=self.related) |
| 501 |
self.grandchild1 = InheritanceManagerTestGrandChild1.objects.create(related=self.related) |
| 502 |
self.grandchild1_2 = InheritanceManagerTestGrandChild1_2.objects.create(related=self.related) |
| 503 |
|
| 504 |
def get_manager(self) -> RelatedManager[InheritanceManagerTestParent]: # type: ignore[override] |
| 505 |
return self.related.imtests |
| 506 |
|
| 507 |
def test_get_method_with_select_subclasses(self) -> None: |
| 508 |
self.assertEqual( |
| 509 |
InheritanceManagerTestParent.objects.select_subclasses().get( |
| 510 |
id=self.child1.id), |
| 511 |
self.child1) |
| 512 |
|
| 513 |
def test_get_method_with_select_subclasses_check_for_useless_join(self) -> None: |
| 514 |
child4 = InheritanceManagerTestChild4.objects.create(related=self.related, other_onetoone=self.child1) |
| 515 |
self.assertEqual( |
| 516 |
str(InheritanceManagerTestChild4.objects.select_subclasses().filter( |
| 517 |
id=child4.id).query), |
| 518 |
str(InheritanceManagerTestChild4.objects.select_subclasses().select_related(None).filter( |
| 519 |
id=child4.id).query)) |
| 520 |
|
| 521 |
def test_annotate_with_select_subclasses(self) -> None: |
| 522 |
qs = InheritanceManagerTestParent.objects.select_subclasses().annotate( |
| 523 |
models.Count('id')) |
| 524 |
self.assertEqual(qs.get(id=self.child1.id).id__count, 1) |
| 525 |
|
| 526 |
def test_annotate_with_named_arguments_with_select_subclasses(self) -> None: |
| 527 |
qs = InheritanceManagerTestParent.objects.select_subclasses().annotate( |
| 528 |
test_count=models.Count('id')) |
| 529 |
self.assertEqual(qs.get(id=self.child1.id).test_count, 1) |
| 530 |
|
| 531 |
def test_annotate_before_select_subclasses(self) -> None: |
| 532 |
qs = InheritanceManagerTestParent.objects.annotate( |
| 533 |
models.Count('id')).select_subclasses() |
| 534 |
self.assertEqual(qs.get(id=self.child1.id).id__count, 1) |
| 535 |
|
| 536 |
def test_annotate_with_named_arguments_before_select_subclasses(self) -> None: |
| 537 |
qs = InheritanceManagerTestParent.objects.annotate( |
| 538 |
test_count=models.Count('id')).select_subclasses() |
| 539 |
self.assertEqual(qs.get(id=self.child1.id).test_count, 1) |
| 540 |
|
| 541 |
def test_clone_when_inheritance_queryset_selects_subclasses_should_clone_them_too(self) -> None: |
| 542 |
qs = InheritanceManagerTestParent.objects.select_subclasses() |
| 543 |
self.assertEqual(qs.subclasses, qs._clone().subclasses) |