8000 [1.7.x] Fixed #23651 -- Isolated non-existent fixture tests · alex-python/django@da0ebe3 · GitHub
[go: up one dir, main page]

Skip to content

Commit da0ebe3

Browse files
committed
[1.7.x] Fixed #23651 -- Isolated non-existent fixture tests
Previous versions of the tests were buggy, as initial_data.json did exist and the test wasn't failing. It was finally failing on Python 3.4.2. Thanks Raphaël Hertzog for the report (and Debian bug #765117 contributors). Backport of 7a893ee from master.
1 parent 555952c commit da0ebe3

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

tests/fixtures/tests.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.core import management
77
from django.db import connection, IntegrityError
88
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
9+
from django.utils.encoding import force_text
910
from django.utils import six
1011

1112
from .models import Article, Book, Spy, Tag, Visa
@@ -157,18 +158,6 @@ def test_loading_and_dumping(self):
157158
'<Book: Music for all ages by Artist formerly known as "Prince" and Django Reinhardt>'
158159
])
159160

160-
# Loading a fixture that doesn't exist emits a warning
161-
with warnings.catch_warnings(record=True) as w:
162-
warnings.simplefilter("always")
163-
management.call_command('loaddata', 'unknown.json', verbosity=0)
164-
self.assertEqual(len(w), 1)
165-
self.assertTrue(w[0].message, "No fixture named 'unknown' found.")
166-
167-
# An attempt to load a nonexistent 'initial_data' fixture isn't an error
168-
with warnings.catch_warnings(record=True) as w:
169-
management.call_command('loaddata', 'initial_data.json', verbosity=0)
170-
self.assertEqual(len(w), 0)
171-
172161
# object list is unaffected
173162
self.assertQuerysetEqual(Article.objects.all(), [
174163
'<Article: XML identified as leading cause of cancer>',
@@ -383,6 +372,35 @@ def test_output_formats(self):
383372
<django-objects version="1.0"><object pk="1" model="fixtures.category"><field type="CharField" name="title">News Stories</field><field type="TextField" name="description">Latest news stories</field></object><object pk="2" model="fixtures.article"><field type="CharField" name="headline">Poker has no place on ESPN</field><field type="DateTimeField" name="pub_date">2006-06-16T12:00:00</field></object><object pk="3" model="fixtures.article"><field type="CharField" name="headline">Time to reform copyright</field><field type="DateTimeField" name="pub_date">2006-06-16T13:00:00</field></object><object pk="1" model="fixtures.tag"><field type="CharField" name="name">copyright</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="2" model="fixtures.tag"><field type="CharField" name="name">law</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="1" model="fixtures.person"><field type="CharField" name="name">Django Reinhardt</field></object><object pk="2" model="fixtures.person"><field type="CharField" name="name">Stephane Grappelli</field></object><object pk="3" model="fixtures.person"><field type="CharField" name="name">Prince</field></object><object pk="10" model="fixtures.book"><field type="CharField" name="name">Achieving self-awareness of Python programs</field><field to="fixtures.person" name="authors" rel="ManyToManyRel"></field></object></django-objects>""", format='xml', natural_foreign_keys=True)
384373

385374

375+
class NonExistentFixtureTests(TestCase):
376+
"""
377+
Custom class to limit fixture dirs.
378+
"""
379+
available_apps = ['django.contrib.auth', 'django.contrib.contenttypes']
380+
381+
def test_loaddata_not_existent_fixture_file(self):
382+
stdout_output = six.StringIO()
383+
with warnings.catch_warnings(record=True) as w:
384+
warnings.simplefilter("always")
385+
# With verbosity=2, we get both stdout output and a warning
386+
management.call_command(
387+
'loaddata',
388+
'this_fixture_doesnt_exist',
389+
verbosity=2,
390+
stdout=stdout_output,
391+
)
392+
self.assertIn("No fixture 'this_fixture_doesnt_exist' in",
393+
force_text(stdout_output.getvalue()))
394+
self.assertEqual(len(w), 1)
395+
self.assertEqual(force_text(w[0].message),
396+
"No fixture named 'this_fixture_doesnt_exist' found.")
397+
398+
# An attempt to load a non-existent 'initial_data' fixture doesn't produce any warning
399+
with warnings.catch_warnings(record=True) as w:
400+
management.call_command('loaddata', 'initial_data.json', verbosity=0)
401+
self.assertEqual(len(w), 0)
402+
403+
386404
class FixtureTransactionTests(DumpDataAssertMixin, TransactionTestCase):
387405

388406
available_apps = [

tests/fixtures_regress/tests.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from django.test import (TestCase, TransactionTestCase, skipIfDBFeature,
1717
skipUnlessDBFeature)
1818
from django.test import override_settings
19-
from django.utils.encoding import force_text
2019
from django.utils._os import upath
2120
from django.utils import six
2221
from django.utils.six import PY3, StringIO
@@ -459,18 +458,6 @@ def test_loaddata_no_fixture_specified(self):
459458
verbosity=0,
460459
)
461460

462-
def test_loaddata_not_existant_fixture_file(self):
463-
stdout_output = StringIO()
464-
with warnings.catch_warnings(record=True):
465-
management.call_command(
466-
'loaddata',
467-
'this_fixture_doesnt_exist',
468-
verbosity=2,
469-
stdout=stdout_output,
470-
)
471-
self.assertTrue("No fixture 'this_fixture_doesnt_exist' in" in
472-
force_text(stdout_output.getvalue()))
473-
474461
def test_ticket_20820(self):
475462
"""
476463
Regression for ticket #20820 -- loaddata on a model that inherits

0 commit comments

Comments
 (0)
0