Skip to content

Commit

Permalink
Assert UnsealedAttributeAccess are emitted from the right location.
Browse files Browse the repository at this point in the history
  • Loading branch information
charettes committed Aug 13, 2024
1 parent 7cbbe23 commit 4aee7ff
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ def test_sealed_deferred_field(self):
message = (
'Attempt to fetch deferred field "weight" on sealed <SeaLion instance>'
)
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
instance.weight
self.assertEqual(ctx.filename, __file__)

def test_not_sealed_deferred_field(self):
instance = SeaLion.objects.defer("weight").get()
Expand All @@ -70,8 +71,9 @@ def test_sealed_foreign_key(self):
message = (
'Attempt to fetch related field "location" on sealed <SeaLion instance>'
)
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
instance.location
self.assertEqual(ctx.filename, __file__)

def test_not_sealed_foreign_key(self):
instance = SeaLion.objects.get()
Expand All @@ -84,10 +86,11 @@ def test_sealed_select_related_foreign_key(self):
message = (
'Attempt to fetch related field "location" on sealed <SeaLion instance>'
)
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
instance.sealion.location
instance = SeaGull.objects.select_related("sealion__location").seal().get()
self.assertEqual(instance.sealion.location, self.location)
self.assertEqual(ctx.filename, __file__)

def test_sealed_select_related_none_foreign_key(self):
SeaLion.objects.update(location=None)
Expand Down Expand Up @@ -145,16 +148,18 @@ def test_sealed_select_related_deferred_field(self):
message = (
'Attempt to fetch deferred field "longitude" on sealed <Location instance>'
)
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
instance.sealion.location.longitude
self.assertEqual(ctx.filename, __file__)

def test_sealed_one_to_one(self):
instance = SeaGull.objects.seal().get()
message = (
'Attempt to fetch related field "sealion" on sealed <SeaGull instance>'
)
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
instance.sealion
self.assertEqual(ctx.filename, __file__)

def test_not_sealed_one_to_one(self):
instance = SeaGull.objects.get()
Expand Down Expand Up @@ -185,10 +190,12 @@ def test_sealed_prefetch_related_reverse_one_to_one(self):
def test_sealed_many_to_many(self):
instance = SeaLion.objects.seal().get()
message = 'Attempt to fetch many-to-many field "previous_locations" on sealed <SeaLion instance>'
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
list(instance.previous_locations.all())
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
self.assertEqual(ctx.filename, __file__)
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
instance.previous_locations.all()[0]
self.assertEqual(ctx.filename, __file__)

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

def test_sealed_prefetch_prefetched_many_to_many(self):
instance = (
Expand All @@ -227,8 +235,9 @@ def test_sealed_prefetch_prefetched_many_to_many(self):
self.assertSequenceEqual(instance.previous_locations.all(), [self.location])
instance = instance.previous_locations.all()[0]
message = 'Attempt to fetch many-to-many field "previous_visitors" on sealed <Location instance>'
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
list(instance.previous_visitors.all())
self.assertEqual(ctx.filename, __file__)

def test_sealed_prefetch_queryset_prefetched_many_to_many(self):
instance = (
Expand All @@ -242,8 +251,9 @@ def test_sealed_prefetch_queryset_prefetched_many_to_many(self):
self.assertSequenceEqual(instance.previous_locations.all(), [self.location])
instance = instance.previous_locations.all()[0]
message = 'Attempt to fetch many-to-many field "previous_visitors" on sealed <Location instance>'
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
list(instance.previous_visitors.all())
self.assertEqual(ctx.filename, __file__)

def test_sealed_string_prefetched_nested_many_to_many(self):
with self.assertNumQueries(3):
Expand All @@ -262,8 +272,9 @@ def test_sealed_string_prefetched_nested_many_to_many(self):
)
instance = instance.previous_locations.all()[0].previous_visitors.all()[0]
message = 'Attempt to fetch many-to-many field "previous_locations" on sealed <SeaLion instance>'
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
list(instance.previous_locations.all())
self.assertEqual(ctx.filename, __file__)

def test_sealed_prefetch_prefetched_nested_many_to_many(self):
instance = (
Expand All @@ -281,8 +292,9 @@ def test_sealed_prefetch_prefetched_nested_many_to_many(self):
)
instance = instance.previous_locations.all()[0].previous_visitors.all()[0]
message = 'Attempt to fetch many-to-many field "previous_locations" on sealed <SeaLion instance>'
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
list(instance.previous_locations.all())
self.assertEqual(ctx.filename, __file__)

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

def test_sealed_deferred_parent_link(self):
instance = GreatSeaLion.objects.only("pk").seal().get()
message = 'Attempt to fetch related field "sealion_ptr" on sealed <GreatSeaLion instance>'
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
instance.sealion_ptr
self.assertEqual(ctx.filename, __file__)

def test_not_sealed_parent_link(self):
instance = GreatSeaLion.objects.only("pk").get()
Expand All @@ -312,8 +326,9 @@ def test_sealed_parent_link(self):
def test_sealed_generic_foreign_key(self):
instance = Nickname.objects.seal().get()
message = 'Attempt to fetch related field "content_object" on sealed <Nickname instance>'
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
instance.content_object
self.assertEqual(ctx.filename, __file__)

def test_not_sealed_generic_foreign_key(self):
instance = Nickname.objects.get()
Expand All @@ -327,8 +342,9 @@ def test_sealed_prefetch_related_generic_foreign_key(self):
def test_sealed_reverse_foreign_key(self):
instance = Location.objects.seal().get()
message = 'Attempt to fetch many-to-many field "visitors" on sealed <Location instance>'
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
list(instance.visitors.all())
self.assertEqual(ctx.filename, __file__)

def test_not_sealed_reverse_foreign_key(self):
instance = Location.objects.get()
Expand All @@ -343,8 +359,9 @@ def test_sealed_reverse_parent_link(self):
message = (
'Attempt to fetch related field "greatsealion" on sealed <SeaLion instance>'
)
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
instance.greatsealion
self.assertEqual(ctx.filename, __file__)

def test_not_sealed_reverse_parent_link(self):
instance = SeaLion.objects.get()
Expand All @@ -357,10 +374,12 @@ def test_sealed_select_related_reverse_parent_link(self):
def test_sealed_reverse_many_to_many(self):
instance = Location.objects.seal().get()
message = 'Attempt to fetch many-to-many field "previous_visitors" on sealed <Location instance>'
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
list(instance.previous_visitors.all())
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
self.assertEqual(ctx.filename, __file__)
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
instance.previous_visitors.all()[0]
self.assertEqual(ctx.filename, __file__)

def test_sealed_reverse_many_to_many_queryset(self):
instance = Location.objects.seal().get()
Expand All @@ -385,10 +404,12 @@ def test_sealed_prefetched_reverse_many_to_many(self):
def test_sealed_generic_relation(self):
instance = SeaGull.objects.seal().get()
message = 'Attempt to fetch many-to-many field "nicknames" on sealed <SeaGull instance>'
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
list(instance.nicknames.all())
with self.assertWarnsMessage(UnsealedAttributeAccess, message):
self.assertEqual(ctx.filename, __file__)
with self.assertWarnsMessage(UnsealedAttributeAccess, message) as ctx:
instance.nicknames.all()[0]
self.assertEqual(ctx.filename, __file__)

def test_not_sealed_generic_relation(self):
instance = SeaGull.objects.get()
Expand Down

0 comments on commit 4aee7ff

Please sign in to comment.