Skip to content

Commit 3838765

Browse files
committed
Rework django_use_model to fix issues noted in #270
1 parent e305486 commit 3838765

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

pytest_django/plugin.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,15 @@ def _django_use_model(request):
418418
else:
419419
models = (model,)
420420

421-
with contextlib.closing(connection.schema_editor()) as schema:
421+
with connection.schema_editor() as schema:
422422
schema.deferred_sql = []
423423
for model_class in models:
424424
if not hasattr(model, '_meta'):
425425
raise ValueError('"model" must be a valid model class')
426426
schema.create_model(model_class)
427427

428428
def drop():
429-
with contextlib.closing(connection.schema_editor()) as schema:
429+
with connection.schema_editor() as schema:
430430
for model_class in models:
431431
schema.delete_model(model_class)
432432

tests/test_database.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ def test_transactions_enabled(self):
141141
assert not connection.in_atomic_block
142142

143143

144+
@pytest.mark.skipif(not hasattr(connection, 'schema_editor'),
145+
reason="This Django version does not support SchemaEditor")
144146
@pytest.mark.django_db
145147
class TestUseModel:
146148
"""Tests for django_use_model marker"""
@@ -163,10 +165,20 @@ def test_unmanaged_destroyed(self):
163165
self.test_unmanaged_missing()
164166

165167

166-
167-
@pytest.mark.django_use_model(Unmanaged)
168+
# TODO: Remove this next test before release
169+
@pytest.mark.skipif(not hasattr(connection, 'schema_editor'),
170+
reason="This Django version does not support SchemaEditor")
171+
@pytest.mark.django_db
172+
@pytest.mark.django_use_model(model=Unmanaged)
168173
def test_marked_test_not_get_hit():
169-
assert True is False
174+
"""
175+
A failing test like this was originally added to demonstrate that adding
176+
"@pytest.mark.django_use_model(ModelClass)" caused a test not to be
177+
collected. It's left here to demonstrate that it's now being collected
178+
when called with model=ModelClass instead; it should be removed before
179+
the next release.
180+
"""
181+
assert "This test failing shows that it is being collected and run" is False
170182

171183

172184
def test_unittest_interaction(django_testdir):

0 commit comments

Comments
 (0)