Skip to content

Commit 4aa6035

Browse files
committed
Test calling shared_db_wrapper multiple times
1 parent c2fd0cd commit 4aa6035

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/test_database.py

+30
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,36 @@ def test_shared_db_wrapper_not_leaking(db):
7575
assert not Item.objects.filter(name='shared item').exists()
7676

7777

78+
def test_nesting_shared_db_wrapper(db, shared_db_wrapper):
79+
finalizers = []
80+
request = type('FakeRequest', (object, ), {
81+
'funcargnames': (),
82+
'addfinalizer': lambda self, f: finalizers.append(f)
83+
})()
84+
85+
with shared_db_wrapper(request):
86+
item1 = Item.objects.create()
87+
88+
with shared_db_wrapper(request):
89+
item2 = Item.objects.create()
90+
91+
# Each call to shared_db_wrapper should have added 1 finalizer
92+
item1_fin, item2_fin = finalizers
93+
94+
# Make sure the data lives outside `with shared_db_wrapper`
95+
assert Item.objects.filter(pk=item1.pk).exists()
96+
assert Item.objects.filter(pk=item2.pk).exists()
97+
98+
# Check that finalizers remove the right data
99+
item2_fin()
100+
assert Item.objects.filter(pk=item1.pk).exists()
101+
assert not Item.objects.filter(pk=item2.pk).exists()
102+
103+
item1_fin()
104+
assert not Item.objects.filter(pk=item1.pk).exists()
105+
assert not Item.objects.filter(pk=item2.pk).exists()
106+
107+
78108
class TestDatabaseFixtures:
79109
"""Tests for the db and transactional_db fixtures"""
80110

0 commit comments

Comments
 (0)