Merge pull request #1134 from senko/ticket_18990

Fixed #18990: Loaddata now complains if fixture doesn't exist
This commit is contained in:
Andrew Godwin 2013-05-19 03:54:29 -07:00
commit 7a99d1e167
4 changed files with 42 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import os
import gzip import gzip
import zipfile import zipfile
from optparse import make_option from optparse import make_option
import warnings
from django.conf import settings from django.conf import settings
from django.core import serializers from django.core import serializers
@ -162,9 +163,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:
warnings.warn("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 +248,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

View File

@ -1,5 +1,7 @@
from __future__ import absolute_import from __future__ import absolute_import
import warnings
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core import management from django.core import management
from django.db import connection, IntegrityError from django.db import connection, IntegrityError
@ -137,8 +139,18 @@ 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 emits a warning
management.call_command('loaddata', 'unknown.json', verbosity=0, commit=False) 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
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 # object list is unaffected
self.assertQuerysetEqual(Article.objects.all(), [ self.assertQuerysetEqual(Article.objects.all(), [
@ -273,9 +285,10 @@ 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
with warnings.catch_warnings(record=True):
management.call_command('loaddata', 'db_fixture_3', verbosity=0, commit=False) management.call_command('loaddata', 'db_fixture_3', verbosity=0, commit=False)
self.assertQuerysetEqual(Article.objects.all(), [])
with warnings.catch_warnings(record=True):
management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default', commit=False) management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default', commit=False)
self.assertQuerysetEqual(Article.objects.all(), []) self.assertQuerysetEqual(Article.objects.all(), [])

View File

@ -1,5 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import warnings
from django.core import management from django.core import management
from django.db import transaction from django.db import transaction
from django.test import TestCase, TransactionTestCase from django.test import TestCase, TransactionTestCase
@ -100,7 +102,9 @@ class FixtureTestCase(TestCase):
) )
# Load a fixture that doesn't exist # Load a fixture that doesn't exist
with warnings.catch_warnings(record=True):
management.call_command("loaddata", "unknown.json", verbosity=0, commit=False) management.call_command("loaddata", "unknown.json", verbosity=0, commit=False)
self.assertQuerysetEqual( self.assertQuerysetEqual(
Article.objects.all(), [ Article.objects.all(), [
"Django conquers world!", "Django conquers world!",

View File

@ -4,6 +4,7 @@ from __future__ import absolute_import, unicode_literals
import os import os
import re import re
import warnings
from django.core.serializers.base import DeserializationError from django.core.serializers.base import DeserializationError
from django.core import management from django.core import management
@ -441,6 +442,7 @@ 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()
with warnings.catch_warnings(record=True):
management.call_command( management.call_command(
'loaddata', 'loaddata',
'this_fixture_doesnt_exist', 'this_fixture_doesnt_exist',