Skip to content

Commit 7466c90

Browse files
committed
Rework django_use_model to fix issues noted in pytest-dev#270
1 parent 85f96ac commit 7466c90

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

docs/helpers.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ when trying to access the database.
9797
``pytest.mark.django_use_model`` - force model creation for unmanaged models
9898
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9999

100-
.. py:function:: pytest.mark.django_use_model(model)
100+
.. py:function:: pytest.mark.django_use_model(model=ModelClass)
101101
102-
:type model: django model or list of django models
102+
:type model: django model or list of django models, as kwarg
103103
:param model:
104104
Model or models to be created, should be used only with models that
105105
have ``Meta.managed = False``
@@ -115,7 +115,7 @@ when trying to access the database.
115115
Example usage::
116116

117117
@pytest.mark.django_db
118-
@pytest.mark.django_use_model(Unmanaged)
118+
@pytest.mark.django_use_model(model=Unmanaged)
119119
def test_unmanaged():
120120
assert Unmanaged.objects.count() >= 0
121121

pytest_django/plugin.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def _django_use_model(request):
402402
The test unit should be decorated:
403403
404404
@pytest.mark.django_db
405-
@pytest.mark.django_use_model(model)
405+
@pytest.mark.django_use_model(model=ModelClass)
406406
407407
:model: ModelClass, one or many
408408
"""
@@ -411,22 +411,22 @@ def _django_use_model(request):
411411
return
412412
from django.db import connection
413413

414-
model = request.getfuncargvalue('model')
414+
model = marker.kwargs['model']
415415

416416
if isinstance(model, (list, tuple)):
417417
models = model
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

+16-4
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"""
@@ -153,7 +155,7 @@ def test_unmanaged_missing(self):
153155
# Probably nothing else can be asserted here.
154156
Unmanaged.objects.exists()
155157

156-
@pytest.mark.django_use_model(Unmanaged)
158+
@pytest.mark.django_use_model(model=Unmanaged)
157159
def test_unmanaged_created(self):
158160
"""Make sure unmanaged models are created"""
159161
assert Unmanaged.objects.count() == 0
@@ -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)