From cc3b3ba93a7bfdd2ece739e97e36150a719acd3e Mon Sep 17 00:00:00 2001 From: Senko Rasic Date: Sat, 18 May 2013 17:51:14 +0200 Subject: [PATCH 1/3] Fixed #18990: Loaddata now complains if fixture doesn't exist The fixture named "initial_data" is exceptional though; if it doesn't exist, the error is not raised. This allows syncdb and flush management commands to attempt to load it without causing an error if it doesn't exist. --- django/core/management/commands/loaddata.py | 11 +++++++++-- tests/fixtures/tests.py | 17 ++++++++++++----- tests/fixtures_regress/tests.py | 15 ++++++++------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index c95d11cf60..fbafed3f92 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -162,9 +162,14 @@ class Command(BaseCommand): else: fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + [''] + label_found = False for fixture_dir in fixture_dirs: - self.process_dir(fixture_dir, fixture_name, compression_formats, - formats) + found = self.process_dir(fixture_dir, fixture_name, + compression_formats, formats) + label_found = label_found or found + + if fixture_name != 'initial_data' and not label_found: + raise CommandError("No fixture named '%s' found." % fixture_name) def process_dir(self, fixture_dir, fixture_name, compression_formats, serialization_formats): @@ -242,3 +247,5 @@ class Command(BaseCommand): raise CommandError( "No fixture data found for '%s'. (File format may be invalid.)" % (fixture_name)) + + return label_found diff --git a/tests/fixtures/tests.py b/tests/fixtures/tests.py index 103612198e..93f2438ce9 100644 --- a/tests/fixtures/tests.py +++ b/tests/fixtures/tests.py @@ -137,8 +137,14 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase): '' ]) - # Load a fixture that doesn't exist - management.call_command('loaddata', 'unknown.json', verbosity=0, commit=False) + # Loading a fixture that doesn't exist results in an error + with self.assertRaises(management.CommandError): + management.call_command('loaddata', 'unknown.json', verbosity=0, + commit=False) + + # An attempt to load a nonexistent 'initial_data' fixture isn't an error + management.call_command('loaddata', 'initial_data.json', verbosity=0, + commit=False) # object list is unaffected self.assertQuerysetEqual(Article.objects.all(), [ @@ -273,10 +279,11 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase): def test_unmatched_identifier_loading(self): # Try to load db fixture 3. This won't load because the database identifier doesn't match - management.call_command('loaddata', 'db_fixture_3', verbosity=0, commit=False) - self.assertQuerysetEqual(Article.objects.all(), []) + with self.assertRaises(management.CommandError): + management.call_command('loaddata', 'db_fixture_3', verbosity=0, commit=False) - management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default', commit=False) + with self.assertRaises(management.CommandError): + management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default', commit=False) self.assertQuerysetEqual(Article.objects.all(), []) def test_output_formats(self): diff --git a/tests/fixtures_regress/tests.py b/tests/fixtures_regress/tests.py index 02e923e386..df84d77a3f 100644 --- a/tests/fixtures_regress/tests.py +++ b/tests/fixtures_regress/tests.py @@ -441,13 +441,14 @@ class TestFixtures(TestCase): def test_loaddata_not_existant_fixture_file(self): stdout_output = StringIO() - management.call_command( - 'loaddata', - 'this_fixture_doesnt_exist', - verbosity=2, - commit=False, - stdout=stdout_output, - ) + with self.assertRaises(management.CommandError): + management.call_command( + 'loaddata', + 'this_fixture_doesnt_exist', + verbosity=2, + commit=False, + stdout=stdout_output, + ) self.assertTrue("No xml fixture 'this_fixture_doesnt_exist' in" in force_text(stdout_output.getvalue())) From c44a2c40fe0ed79b0fa00233a204d41e9c677750 Mon Sep 17 00:00:00 2001 From: Senko Rasic Date: Sun, 19 May 2013 11:20:10 +0200 Subject: [PATCH 2/3] Fixed #18990 -- Loaddata now complains if fixture doesn't exist If the fixture doesn't exist, loaddata will output a warning. The fixture named "initial_data" is exceptional though; if it doesn't exist, the warning is not emitted. This allows syncdb and flush management commands to attempt to load it without causing spurious warnings. Thanks to Derega, ptone, dirigeant and d1ffuz0r for contributions to the ticket. --- django/core/management/commands/loaddata.py | 3 ++- tests/fixtures/tests.py | 18 ++++++++++++------ tests/fixtures_model_package/tests.py | 5 ++++- tests/fixtures_regress/tests.py | 3 ++- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index fbafed3f92..ab9f7468c4 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -4,6 +4,7 @@ import os import gzip import zipfile from optparse import make_option +import warnings from django.conf import settings from django.core import serializers @@ -169,7 +170,7 @@ class Command(BaseCommand): label_found = label_found or found if fixture_name != 'initial_data' and not label_found: - raise CommandError("No fixture named '%s' found." % fixture_name) + warnings.warn("No fixture named '%s' found." % fixture_name) def process_dir(self, fixture_dir, fixture_name, compression_formats, serialization_formats): diff --git a/tests/fixtures/tests.py b/tests/fixtures/tests.py index 93f2438ce9..f954933046 100644 --- a/tests/fixtures/tests.py +++ b/tests/fixtures/tests.py @@ -1,5 +1,7 @@ from __future__ import absolute_import +import warnings + from django.contrib.sites.models import Site from django.core import management from django.db import connection, IntegrityError @@ -137,14 +139,18 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase): '' ]) - # Loading a fixture that doesn't exist results in an error - with self.assertRaises(management.CommandError): + # Loading a fixture that doesn't exist emits a warning + with warnings.catch_warnings(record=True) as w: management.call_command('loaddata', 'unknown.json', verbosity=0, commit=False) + self.assertEqual(len(w), 1) + self.assertTrue(w[0].message, "No fixture named 'unknown' found.") # An attempt to load a nonexistent 'initial_data' fixture isn't an error - management.call_command('loaddata', 'initial_data.json', verbosity=0, - commit=False) + with warnings.catch_warnings(record=True) as w: + management.call_command('loaddata', 'initial_data.json', verbosity=0, + commit=False) + self.assertEqual(len(w), 0) # object list is unaffected self.assertQuerysetEqual(Article.objects.all(), [ @@ -279,10 +285,10 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase): def test_unmatched_identifier_loading(self): # Try to load db fixture 3. This won't load because the database identifier doesn't match - with self.assertRaises(management.CommandError): + with warnings.catch_warnings(record=True): management.call_command('loaddata', 'db_fixture_3', verbosity=0, commit=False) - with self.assertRaises(management.CommandError): + with warnings.catch_warnings(record=True): management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default', commit=False) self.assertQuerysetEqual(Article.objects.all(), []) diff --git a/tests/fixtures_model_package/tests.py b/tests/fixtures_model_package/tests.py index c250f647ce..af6b059c66 100644 --- a/tests/fixtures_model_package/tests.py +++ b/tests/fixtures_model_package/tests.py @@ -100,7 +100,10 @@ class FixtureTestCase(TestCase): ) # Load a fixture that doesn't exist - management.call_command("loaddata", "unknown.json", verbosity=0, commit=False) + import warnings + with warnings.catch_warnings(record=True): + management.call_command("loaddata", "unknown.json", verbosity=0, commit=False) + self.assertQuerysetEqual( Article.objects.all(), [ "Django conquers world!", diff --git a/tests/fixtures_regress/tests.py b/tests/fixtures_regress/tests.py index df84d77a3f..97ad6c326a 100644 --- a/tests/fixtures_regress/tests.py +++ b/tests/fixtures_regress/tests.py @@ -441,7 +441,8 @@ class TestFixtures(TestCase): def test_loaddata_not_existant_fixture_file(self): stdout_output = StringIO() - with self.assertRaises(management.CommandError): + import warnings + with warnings.catch_warnings(record=True): management.call_command( 'loaddata', 'this_fixture_doesnt_exist', From 65c557115f6e76293c39ce7b73b62216911f9489 Mon Sep 17 00:00:00 2001 From: Senko Rasic Date: Sun, 19 May 2013 12:49:03 +0200 Subject: [PATCH 3/3] fix warnings imports in fixtures tests --- tests/fixtures_model_package/tests.py | 3 ++- tests/fixtures_regress/tests.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/fixtures_model_package/tests.py b/tests/fixtures_model_package/tests.py index af6b059c66..fbd0271336 100644 --- a/tests/fixtures_model_package/tests.py +++ b/tests/fixtures_model_package/tests.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import warnings + from django.core import management from django.db import transaction from django.test import TestCase, TransactionTestCase @@ -100,7 +102,6 @@ class FixtureTestCase(TestCase): ) # Load a fixture that doesn't exist - import warnings with warnings.catch_warnings(record=True): management.call_command("loaddata", "unknown.json", verbosity=0, commit=False) diff --git a/tests/fixtures_regress/tests.py b/tests/fixtures_regress/tests.py index 97ad6c326a..5114302267 100644 --- a/tests/fixtures_regress/tests.py +++ b/tests/fixtures_regress/tests.py @@ -4,6 +4,7 @@ from __future__ import absolute_import, unicode_literals import os import re +import warnings from django.core.serializers.base import DeserializationError from django.core import management @@ -441,7 +442,6 @@ class TestFixtures(TestCase): def test_loaddata_not_existant_fixture_file(self): stdout_output = StringIO() - import warnings with warnings.catch_warnings(record=True): management.call_command( 'loaddata',