Commit d5b90c8e authored by Andrew Kuchev's avatar Andrew Kuchev Committed by Tim Graham
Browse files

Fixed #21549 -- Made loaddata's 'fixture not found' warning an exception.

Thanks to mpasternak for the report and Tim Graham for the review.
parent 2c6c873e
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -239,8 +239,7 @@ class Command(BaseCommand):
            fixture_files.extend(fixture_files_in_dir)

        if not fixture_files:
            # Warning kept for backwards-compatibility; why not an exception?
            warnings.warn("No fixture named '%s' found." % fixture_name)
            raise CommandError("No fixture named '%s' found." % fixture_name)

        return fixture_files

+3 −0
Original line number Diff line number Diff line
@@ -408,6 +408,9 @@ Miscellaneous
* Support for ``skip_validation`` in ``BaseCommand.execute(**options)`` is
  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:

Features deprecated in 1.10
+12 −22
Original line number Diff line number Diff line
@@ -4,16 +4,16 @@ import os
import sys
import tempfile
import unittest
import warnings

from django.apps import apps
from django.contrib.sites.models import Site
from django.core import management
from django.core.files.temp import NamedTemporaryFile
from django.core.management import CommandError
from django.core.serializers.base import ProgressBar
from django.db import IntegrityError, connection
from django.test import (
    TestCase, TransactionTestCase, ignore_warnings, mock, skipUnlessDBFeature,
    TestCase, TransactionTestCase, mock, skipUnlessDBFeature,
)
from django.utils import six
from django.utils.encoding import force_text
@@ -532,11 +532,11 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase):
            management.call_command('loaddata', 'invalid.json', verbosity=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):
        """
        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")
        self.assertQuerysetEqual(Article.objects.all(), [])
        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?>',
        ])

    @ignore_warnings(category=UserWarning, message="No fixture named")
    def test_unmatched_identifier_loading(self):
        # 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)
        with self.assertRaisesMessage(CommandError, "No fixture named 'db_fixture_3' found."):
            management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default')
        self.assertQuerysetEqual(Article.objects.all(), [])

@@ -628,20 +629,8 @@ class NonExistentFixtureTests(TestCase):

    def test_loaddata_not_existent_fixture_file(self):
        stdout_output = six.StringIO()
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            # 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.")
        with self.assertRaisesMessage(CommandError, "No fixture named 'this_fixture_doesnt_exist' found."):
            management.call_command('loaddata', 'this_fixture_doesnt_exist', stdout=stdout_output)

    @mock.patch('django.db.connection.enable_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
        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)
        disable_constraint_checking.assert_not_called()
        enable_constraint_checking.assert_not_called()
+2 −6
Original line number Diff line number Diff line
from __future__ import unicode_literals

import warnings

from django.core import management
from django.core.management import CommandError
from django.test import TestCase

from .models import Article
@@ -51,11 +50,8 @@ class FixtureTestCase(TestCase):
        )

        # Load a fixture that doesn't exist
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
        with self.assertRaisesMessage(CommandError, "No fixture named 'unknown' found."):
            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(
            Article.objects.all(), [