From 382c53d7d88da70a3645c6db063995382f61dc45 Mon Sep 17 00:00:00 2001 From: Przemek Lewandowski Date: Sat, 23 Feb 2013 20:34:59 +0100 Subject: [PATCH] Fixed #18213 -- Allowed empty fixtures (emit a warning rather than raising an exception). --- django/core/management/commands/loaddata.py | 12 ++++--- docs/releases/1.7.txt | 3 ++ tests/fixtures_regress/tests.py | 39 ++++++++++++++------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index aa879f6acc..e7aebf6d2a 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import glob import gzip import os +import warnings import zipfile from optparse import make_option import warnings @@ -156,12 +157,13 @@ class Command(BaseCommand): finally: fixture.close() - # If the fixture we loaded contains 0 objects, assume that an - # error was encountered during fixture loading. + # Warn if the fixture we loaded contains 0 objects. if objects_in_fixture == 0: - raise CommandError( - "No fixture data found for '%s'. " - "(File format may be invalid.)" % fixture_name) + warnings.warn( + "No fixture data found for '%s'. (File format may be " + "invalid.)" % fixture_name, + RuntimeWarning + ) def _find_fixtures(self, fixture_label): """ diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index 031c165302..6837af9a59 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -113,6 +113,9 @@ Miscellaneous :attr:`~django.forms.formsets.BaseFormSet.can_delete` for instructions on how to manually delete objects from deleted forms. +* Loading empty fixtures emits a ``RuntimeWarning`` rather than raising + :class:`~django.core.management.CommandError`. + Features deprecated in 1.7 ========================== diff --git a/tests/fixtures_regress/tests.py b/tests/fixtures_regress/tests.py index a6bad5716d..9899bf939c 100644 --- a/tests/fixtures_regress/tests.py +++ b/tests/fixtures_regress/tests.py @@ -181,55 +181,68 @@ class TestFixtures(TestCase): """ Test for ticket #4371 -- Loading a fixture file with invalid data using explicit filename. - Validate that error conditions are caught correctly + Test for ticket #18213 -- warning conditions are caught correctly """ - with six.assertRaisesRegex(self, management.CommandError, - "No fixture data found for 'bad_fixture2'. \(File format may be invalid.\)"): + with warnings.catch_warnings(record=True) as warning_list: + warnings.simplefilter("always") management.call_command( 'loaddata', 'bad_fixture2.xml', verbosity=0, ) + warning = warning_list.pop() + self.assertEqual(warning.category, RuntimeWarning) + self.assertEqual(str(warning.message), "No fixture data found for 'bad_fixture2'. (File format may be invalid.)") def test_invalid_data_no_ext(self): """ Test for ticket #4371 -- Loading a fixture file with invalid data without file extension. - Validate that error conditions are caught correctly + Test for ticket #18213 -- warning conditions are caught correctly """ - with six.assertRaisesRegex(self, management.CommandError, - "No fixture data found for 'bad_fixture2'. \(File format may be invalid.\)"): + with warnings.catch_warnings(record=True) as warning_list: + warnings.simplefilter("always") management.call_command( 'loaddata', 'bad_fixture2', verbosity=0, ) + warning = warning_list.pop() + self.assertEqual(warning.category, RuntimeWarning) + self.assertEqual(str(warning.message), "No fixture data found for 'bad_fixture2'. (File format may be invalid.)") def test_empty(self): """ - Test for ticket #4371 -- Loading a fixture file with no data returns an error. - Validate that error conditions are caught correctly + Test for ticket #18213 -- Loading a fixture file with no data output a warning. + Previously empty fixture raises an error exception, see ticket #4371. """ - with six.assertRaisesRegex(self, management.CommandError, - "No fixture data found for 'empty'. \(File format may be invalid.\)"): + with warnings.catch_warnings(record=True) as warning_list: + warnings.simplefilter("always") management.call_command( 'loaddata', 'empty', verbosity=0, ) + warning = warning_list.pop() + self.assertEqual(warning.category, RuntimeWarning) + self.assertEqual(str(warning.message), "No fixture data found for 'empty'. (File format may be invalid.)") def test_error_message(self): """ - (Regression for #9011 - error message is correct) + Regression for #9011 - error message is correct. + Change from error to warning for ticket #18213. """ - with six.assertRaisesRegex(self, management.CommandError, - "^No fixture data found for 'bad_fixture2'. \(File format may be invalid.\)$"): + with warnings.catch_warnings(record=True) as warning_list: + warnings.simplefilter("always") management.call_command( 'loaddata', 'bad_fixture2', 'animal', verbosity=0, ) + warning = warning_list.pop() + self.assertEqual(warning.category, RuntimeWarning) + self.assertEqual(str(warning.message), "No fixture data found for 'bad_fixture2'. (File format may be invalid.)") def test_pg_sequence_resetting_checks(self): """