Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit c44a2c40fe0ed79b0fa00233a204d41e9c677750 1 parent cc3b3ba
Senko Rašić senko authored
3  django/core/management/commands/loaddata.py
View
@@ -4,6 +4,7 @@
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 @@ def load_label(self, fixture_label, app_fixtures):
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):
18 tests/fixtures/tests.py
View
@@ -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 @@ def test_loading_and_dumping(self):
'<Book: Music for all ages by Artist formerly known as "Prince" and Django Reinhardt>'
])
- # 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 @@ def test_loading_using(self):
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(), [])
5 tests/fixtures_model_package/tests.py
View
@@ -100,7 +100,10 @@ def test_loaddata(self):
)
# 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!",
3  tests/fixtures_regress/tests.py
View
@@ -441,7 +441,8 @@ def test_loaddata_no_fixture_specified(self):
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',
Please sign in to comment.
Something went wrong with that request. Please try again.