Skip to content

Commit 4aee7ff

Browse files
committed
Assert UnsealedAttributeAccess are emitted from the right location.
1 parent 7cbbe23 commit 4aee7ff

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

tests/test_query.py

+42-21
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ def test_sealed_deferred_field(self):
5858
message = (
5959
'Attempt to fetch deferred field "weight" on sealed <SeaLion instance>'
6060
)
61-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
61+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
6262
instance.weight
63+
self.assertEqual(ctx.filename, __file__)
6364

6465
def test_not_sealed_deferred_field(self):
6566
instance = SeaLion.objects.defer("weight").get()
@@ -70,8 +71,9 @@ def test_sealed_foreign_key(self):
7071
message = (
7172
'Attempt to fetch related field "location" on sealed <SeaLion instance>'
7273
)
73-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
74+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
7475
instance.location
76+
self.assertEqual(ctx.filename, __file__)
7577

7678
def test_not_sealed_foreign_key(self):
7779
instance = SeaLion.objects.get()
@@ -84,10 +86,11 @@ def test_sealed_select_related_foreign_key(self):
8486
message = (
8587
'Attempt to fetch related field "location" on sealed <SeaLion instance>'
8688
)
87-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
89+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
8890
instance.sealion.location
8991
instance = SeaGull.objects.select_related("sealion__location").seal().get()
9092
self.assertEqual(instance.sealion.location, self.location)
93+
self.assertEqual(ctx.filename, __file__)
9194

9295
def test_sealed_select_related_none_foreign_key(self):
9396
SeaLion.objects.update(location=None)
@@ -145,16 +148,18 @@ def test_sealed_select_related_deferred_field(self):
145148
message = (
146149
'Attempt to fetch deferred field "longitude" on sealed <Location instance>'
147150
)
148-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
151+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
149152
instance.sealion.location.longitude
153+
self.assertEqual(ctx.filename, __file__)
150154

151155
def test_sealed_one_to_one(self):
152156
instance = SeaGull.objects.seal().get()
153157
message = (
154158
'Attempt to fetch related field "sealion" on sealed <SeaGull instance>'
155159
)
156-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
160+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
157161
instance.sealion
162+
self.assertEqual(ctx.filename, __file__)
158163

159164
def test_not_sealed_one_to_one(self):
160165
instance = SeaGull.objects.get()
@@ -185,10 +190,12 @@ def test_sealed_prefetch_related_reverse_one_to_one(self):
185190
def test_sealed_many_to_many(self):
186191
instance = SeaLion.objects.seal().get()
187192
message = 'Attempt to fetch many-to-many field "previous_locations" on sealed <SeaLion instance>'
188-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
193+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
189194
list(instance.previous_locations.all())
190-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
195+
self.assertEqual(ctx.filename, __file__)
196+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
191197
instance.previous_locations.all()[0]
198+
self.assertEqual(ctx.filename, __file__)
192199

193200
def test_sealed_many_to_many_queryset(self):
194201
instance = SeaLion.objects.seal().get()
@@ -212,8 +219,9 @@ def test_sealed_string_prefetched_many_to_many(self):
212219
self.assertSequenceEqual(instance.previous_locations.all(), [self.location])
213220
instance = instance.previous_locations.all()[0]
214221
message = 'Attempt to fetch many-to-many field "previous_visitors" on sealed <Location instance>'
215-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
222+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
216223
list(instance.previous_visitors.all())
224+
self.assertEqual(ctx.filename, __file__)
217225

218226
def test_sealed_prefetch_prefetched_many_to_many(self):
219227
instance = (
@@ -227,8 +235,9 @@ def test_sealed_prefetch_prefetched_many_to_many(self):
227235
self.assertSequenceEqual(instance.previous_locations.all(), [self.location])
228236
instance = instance.previous_locations.all()[0]
229237
message = 'Attempt to fetch many-to-many field "previous_visitors" on sealed <Location instance>'
230-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
238+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
231239
list(instance.previous_visitors.all())
240+
self.assertEqual(ctx.filename, __file__)
232241

233242
def test_sealed_prefetch_queryset_prefetched_many_to_many(self):
234243
instance = (
@@ -242,8 +251,9 @@ def test_sealed_prefetch_queryset_prefetched_many_to_many(self):
242251
self.assertSequenceEqual(instance.previous_locations.all(), [self.location])
243252
instance = instance.previous_locations.all()[0]
244253
message = 'Attempt to fetch many-to-many field "previous_visitors" on sealed <Location instance>'
245-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
254+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
246255
list(instance.previous_visitors.all())
256+
self.assertEqual(ctx.filename, __file__)
247257

248258
def test_sealed_string_prefetched_nested_many_to_many(self):
249259
with self.assertNumQueries(3):
@@ -262,8 +272,9 @@ def test_sealed_string_prefetched_nested_many_to_many(self):
262272
)
263273
instance = instance.previous_locations.all()[0].previous_visitors.all()[0]
264274
message = 'Attempt to fetch many-to-many field "previous_locations" on sealed <SeaLion instance>'
265-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
275+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
266276
list(instance.previous_locations.all())
277+
self.assertEqual(ctx.filename, __file__)
267278

268279
def test_sealed_prefetch_prefetched_nested_many_to_many(self):
269280
instance = (
@@ -281,8 +292,9 @@ def test_sealed_prefetch_prefetched_nested_many_to_many(self):
281292
)
282293
instance = instance.previous_locations.all()[0].previous_visitors.all()[0]
283294
message = 'Attempt to fetch many-to-many field "previous_locations" on sealed <SeaLion instance>'
284-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
295+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
285296
list(instance.previous_locations.all())
297+
self.assertEqual(ctx.filename, __file__)
286298

287299
def test_prefetched_sealed_many_to_many(self):
288300
instance = SeaLion.objects.prefetch_related(
@@ -291,14 +303,16 @@ def test_prefetched_sealed_many_to_many(self):
291303
with self.assertNumQueries(0):
292304
self.assertSequenceEqual(instance.previous_locations.all(), [self.location])
293305
message = 'Attempt to fetch many-to-many field "previous_visitors" on sealed <Location instance>'
294-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
306+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
295307
list(instance.previous_locations.all()[0].previous_visitors.all())
308+
self.assertEqual(ctx.filename, __file__)
296309

297310
def test_sealed_deferred_parent_link(self):
298311
instance = GreatSeaLion.objects.only("pk").seal().get()
299312
message = 'Attempt to fetch related field "sealion_ptr" on sealed <GreatSeaLion instance>'
300-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
313+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
301314
instance.sealion_ptr
315+
self.assertEqual(ctx.filename, __file__)
302316

303317
def test_not_sealed_parent_link(self):
304318
instance = GreatSeaLion.objects.only("pk").get()
@@ -312,8 +326,9 @@ def test_sealed_parent_link(self):
312326
def test_sealed_generic_foreign_key(self):
313327
instance = Nickname.objects.seal().get()
314328
message = 'Attempt to fetch related field "content_object" on sealed <Nickname instance>'
315-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
329+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
316330
instance.content_object
331+
self.assertEqual(ctx.filename, __file__)
317332

318333
def test_not_sealed_generic_foreign_key(self):
319334
instance = Nickname.objects.get()
@@ -327,8 +342,9 @@ def test_sealed_prefetch_related_generic_foreign_key(self):
327342
def test_sealed_reverse_foreign_key(self):
328343
instance = Location.objects.seal().get()
329344
message = 'Attempt to fetch many-to-many field "visitors" on sealed <Location instance>'
330-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
345+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
331346
list(instance.visitors.all())
347+
self.assertEqual(ctx.filename, __file__)
332348

333349
def test_not_sealed_reverse_foreign_key(self):
334350
instance = Location.objects.get()
@@ -343,8 +359,9 @@ def test_sealed_reverse_parent_link(self):
343359
message = (
344360
'Attempt to fetch related field "greatsealion" on sealed <SeaLion instance>'
345361
)
346-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
362+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
347363
instance.greatsealion
364+
self.assertEqual(ctx.filename, __file__)
348365

349366
def test_not_sealed_reverse_parent_link(self):
350367
instance = SeaLion.objects.get()
@@ -357,10 +374,12 @@ def test_sealed_select_related_reverse_parent_link(self):
357374
def test_sealed_reverse_many_to_many(self):
358375
instance = Location.objects.seal().get()
359376
message = 'Attempt to fetch many-to-many field "previous_visitors" on sealed <Location instance>'
360-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
377+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
361378
list(instance.previous_visitors.all())
362-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
379+
self.assertEqual(ctx.filename, __file__)
380+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
363381
instance.previous_visitors.all()[0]
382+
self.assertEqual(ctx.filename, __file__)
364383

365384
def test_sealed_reverse_many_to_many_queryset(self):
366385
instance = Location.objects.seal().get()
@@ -385,10 +404,12 @@ def test_sealed_prefetched_reverse_many_to_many(self):
385404
def test_sealed_generic_relation(self):
386405
instance = SeaGull.objects.seal().get()
387406
message = 'Attempt to fetch many-to-many field "nicknames" on sealed <SeaGull instance>'
388-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
407+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
389408
list(instance.nicknames.all())
390-
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
409+
self.assertEqual(ctx.filename, __file__)
410+
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
391411
instance.nicknames.all()[0]
412+
self.assertEqual(ctx.filename, __file__)
392413

393414
def test_not_sealed_generic_relation(self):
394415
instance = SeaGull.objects.get()

0 commit comments

Comments
 (0)