From ae2a7da86bf841d42be86dea3effb0261187c950 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Tue, 17 May 2016 09:52:01 +0300 Subject: Fixed #20468 -- Added loaddata --exclude option. Thanks Alex Morozov for the initial patch. --- tests/fixtures/tests.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/fixtures/tests.py b/tests/fixtures/tests.py index b021d927f0..95240652f3 100644 --- a/tests/fixtures/tests.py +++ b/tests/fixtures/tests.py @@ -20,7 +20,7 @@ from django.test import ( from django.utils import six from django.utils.encoding import force_text -from .models import Article, ProxySpy, Spy, Tag, Visa +from .models import Article, Category, ProxySpy, Spy, Tag, Visa class TestCaseFixtureLoadingTests(TestCase): @@ -370,7 +370,7 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase): self._dumpdata_assert(['fixtures', 'sites'], '', exclude_list=['foo_app']) # Excluding a bogus model should throw an error - with self.assertRaisesMessage(management.CommandError, "Unknown model in excludes: fixtures.FooModel"): + with self.assertRaisesMessage(management.CommandError, "Unknown model: fixtures.FooModel"): self._dumpdata_assert(['fixtures', 'sites'], '', exclude_list=['fixtures.FooModel']) @unittest.skipIf(sys.platform.startswith('win'), "Windows doesn't support '?' in filenames.") @@ -650,6 +650,30 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase): format='xml', natural_foreign_keys=True ) + def test_loading_with_exclude_app(self): + Site.objects.all().delete() + management.call_command('loaddata', 'fixture1', exclude=['fixtures'], verbosity=0) + self.assertFalse(Article.objects.exists()) + self.assertFalse(Category.objects.exists()) + self.assertQuerysetEqual(Site.objects.all(), ['']) + + def test_loading_with_exclude_model(self): + Site.objects.all().delete() + management.call_command('loaddata', 'fixture1', exclude=['fixtures.Article'], verbosity=0) + self.assertFalse(Article.objects.exists()) + self.assertQuerysetEqual(Category.objects.all(), ['']) + self.assertQuerysetEqual(Site.objects.all(), ['']) + + def test_exclude_option_errors(self): + """Excluding a bogus app or model should raise an error.""" + msg = "No installed app with label 'foo_app'." + with self.assertRaisesMessage(management.CommandError, msg): + management.call_command('loaddata', 'fixture1', exclude=['foo_app'], verbosity=0) + + msg = "Unknown model: fixtures.FooModel" + with self.assertRaisesMessage(management.CommandError, msg): + management.call_command('loaddata', 'fixture1', exclude=['fixtures.FooModel'], verbosity=0) + class NonExistentFixtureTests(TestCase): """ -- cgit v1.3