Skip to content

Commit 176ac9f

Browse files
committed
Rework django_use_model to fix issues noted in pytest-dev#270
1 parent 32b5c5c commit 176ac9f

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

docs/helpers.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ on what marks are and for notes on using_ them.
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 @@ on what marks are and for notes on using_ them.
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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def _django_use_model(request):
390390
The test unit should be decorated:
391391
392392
@pytest.mark.django_db
393-
@pytest.mark.django_use_model(model)
393+
@pytest.mark.django_use_model(model=ModelClass)
394394
395395
:model: ModelClass, one or many
396396
"""
@@ -399,22 +399,22 @@ def _django_use_model(request):
399399
return
400400
from django.db import connection
401401

402-
model = request.getfuncargvalue('model')
402+
model = marker.kwargs['model']
403403

404404
if isinstance(model, (list, tuple)):
405405
models = model
406406
else:
407407
models = (model,)
408408

409-
with contextlib.closing(connection.schema_editor()) as schema:
409+
with connection.schema_editor() as schema:
410410
schema.deferred_sql = []
411411
for model_class in models:
412412
if not hasattr(model, '_meta'):
413413
raise ValueError('"model" must be a valid model class')
414414
schema.create_model(model_class)
415415

416416
def drop():
417-
with contextlib.closing(connection.schema_editor()) as schema:
417+
with connection.schema_editor() as schema:
418418
for model_class in models:
419419
schema.delete_model(model_class)
420420

tests/test_database.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ def test_transactions_enabled(self):
164164
assert not noop_transactions()
165165

166166

167+
@pytest.mark.skipif(not hasattr(connection, 'schema_editor'),
168+
reason="This Django version does not support SchemaEditor")
167169
@pytest.mark.django_db
168170
class TestUseModel:
169171
"""Tests for django_use_model marker"""
@@ -176,7 +178,7 @@ def test_unmanaged_missing(self):
176178
# Probably nothing else can be asserted here.
177179
Unmanaged.objects.exists()
178180

179-
@pytest.mark.django_use_model(Unmanaged)
181+
@pytest.mark.django_use_model(model=Unmanaged)
180182
def test_unmanaged_created(self):
181183
"""Make sure unmanaged models are created"""
182184
assert Unmanaged.objects.count() == 0
@@ -186,10 +188,20 @@ def test_unmanaged_destroyed(self):
186188
self.test_unmanaged_missing()
187189

188190

189-
190-
@pytest.mark.django_use_model(Unmanaged)
191+
# TODO: Remove this next test before release
192+
@pytest.mark.skipif(not hasattr(connection, 'schema_editor'),
193+
reason="This Django version does not support SchemaEditor")
194+
@pytest.mark.django_db
195+
@pytest.mark.django_use_model(model=Unmanaged)
191196
def test_marked_test_not_get_hit():
192-
assert True is False
197+
"""
198+
A failing test like this was originally added to demonstrate that adding
199+
"@pytest.mark.django_use_model(ModelClass)" caused a test not to be
200+
collected. It's left here to demonstrate that it's now being collected
201+
when called with model=ModelClass instead; it should be removed before
202+
the next release.
203+
"""
204+
assert "This test failing shows that it is being collected and run" is False
193205

194206

195207
def test_unittest_interaction(django_testdir):

0 commit comments

Comments
 (0)