Fixed #21549 -- Made loaddata's 'fixture not found' warning an exception.
Thanks to mpasternak for the report and Tim Graham for the review.
This commit is contained in:
parent
2c6c873e3f
commit
d5b90c8e12
|
@ -239,8 +239,7 @@ class Command(BaseCommand):
|
||||||
fixture_files.extend(fixture_files_in_dir)
|
fixture_files.extend(fixture_files_in_dir)
|
||||||
|
|
||||||
if not fixture_files:
|
if not fixture_files:
|
||||||
# Warning kept for backwards-compatibility; why not an exception?
|
raise CommandError("No fixture named '%s' found." % fixture_name)
|
||||||
warnings.warn("No fixture named '%s' found." % fixture_name)
|
|
||||||
|
|
||||||
return fixture_files
|
return fixture_files
|
||||||
|
|
||||||
|
|
|
@ -408,6 +408,9 @@ Miscellaneous
|
||||||
* Support for ``skip_validation`` in ``BaseCommand.execute(**options)`` is
|
* Support for ``skip_validation`` in ``BaseCommand.execute(**options)`` is
|
||||||
removed. Use ``skip_checks`` (added in Django 1.7) instead.
|
removed. Use ``skip_checks`` (added in Django 1.7) instead.
|
||||||
|
|
||||||
|
* :djadmin:`loaddata` now raises a ``CommandError`` instead of showing a
|
||||||
|
warning when the specified fixture file is not found.
|
||||||
|
|
||||||
.. _deprecated-features-1.10:
|
.. _deprecated-features-1.10:
|
||||||
|
|
||||||
Features deprecated in 1.10
|
Features deprecated in 1.10
|
||||||
|
|
|
@ -4,16 +4,16 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
import warnings
|
|
||||||
|
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
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.core.files.temp import NamedTemporaryFile
|
from django.core.files.temp import NamedTemporaryFile
|
||||||
|
from django.core.management import CommandError
|
||||||
from django.core.serializers.base import ProgressBar
|
from django.core.serializers.base import ProgressBar
|
||||||
from django.db import IntegrityError, connection
|
from django.db import IntegrityError, connection
|
||||||
from django.test import (
|
from django.test import (
|
||||||
TestCase, TransactionTestCase, ignore_warnings, mock, skipUnlessDBFeature,
|
TestCase, TransactionTestCase, mock, skipUnlessDBFeature,
|
||||||
)
|
)
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
|
@ -532,11 +532,11 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase):
|
||||||
management.call_command('loaddata', 'invalid.json', verbosity=0)
|
management.call_command('loaddata', 'invalid.json', verbosity=0)
|
||||||
self.assertIn("Could not load fixtures.Article(pk=1):", cm.exception.args[0])
|
self.assertIn("Could not load fixtures.Article(pk=1):", cm.exception.args[0])
|
||||||
|
|
||||||
@ignore_warnings(category=UserWarning, message="No fixture named")
|
|
||||||
def test_loaddata_app_option(self):
|
def test_loaddata_app_option(self):
|
||||||
"""
|
"""
|
||||||
Verifies that the --app option works.
|
Verifies that the --app option works.
|
||||||
"""
|
"""
|
||||||
|
with self.assertRaisesMessage(CommandError, "No fixture named 'db_fixture_1' found."):
|
||||||
management.call_command('loaddata', 'db_fixture_1', verbosity=0, app_label="someotherapp")
|
management.call_command('loaddata', 'db_fixture_1', verbosity=0, app_label="someotherapp")
|
||||||
self.assertQuerysetEqual(Article.objects.all(), [])
|
self.assertQuerysetEqual(Article.objects.all(), [])
|
||||||
management.call_command('loaddata', 'db_fixture_1', verbosity=0, app_label="fixtures")
|
management.call_command('loaddata', 'db_fixture_1', verbosity=0, app_label="fixtures")
|
||||||
|
@ -563,10 +563,11 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase):
|
||||||
'<Article: Who needs to use compressed data?>',
|
'<Article: Who needs to use compressed data?>',
|
||||||
])
|
])
|
||||||
|
|
||||||
@ignore_warnings(category=UserWarning, message="No fixture named")
|
|
||||||
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 self.assertRaisesMessage(CommandError, "No fixture named 'db_fixture_3' found."):
|
||||||
management.call_command('loaddata', 'db_fixture_3', verbosity=0)
|
management.call_command('loaddata', 'db_fixture_3', verbosity=0)
|
||||||
|
with self.assertRaisesMessage(CommandError, "No fixture named 'db_fixture_3' found."):
|
||||||
management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default')
|
management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default')
|
||||||
self.assertQuerysetEqual(Article.objects.all(), [])
|
self.assertQuerysetEqual(Article.objects.all(), [])
|
||||||
|
|
||||||
|
@ -628,20 +629,8 @@ class NonExistentFixtureTests(TestCase):
|
||||||
|
|
||||||
def test_loaddata_not_existent_fixture_file(self):
|
def test_loaddata_not_existent_fixture_file(self):
|
||||||
stdout_output = six.StringIO()
|
stdout_output = six.StringIO()
|
||||||
with warnings.catch_warnings(record=True) as w:
|
with self.assertRaisesMessage(CommandError, "No fixture named 'this_fixture_doesnt_exist' found."):
|
||||||
warnings.simplefilter("always")
|
management.call_command('loaddata', 'this_fixture_doesnt_exist', stdout=stdout_output)
|
||||||
# With verbosity=2, we get both stdout output and a warning
|
|
||||||
management.call_command(
|
|
||||||
'loaddata',
|
|
||||||
'this_fixture_doesnt_exist',
|
|
||||||
verbosity=2,
|
|
||||||
stdout=stdout_output,
|
|
||||||
)
|
|
||||||
self.assertIn("No fixture 'this_fixture_doesnt_exist' in",
|
|
||||||
force_text(stdout_output.getvalue()))
|
|
||||||
self.assertEqual(len(w), 1)
|
|
||||||
self.assertEqual(force_text(w[0].message),
|
|
||||||
"No fixture named 'this_fixture_doesnt_exist' found.")
|
|
||||||
|
|
||||||
@mock.patch('django.db.connection.enable_constraint_checking')
|
@mock.patch('django.db.connection.enable_constraint_checking')
|
||||||
@mock.patch('django.db.connection.disable_constraint_checking')
|
@mock.patch('django.db.connection.disable_constraint_checking')
|
||||||
|
@ -651,6 +640,7 @@ class NonExistentFixtureTests(TestCase):
|
||||||
If no fixtures match the loaddata command, constraints checks on the
|
If no fixtures match the loaddata command, constraints checks on the
|
||||||
database shouldn't be disabled. This is performance critical on MSSQL.
|
database shouldn't be disabled. This is performance critical on MSSQL.
|
||||||
"""
|
"""
|
||||||
|
with self.assertRaisesMessage(CommandError, "No fixture named 'this_fixture_doesnt_exist' found."):
|
||||||
management.call_command('loaddata', 'this_fixture_doesnt_exist', verbosity=0)
|
management.call_command('loaddata', 'this_fixture_doesnt_exist', verbosity=0)
|
||||||
disable_constraint_checking.assert_not_called()
|
disable_constraint_checking.assert_not_called()
|
||||||
enable_constraint_checking.assert_not_called()
|
enable_constraint_checking.assert_not_called()
|
||||||
|
|
|
@ -1,8 +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.core.management import CommandError
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from .models import Article
|
from .models import Article
|
||||||
|
@ -51,11 +50,8 @@ class FixtureTestCase(TestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Load a fixture that doesn't exist
|
# Load a fixture that doesn't exist
|
||||||
with warnings.catch_warnings(record=True) as w:
|
with self.assertRaisesMessage(CommandError, "No fixture named 'unknown' found."):
|
||||||
warnings.simplefilter("always")
|
|
||||||
management.call_command("loaddata", "unknown.json", verbosity=0)
|
management.call_command("loaddata", "unknown.json", verbosity=0)
|
||||||
self.assertEqual(len(w), 1)
|
|
||||||
self.assertTrue(w[0].message, "No fixture named 'unknown' found.")
|
|
||||||
|
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
Article.objects.all(), [
|
Article.objects.all(), [
|
||||||
|
|
Loading…
Reference in New Issue