| 1 |
from django.contrib.contenttypes.models import ContentType |
| 2 |
from django.db.models import ProtectedError, Q, Sum |
| 3 |
from django.forms.models import modelform_factory |
| 4 |
from django.test import TestCase, skipIfDBFeature |
| 5 |
|
| 6 |
from .models import ( |
| 7 |
A, |
| 8 |
Address, |
| 9 |
B, |
| 10 |
Board, |
| 11 |
C, |
| 12 |
Cafe, |
| 13 |
CharLink, |
| 14 |
Company, |
| 15 |
Contact, |
| 16 |
Content, |
| 17 |
D, |
| 18 |
Guild, |
| 19 |
HasLinkThing, |
| 20 |
Link, |
| 21 |
Node, |
| 22 |
Note, |
| 23 |
OddRelation1, |
| 24 |
OddRelation2, |
| 25 |
Organization, |
| 26 |
Person, |
| 27 |
Place, |
| 28 |
Related, |
| 29 |
Restaurant, |
| 30 |
Tag, |
| 31 |
Team, |
| 32 |
TextLink, |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
class GenericRelationTests(TestCase): |
| 37 |
def test_inherited_models_content_type(self): |
| 38 |
""" |
| 39 |
GenericRelations on inherited classes use the correct content type. |
| 40 |
""" |
| 41 |
p = Place.objects.create(name="South Park") |
| 42 |
r = Restaurant.objects.create(name="Chubby's") |
| 43 |
l1 = Link.objects.create(content_object=p) |
| 44 |
l2 = Link.objects.create(content_object=r) |
| 45 |
self.assertEqual(list(p.links.all()), [l1]) |
| 46 |
self.assertEqual(list(r.links.all()), [l2]) |
| 47 |
|
| 48 |
def test_reverse_relation_pk(self): |
| 49 |
""" |
| 50 |
The correct column name is used for the primary key on the |
| 51 |
originating model of a query. See #12664. |
| 52 |
""" |
| 53 |
p = Person.objects.create(account=23, name="Chef") |
| 54 |
Address.objects.create( |
| 55 |
street="123 Anywhere Place", |
| 56 |
city="Conifer", |
| 57 |
state="CO", |
| 58 |
zipcode="80433", |
| 59 |
content_object=p, |
| 60 |
) |
| 61 |
|
| 62 |
qs = Person.objects.filter(addresses__zipcode="80433") |
| 63 |
self.assertEqual(1, qs.count()) |
| 64 |
self.assertEqual("Chef", qs[0].name) |
| 65 |
|
| 66 |
def test_charlink_delete(self): |
| 67 |
oddrel = OddRelation1.objects.create(name="clink") |
| 68 |
CharLink.objects.create(content_object=oddrel) |
| 69 |
oddrel.delete() |
| 70 |
|
| 71 |
def test_textlink_delete(self): |
| 72 |
oddrel = OddRelation2.objects.create(name="tlink") |
| 73 |
TextLink.objects.create(content_object=oddrel) |
| 74 |
oddrel.delete() |
| 75 |
|
| 76 |
def test_charlink_filter(self): |
| 77 |
oddrel = OddRelation1.objects.create(name="clink") |
| 78 |
CharLink.objects.create(content_object=oddrel, value="value") |
| 79 |
self.assertSequenceEqual( |
| 80 |
OddRelation1.objects.filter(clinks__value="value"), [oddrel] |
| 81 |
) |
| 82 |
|
| 83 |
def test_textlink_filter(self): |
| 84 |
oddrel = OddRelation2.objects.create(name="clink") |
| 85 |
TextLink.objects.create(content_object=oddrel, value="value") |
| 86 |
self.assertSequenceEqual( |
| 87 |
OddRelation2.objects.filter(tlinks__value="value"), [oddrel] |
| 88 |
) |
| 89 |
|
| 90 |
def test_coerce_object_id_remote_field_cache_persistence(self): |
| 91 |
restaurant = Restaurant.objects.create() |
| 92 |
CharLink.objects.create(content_object=restaurant) |
| 93 |
charlink = CharLink.objects.latest("pk") |
| 94 |
self.assertIs(charlink.content_object, charlink.content_object) |
| 95 |
# If the model (Cafe) uses more than one level of multi-table inheritance. |
| 96 |
cafe = Cafe.objects.create() |
| 97 |
CharLink.objects.create(content_object=cafe) |
| 98 |
charlink = CharLink.objects.latest("pk") |
| 99 |
self.assertIs(charlink.content_object, charlink.content_object) |
| 100 |
|
| 101 |
def test_q_object_or(self): |
| 102 |
""" |
| 103 |
SQL query parameters for generic relations are properly |
| 104 |
grouped when OR is used (#11535). |
| 105 |
|
| 106 |
In this bug the first query (below) works while the second, with the |
| 107 |
query parameters the same but in reverse order, does not. |
| 108 |
|
| 109 |
The issue is that the generic relation conditions do not get properly |
| 110 |
grouped in parentheses. |
| 111 |
""" |
| 112 |
note_contact = Contact.objects.create() |
| 113 |
org_contact = Contact.objects.create() |
| 114 |
Note.objects.create(note="note", content_object=note_contact) |
| 115 |
org = Organization.objects.create(name="org name") |
| 116 |
org.contacts.add(org_contact) |
| 117 |
# search with a non-matching note and a matching org name |
| 118 |
qs = Contact.objects.filter( |
| 119 |
Q(notes__note__icontains=r"other note") |
| 120 |
| Q(organizations__name__icontains=r"org name") |
| 121 |
) |
| 122 |
self.assertIn(org_contact, qs) |
| 123 |
# search again, with the same query parameters, in reverse order |
| 124 |
qs = Contact.objects.filter( |
| 125 |
Q(organizations__name__icontains=r"org name") |
| 126 |
| Q(notes__note__icontains=r"other note") |
| 127 |
) |
| 128 |
self.assertIn(org_contact, qs) |
| 129 |
|
| 130 |
def test_join_reuse(self): |
| 131 |
qs = Person.objects.filter(addresses__street="foo").filter( |
| 132 |
addresses__street="bar" |
| 133 |
) |
| 134 |
self.assertEqual(str(qs.query).count("JOIN"), 2) |
| 135 |
|
| 136 |
def test_generic_relation_ordering(self): |
| 137 |
""" |
| 138 |
Ordering over a generic relation does not include extraneous |
| 139 |
duplicate results, nor excludes rows not participating in the relation. |
| 140 |
""" |
| 141 |
p1 = Place.objects.create(name="South Park") |
| 142 |
p2 = Place.objects.create(name="The City") |
| 143 |
c = Company.objects.create(name="Chubby's Intl.") |
| 144 |
Link.objects.create(content_object=p1) |
| 145 |
Link.objects.create(content_object=c) |
| 146 |
|
| 147 |
places = list(Place.objects.order_by("links__id")) |
| 148 |
|
| 149 |
def count_places(place): |
| 150 |
return len([p for p in places if p.id == place.id]) |
| 151 |
|
| 152 |
self.assertEqual(len(places), 2) |
| 153 |
self.assertEqual(count_places(p1), 1) |
| 154 |
self.assertEqual(count_places(p2), 1) |
| 155 |
|
| 156 |
def test_target_model_len_zero(self): |
| 157 |
""" |
| 158 |
Saving a model with a GenericForeignKey to a model instance whose |
| 159 |
__len__ method returns 0 (Team.__len__() here) shouldn't fail (#13085). |
| 160 |
""" |
| 161 |
team1 = Team.objects.create(name="Backend devs") |
| 162 |
note = Note(note="Deserve a bonus", content_object=team1) |
| 163 |
note.save() |
| 164 |
|
| 165 |
def test_target_model_bool_false(self): |
| 166 |
""" |
| 167 |
Saving a model with a GenericForeignKey to a model instance whose |
| 168 |
__bool__ method returns False (Guild.__bool__() here) shouldn't fail |
| 169 |
(#13085). |
| 170 |
""" |
| 171 |
g1 = Guild.objects.create(name="First guild") |
| 172 |
note = Note(note="Note for guild", content_object=g1) |
| 173 |
note.save() |
| 174 |
|
| 175 |
@skipIfDBFeature("interprets_empty_strings_as_nulls") |
| 176 |
def test_gfk_to_model_with_empty_pk(self): |
| 177 |
"""Test related to #13085""" |
| 178 |
# Saving model with GenericForeignKey to model instance with an |
| 179 |
# empty CharField PK |
| 180 |
b1 = Board.objects.create(name="") |
| 181 |
tag = Tag(label="VP", content_object=b1) |
| 182 |
tag.save() |
| 183 |
|
| 184 |
def test_ticket_20378(self): |
| 185 |
# Create a couple of extra HasLinkThing so that the autopk value |
| 186 |
# isn't the same for Link and HasLinkThing. |
| 187 |
hs1 = HasLinkThing.objects.create() |
| 188 |
hs2 = HasLinkThing.objects.create() |
| 189 |
hs3 = HasLinkThing.objects.create() |
| 190 |
hs4 = HasLinkThing.objects.create() |
| 191 |
l1 = Link.objects.create(content_object=hs3) |
| 192 |
l2 = Link.objects.create(content_object=hs4) |
| 193 |
self.assertSequenceEqual(HasLinkThing.objects.filter(links=l1), [hs3]) |
| 194 |
self.assertSequenceEqual(HasLinkThing.objects.filter(links=l2), [hs4]) |
| 195 |
self.assertSequenceEqual( |
| 196 |
HasLinkThing.objects.exclude(links=l2), [hs1, hs2, hs3] |
| 197 |
) |
| 198 |
self.assertSequenceEqual( |
| 199 |
HasLinkThing.objects.exclude(links=l1), [hs1, hs2, hs4] |
| 200 |
) |
| 201 |
|
| 202 |
def test_ticket_20564(self): |
| 203 |
b1 = B.objects.create() |
| 204 |
b2 = B.objects.create() |
| 205 |
b3 = B.objects.create() |
| 206 |
c1 = C.objects.create(b=b1) |
| 207 |
c2 = C.objects.create(b=b2) |
| 208 |
c3 = C.objects.create(b=b3) |
| 209 |
A.objects.create(flag=None, content_object=b1) |
| 210 |
A.objects.create(flag=True, content_object=b2) |
| 211 |
self.assertSequenceEqual(C.objects.filter(b__a__flag=None), [c1, c3]) |
| 212 |
self.assertSequenceEqual(C.objects.exclude(b__a__flag=None), [c2]) |
| 213 |
|
| 214 |
def test_ticket_20564_nullable_fk(self): |
| 215 |
b1 = B.objects.create() |
| 216 |
b2 = B.objects.create() |
| 217 |
b3 = B.objects.create() |
| 218 |
d1 = D.objects.create(b=b1) |
| 219 |
d2 = D.objects.create(b=b2) |
| 220 |
d3 = D.objects.create(b=b3) |
| 221 |
d4 = D.objects.create() |
| 222 |
A.objects.create(flag=None, content_object=b1) |
| 223 |
A.objects.create(flag=True, content_object=b1) |
| 224 |
A.objects.create(flag=True, content_object=b2) |
| 225 |
self.assertSequenceEqual(D.objects.exclude(b__a__flag=None), [d2]) |
| 226 |
self.assertSequenceEqual(D.objects.filter(b__a__flag=None), [d1, d3, d4]) |
| 227 |
self.assertSequenceEqual(B.objects.filter(a__flag=None), [b1, b3]) |
| 228 |
self.assertSequenceEqual(B.objects.exclude(a__flag=None), [b2]) |
| 229 |
|
| 230 |
def test_extra_join_condition(self): |
| 231 |
# A crude check that content_type_id is taken in account in the |
| 232 |
# join/subquery condition. |
| 233 |
self.assertIn( |
| 234 |
"content_type_id", str(B.objects.exclude(a__flag=None).query).lower() |
| 235 |
) |
| 236 |
# No need for any joins - the join from inner query can be trimmed in |
| 237 |
# this case (but not in the above case as no a objects at all for given |
| 238 |
# B would then fail). |
| 239 |
self.assertNotIn(" join ", str(B.objects.exclude(a__flag=True).query).lower()) |
| 240 |
self.assertIn( |
| 241 |
"content_type_id", str(B.objects.exclude(a__flag=True).query).lower() |
| 242 |
) |
| 243 |
|
| 244 |
def test_annotate(self): |
| 245 |
hs1 = HasLinkThing.objects.create() |
| 246 |
hs2 = HasLinkThing.objects.create() |
| 247 |
HasLinkThing.objects.create() |
| 248 |
b = Board.objects.create(name=str(hs1.pk)) |
| 249 |
Link.objects.create(content_object=hs2) |
| 250 |
link = Link.objects.create(content_object=hs1) |
| 251 |
Link.objects.create(content_object=b) |
| 252 |
qs = HasLinkThing.objects.annotate(Sum("links")).filter(pk=hs1.pk) |
| 253 |
# If content_type restriction isn't in the query's join condition, |
| 254 |
# then wrong results are produced here as the link to b will also match |
| 255 |
# (b and hs1 have equal pks). |
| 256 |
self.assertEqual(qs.count(), 1) |
| 257 |
self.assertEqual(qs[0].links__sum, link.id) |
| 258 |
link.delete() |
| 259 |
# Now if we don't have proper left join, we will not produce any |
| 260 |
# results at all here. |
| 261 |
# clear cached results |
| 262 |
qs = qs.all() |
| 263 |
self.assertEqual(qs.count(), 1) |
| 264 |
# Note - 0 here would be a nicer result... |
| 265 |
self.assertIs(qs[0].links__sum, None) |
| 266 |
# Finally test that filtering works. |
| 267 |
self.assertEqual(qs.filter(links__sum__isnull=True).count(), 1) |
| 268 |
self.assertEqual(qs.filter(links__sum__isnull=False).count(), 0) |
| 269 |
|
| 270 |
def test_filter_targets_related_pk(self): |
| 271 |
# Use hardcoded PKs to ensure different PKs for "link" and "hs2" |
| 272 |
# objects. |
| 273 |
HasLinkThing.objects.create(pk=1) |
| 274 |
hs2 = HasLinkThing.objects.create(pk=2) |
| 275 |
link = Link.objects.create(content_object=hs2, pk=1) |
| 276 |
self.assertNotEqual(link.object_id, link.pk) |
| 277 |
self.assertSequenceEqual(HasLinkThing.objects.filter(links=link.pk), [hs2]) |
| 278 |
|
| 279 |
def test_editable_generic_rel(self): |
| 280 |
GenericRelationForm = modelform_factory(HasLinkThing, fields="__all__") |
| 281 |
form = GenericRelationForm() |
| 282 |
self.assertIn("links", form.fields) |
| 283 |
form = GenericRelationForm({"links": None}) |
| 284 |
self.assertTrue(form.is_valid()) |
| 285 |
form.save() |
| 286 |
links = HasLinkThing._meta.get_field("links") |
| 287 |
self.assertEqual(links.save_form_data_calls, 1) |
| 288 |
|
| 289 |
def test_ticket_22998(self): |
| 290 |
related = Related.objects.create() |
| 291 |
content = Content.objects.create(related_obj=related) |
| 292 |
Node.objects.create(content=content) |
| 293 |
|
| 294 |
# deleting the Related cascades to the Content cascades to the Node, |
| 295 |
# where the pre_delete signal should fire and prevent deletion. |
| 296 |
with self.assertRaises(ProtectedError): |
| 297 |
related.delete() |
| 298 |
|
| 299 |
def test_ticket_22982(self): |
| 300 |
place = Place.objects.create(name="My Place") |
| 301 |
self.assertIn("GenericRelatedObjectManager", str(place.links)) |
| 302 |
|
| 303 |
def test_filter_on_related_proxy_model(self): |
| 304 |
place = Place.objects.create() |
| 305 |
Link.objects.create(content_object=place) |
| 306 |
self.assertEqual(Place.objects.get(link_proxy__object_id=place.id), place) |
| 307 |
|
| 308 |
def test_generic_reverse_relation_with_mti(self): |
| 309 |
""" |
| 310 |
Filtering with a reverse generic relation, where the GenericRelation |
| 311 |
comes from multi-table inheritance. |
| 312 |
""" |
| 313 |
place = Place.objects.create(name="Test Place") |
| 314 |
link = Link.objects.create(content_object=place) |
| 315 |
result = Link.objects.filter(places=place) |
| 316 |
self.assertCountEqual(result, [link]) |
| 317 |
|
| 318 |
def test_generic_reverse_relation_with_abc(self): |
| 319 |
""" |
| 320 |
The reverse generic relation accessor (targets) is created if the |
| 321 |
GenericRelation comes from an abstract base model (HasLinks). |
| 322 |
""" |
| 323 |
thing = HasLinkThing.objects.create() |
| 324 |
link = Link.objects.create(content_object=thing) |
| 325 |
self.assertCountEqual(link.targets.all(), [thing]) |
| 326 |
|
| 327 |
def test_generic_reverse_relation_exclude_filter(self): |
| 328 |
place1 = Place.objects.create(name="Test Place 1") |
| 329 |
place2 = Place.objects.create(name="Test Place 2") |
| 330 |
Link.objects.create(content_object=place1) |
| 331 |
link2 = Link.objects.create(content_object=place2) |
| 332 |
qs = Link.objects.filter(~Q(places__name="Test Place 1")) |
| 333 |
self.assertSequenceEqual(qs, [link2]) |
| 334 |
qs = Link.objects.exclude(places__name="Test Place 1") |
| 335 |
self.assertSequenceEqual(qs, [link2]) |
| 336 |
|
| 337 |
def test_check_cached_value_pk_different_type(self): |
| 338 |
"""Primary key is not checked if the content type doesn't match.""" |
| 339 |
board = Board.objects.create(name="some test") |
| 340 |
oddrel = OddRelation1.objects.create(name="clink") |
| 341 |
charlink = CharLink.objects.create(content_object=oddrel) |
| 342 |
charlink = CharLink.objects.get(pk=charlink.pk) |
| 343 |
self.assertEqual(charlink.content_object, oddrel) |
| 344 |
charlink.object_id = board.pk |
| 345 |
charlink.content_type_id = ContentType.objects.get_for_model(Board).id |
| 346 |
self.assertEqual(charlink.content_object, board) |