|
6 | 6 | from django.core import management
|
7 | 7 | from django.db import connection, IntegrityError
|
8 | 8 | from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
|
| 9 | +from django.utils.encoding import force_text |
9 | 10 | from django.utils import six
|
10 | 11 |
|
11 | 12 | from .models import Article, Book, Spy, Tag, Visa
|
@@ -157,18 +158,6 @@ def test_loading_and_dumping(self):
|
157 | 158 | '<Book: Music for all ages by Artist formerly known as "Prince" and Django Reinhardt>'
|
158 | 159 | ])
|
159 | 160 |
|
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 |
| - |
172 | 161 | # object list is unaffected
|
173 | 162 | self.assertQuerysetEqual(Article.objects.all(), [
|
174 | 163 | '<Article: XML identified as leading cause of cancer>',
|
@@ -383,6 +372,35 @@ def test_output_formats(self):
|
383 | 372 | <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)
|
384 | 373 |
|
385 | 374 |
|
| 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 | + |
386 | 404 | class FixtureTransactionTests(DumpDataAssertMixin, TransactionTestCase):
|
387 | 405 |
|
388 | 406 | available_apps = [
|
|
0 commit comments