Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1134 from senko/ticket_18990

Fixed #18990: Loaddata now complains if fixture doesn't exist
  • Loading branch information...
commit 7a99d1e167f81c5fcd1b9f7f548c5d6959cef2ef 2 parents cc62cbe + 65c5571
Andrew Godwin authored May 19, 2013
12  django/core/management/commands/loaddata.py
@@ -4,6 +4,7 @@
4 4
 import gzip
5 5
 import zipfile
6 6
 from optparse import make_option
  7
+import warnings
7 8
 
8 9
 from django.conf import settings
9 10
 from django.core import serializers
@@ -162,9 +163,14 @@ def load_label(self, fixture_label, app_fixtures):
162 163
         else:
163 164
             fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + ['']
164 165
 
  166
+        label_found = False
165 167
         for fixture_dir in fixture_dirs:
166  
-            self.process_dir(fixture_dir, fixture_name, compression_formats,
167  
-                             formats)
  168
+            found = self.process_dir(fixture_dir, fixture_name,
  169
+                compression_formats, formats)
  170
+            label_found = label_found or found
  171
+
  172
+        if fixture_name != 'initial_data' and not label_found:
  173
+            warnings.warn("No fixture named '%s' found." % fixture_name)
168 174
 
169 175
     def process_dir(self, fixture_dir, fixture_name, compression_formats,
170 176
                     serialization_formats):
@@ -242,3 +248,5 @@ def process_dir(self, fixture_dir, fixture_name, compression_formats,
242 248
                     raise CommandError(
243 249
                         "No fixture data found for '%s'. (File format may be invalid.)" %
244 250
                             (fixture_name))
  251
+
  252
+        return label_found
23  tests/fixtures/tests.py
... ...
@@ -1,5 +1,7 @@
1 1
 from __future__ import absolute_import
2 2
 
  3
+import warnings
  4
+
3 5
 from django.contrib.sites.models import Site
4 6
 from django.core import management
5 7
 from django.db import connection, IntegrityError
@@ -137,8 +139,18 @@ def test_loading_and_dumping(self):
137 139
             '<Book: Music for all ages by Artist formerly known as "Prince" and Django Reinhardt>'
138 140
         ])
139 141
 
140  
-        # Load a fixture that doesn't exist
141  
-        management.call_command('loaddata', 'unknown.json', verbosity=0, commit=False)
  142
+        # Loading a fixture that doesn't exist emits a warning
  143
+        with warnings.catch_warnings(record=True) as w:
  144
+            management.call_command('loaddata', 'unknown.json', verbosity=0,
  145
+                commit=False)
  146
+        self.assertEqual(len(w), 1)
  147
+        self.assertTrue(w[0].message, "No fixture named 'unknown' found.")
  148
+
  149
+        # An attempt to load a nonexistent 'initial_data' fixture isn't an error
  150
+        with warnings.catch_warnings(record=True) as w:
  151
+            management.call_command('loaddata', 'initial_data.json', verbosity=0,
  152
+                commit=False)
  153
+        self.assertEqual(len(w), 0)
142 154
 
143 155
         # object list is unaffected
144 156
         self.assertQuerysetEqual(Article.objects.all(), [
@@ -273,10 +285,11 @@ def test_loading_using(self):
273 285
 
274 286
     def test_unmatched_identifier_loading(self):
275 287
         # Try to load db fixture 3. This won't load because the database identifier doesn't match
276  
-        management.call_command('loaddata', 'db_fixture_3', verbosity=0, commit=False)
277  
-        self.assertQuerysetEqual(Article.objects.all(), [])
  288
+        with warnings.catch_warnings(record=True):
  289
+            management.call_command('loaddata', 'db_fixture_3', verbosity=0, commit=False)
278 290
 
279  
-        management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default', commit=False)
  291
+        with warnings.catch_warnings(record=True):
  292
+            management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default', commit=False)
280 293
         self.assertQuerysetEqual(Article.objects.all(), [])
281 294
 
282 295
     def test_output_formats(self):
6  tests/fixtures_model_package/tests.py
... ...
@@ -1,5 +1,7 @@
1 1
 from __future__ import unicode_literals
2 2
 
  3
+import warnings
  4
+
3 5
 from django.core import management
4 6
 from django.db import transaction
5 7
 from django.test import TestCase, TransactionTestCase
@@ -100,7 +102,9 @@ def test_loaddata(self):
100 102
         )
101 103
 
102 104
         # Load a fixture that doesn't exist
103  
-        management.call_command("loaddata", "unknown.json", verbosity=0, commit=False)
  105
+        with warnings.catch_warnings(record=True):
  106
+            management.call_command("loaddata", "unknown.json", verbosity=0, commit=False)
  107
+
104 108
         self.assertQuerysetEqual(
105 109
             Article.objects.all(), [
106 110
                 "Django conquers world!",
16  tests/fixtures_regress/tests.py
@@ -4,6 +4,7 @@
4 4
 
5 5
 import os
6 6
 import re
  7
+import warnings
7 8
 
8 9
 from django.core.serializers.base import DeserializationError
9 10
 from django.core import management
@@ -441,13 +442,14 @@ def test_loaddata_no_fixture_specified(self):
441 442
 
442 443
     def test_loaddata_not_existant_fixture_file(self):
443 444
         stdout_output = StringIO()
444  
-        management.call_command(
445  
-            'loaddata',
446  
-            'this_fixture_doesnt_exist',
447  
-            verbosity=2,
448  
-            commit=False,
449  
-            stdout=stdout_output,
450  
-        )
  445
+        with warnings.catch_warnings(record=True):
  446
+            management.call_command(
  447
+                'loaddata',
  448
+                'this_fixture_doesnt_exist',
  449
+                verbosity=2,
  450
+                commit=False,
  451
+                stdout=stdout_output,
  452
+            )
451 453
         self.assertTrue("No xml fixture 'this_fixture_doesnt_exist' in" in
452 454
             force_text(stdout_output.getvalue()))
453 455
 

0 notes on commit 7a99d1e

Please sign in to comment.
Something went wrong with that request. Please try again.