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.
This commit is contained in:
parent
0a50311063
commit
cc3b3ba93a
|
@ -162,9 +162,14 @@ class Command(BaseCommand):
|
||||||
else:
|
else:
|
||||||
fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + ['']
|
fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + ['']
|
||||||
|
|
||||||
|
label_found = False
|
||||||
for fixture_dir in fixture_dirs:
|
for fixture_dir in fixture_dirs:
|
||||||
self.process_dir(fixture_dir, fixture_name, compression_formats,
|
found = self.process_dir(fixture_dir, fixture_name,
|
||||||
formats)
|
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,
|
def process_dir(self, fixture_dir, fixture_name, compression_formats,
|
||||||
serialization_formats):
|
serialization_formats):
|
||||||
|
@ -242,3 +247,5 @@ class Command(BaseCommand):
|
||||||
raise CommandError(
|
raise CommandError(
|
||||||
"No fixture data found for '%s'. (File format may be invalid.)" %
|
"No fixture data found for '%s'. (File format may be invalid.)" %
|
||||||
(fixture_name))
|
(fixture_name))
|
||||||
|
|
||||||
|
return label_found
|
||||||
|
|
|
@ -137,8 +137,14 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase):
|
||||||
'<Book: Music for all ages by Artist formerly known as "Prince" and Django Reinhardt>'
|
'<Book: Music for all ages by Artist formerly known as "Prince" and Django Reinhardt>'
|
||||||
])
|
])
|
||||||
|
|
||||||
# Load a fixture that doesn't exist
|
# Loading a fixture that doesn't exist results in an error
|
||||||
management.call_command('loaddata', 'unknown.json', verbosity=0, commit=False)
|
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
|
# object list is unaffected
|
||||||
self.assertQuerysetEqual(Article.objects.all(), [
|
self.assertQuerysetEqual(Article.objects.all(), [
|
||||||
|
@ -273,10 +279,11 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase):
|
||||||
|
|
||||||
def test_unmatched_identifier_loading(self):
|
def test_unmatched_identifier_loading(self):
|
||||||
# Try to load db fixture 3. This won't load because the database identifier doesn't match
|
# 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)
|
with self.assertRaises(management.CommandError):
|
||||||
self.assertQuerysetEqual(Article.objects.all(), [])
|
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(), [])
|
self.assertQuerysetEqual(Article.objects.all(), [])
|
||||||
|
|
||||||
def test_output_formats(self):
|
def test_output_formats(self):
|
||||||
|
|
|
@ -441,13 +441,14 @@ class TestFixtures(TestCase):
|
||||||
|
|
||||||
def test_loaddata_not_existant_fixture_file(self):
|
def test_loaddata_not_existant_fixture_file(self):
|
||||||
stdout_output = StringIO()
|
stdout_output = StringIO()
|
||||||
management.call_command(
|
with self.assertRaises(management.CommandError):
|
||||||
'loaddata',
|
management.call_command(
|
||||||
'this_fixture_doesnt_exist',
|
'loaddata',
|
||||||
verbosity=2,
|
'this_fixture_doesnt_exist',
|
||||||
commit=False,
|
verbosity=2,
|
||||||
stdout=stdout_output,
|
commit=False,
|
||||||
)
|
stdout=stdout_output,
|
||||||
|
)
|
||||||
self.assertTrue("No xml fixture 'this_fixture_doesnt_exist' in" in
|
self.assertTrue("No xml fixture 'this_fixture_doesnt_exist' in" in
|
||||||
force_text(stdout_output.getvalue()))
|
force_text(stdout_output.getvalue()))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue