-
Notifications
You must be signed in to change notification settings - Fork 348
/
Copy pathtest_database.py
233 lines (173 loc) · 7.35 KB
/
test_database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
from __future__ import with_statement
import pytest
from django.db import connection, DatabaseError
from django.test.testcases import connections_support_transactions
from pytest_django.pytest_compat import getfixturevalue
from pytest_django_test.app.models import Item, Unmanaged
def test_noaccess():
with pytest.raises(pytest.fail.Exception):
Item.objects.create(name='spam')
with pytest.raises(pytest.fail.Exception):
Item.objects.count()
@pytest.fixture
def noaccess():
with pytest.raises(pytest.fail.Exception):
Item.objects.create(name='spam')
with pytest.raises(pytest.fail.Exception):
Item.objects.count()
def test_noaccess_fixture(noaccess):
# Setup will fail if this test needs to fail
pass
class TestDatabaseFixtures:
"""Tests for the db and transactional_db fixtures"""
@pytest.fixture(params=['db', 'transactional_db'])
def both_dbs(self, request):
if request.param == 'transactional_db':
return getfixturevalue(request, 'transactional_db')
elif request.param == 'db':
return getfixturevalue(request, 'db')
def test_access(self, both_dbs):
Item.objects.create(name='spam')
def test_clean_db(self, both_dbs):
# Relies on the order: test_access created an object
assert Item.objects.count() == 0
def test_transactions_disabled(self, db):
if not connections_support_transactions():
pytest.skip('transactions required for this test')
assert connection.in_atomic_block
def test_transactions_enabled(self, transactional_db):
if not connections_support_transactions():
pytest.skip('transactions required for this test')
assert not connection.in_atomic_block
@pytest.fixture
def mydb(self, both_dbs):
# This fixture must be able to access the database
Item.objects.create(name='spam')
def test_mydb(self, mydb):
if not connections_support_transactions():
pytest.skip('transactions required for this test')
# Check the fixture had access to the db
item = Item.objects.get(name='spam')
assert item
def test_fixture_clean(self, both_dbs):
# Relies on the order: test_mydb created an object
# See https://github.com/pytest-dev/pytest-django/issues/17
assert Item.objects.count() == 0
@pytest.fixture
def fin(self, request, both_dbs):
# This finalizer must be able to access the database
request.addfinalizer(lambda: Item.objects.create(name='spam'))
def test_fin(self, fin):
# Check finalizer has db access (teardown will fail if not)
pass
class TestDatabaseFixturesBothOrder:
@pytest.fixture
def fixture_with_db(self, db):
Item.objects.create(name='spam')
@pytest.fixture
def fixture_with_transdb(self, transactional_db):
Item.objects.create(name='spam')
def test_trans(self, fixture_with_transdb):
pass
def test_db(self, fixture_with_db):
pass
def test_db_trans(self, fixture_with_db, fixture_with_transdb):
pass
def test_trans_db(self, fixture_with_transdb, fixture_with_db):
pass
class TestDatabaseMarker:
"Tests for the django_db marker."
@pytest.mark.django_db
def test_access(self):
Item.objects.create(name='spam')
@pytest.mark.django_db
def test_clean_db(self):
# Relies on the order: test_access created an object.
assert Item.objects.count() == 0
@pytest.mark.django_db
def test_transactions_disabled(self):
if not connections_support_transactions():
pytest.skip('transactions required for this test')
assert connection.in_atomic_block
@pytest.mark.django_db(transaction=False)
def test_transactions_disabled_explicit(self):
if not connections_support_transactions():
pytest.skip('transactions required for this test')
assert connection.in_atomic_block
@pytest.mark.django_db(transaction=True)
def test_transactions_enabled(self):
if not connections_support_transactions():
pytest.skip('transactions required for this test')
assert not connection.in_atomic_block
@pytest.mark.skipif(not hasattr(connection, 'schema_editor'),
reason="This Django version does not support SchemaEditor")
@pytest.mark.django_db
class TestUseModel:
"""Tests for django_use_model marker"""
def test_unmanaged_missing(self):
"""Test that Unmanaged model is not created by default"""
with pytest.raises(DatabaseError):
# If table does not exists, django will raise DatabaseError
# but the message will depend on the backend.
# Probably nothing else can be asserted here.
Unmanaged.objects.exists()
@pytest.mark.django_use_model(model=Unmanaged)
def test_unmanaged_created(self):
"""Make sure unmanaged models are created"""
assert Unmanaged.objects.count() == 0
def test_unmanaged_destroyed(self):
"""Test that Unmanaged model was destroyed after last use"""
self.test_unmanaged_missing()
def test_unittest_interaction(django_testdir):
"Test that (non-Django) unittests cannot access the DB."
django_testdir.create_test_module('''
import pytest
import unittest
from .app.models import Item
class TestCase_setupClass(unittest.TestCase):
@classmethod
def setUpClass(cls):
Item.objects.create(name='foo')
def test_db_access_1(self):
Item.objects.count() == 1
class TestCase_setUp(unittest.TestCase):
@classmethod
def setUp(cls):
Item.objects.create(name='foo')
def test_db_access_2(self):
Item.objects.count() == 1
class TestCase(unittest.TestCase):
def test_db_access_3(self):
Item.objects.count() == 1
''')
result = django_testdir.runpytest_subprocess('-v', '--reuse-db')
result.stdout.fnmatch_lines([
"*test_db_access_1 ERROR*",
"*test_db_access_2 FAILED*",
"*test_db_access_3 FAILED*",
"*ERROR at setup of TestCase_setupClass.test_db_access_1*",
'*Failed: Database access not allowed, use the "django_db" mark, '
'or the "db" or "transactional_db" fixtures to enable it.',
])
class Test_database_blocking:
def test_db_access_in_conftest(self, django_testdir):
"""Make sure database access in conftest module is prohibited."""
django_testdir.makeconftest("""
from tpkg.app.models import Item
Item.objects.get()
""")
result = django_testdir.runpytest_subprocess('-v')
result.stderr.fnmatch_lines([
'*Failed: Database access not allowed, use the "django_db" mark, '
'or the "db" or "transactional_db" fixtures to enable it.*',
])
def test_db_access_in_test_module(self, django_testdir):
django_testdir.create_test_module("""
from tpkg.app.models import Item
Item.objects.get()
""")
result = django_testdir.runpytest_subprocess('-v')
result.stdout.fnmatch_lines([
'*Failed: Database access not allowed, use the "django_db" mark, '
'or the "db" or "transactional_db" fixtures to enable it.',
])