diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index e70f85b9a6..817ffaa83e 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -76,7 +76,17 @@ class Command(BaseCommand): if has_bz2: compression_types['bz2'] = bz2.BZ2File - app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()] + app_module_paths = [] + for app in get_apps(): + if hasattr(app, '__path__'): + # It's a 'models/' subpackage + for path in app.__path__: + app_module_paths.append(path) + else: + # It's a models.py module + app_module_paths.append(app.__file__) + + app_fixtures = [os.path.join(os.path.dirname(path), 'fixtures') for path in app_module_paths] for fixture_label in fixture_labels: parts = fixture_label.split('.') diff --git a/tests/modeltests/fixtures_model_package/__init__.py b/tests/modeltests/fixtures_model_package/__init__.py new file mode 100644 index 0000000000..139597f9cb --- /dev/null +++ b/tests/modeltests/fixtures_model_package/__init__.py @@ -0,0 +1,2 @@ + + diff --git a/tests/modeltests/fixtures_model_package/fixtures/fixture1.json b/tests/modeltests/fixtures_model_package/fixtures/fixture1.json new file mode 100644 index 0000000000..7684d84609 --- /dev/null +++ b/tests/modeltests/fixtures_model_package/fixtures/fixture1.json @@ -0,0 +1,18 @@ +[ + { + "pk": "2", + "model": "fixtures_model_package.article", + "fields": { + "headline": "Poker has no place on ESPN", + "pub_date": "2006-06-16 12:00:00" + } + }, + { + "pk": "3", + "model": "fixtures_model_package.article", + "fields": { + "headline": "Time to reform copyright", + "pub_date": "2006-06-16 13:00:00" + } + } +] diff --git a/tests/modeltests/fixtures_model_package/fixtures/fixture2.json b/tests/modeltests/fixtures_model_package/fixtures/fixture2.json new file mode 100644 index 0000000000..4997627385 --- /dev/null +++ b/tests/modeltests/fixtures_model_package/fixtures/fixture2.json @@ -0,0 +1,18 @@ +[ + { + "pk": "3", + "model": "fixtures_model_package.article", + "fields": { + "headline": "Copyright is fine the way it is", + "pub_date": "2006-06-16 14:00:00" + } + }, + { + "pk": "4", + "model": "fixtures_model_package.article", + "fields": { + "headline": "Django conquers world!", + "pub_date": "2006-06-16 15:00:00" + } + } +] diff --git a/tests/modeltests/fixtures_model_package/fixtures/fixture2.xml b/tests/modeltests/fixtures_model_package/fixtures/fixture2.xml new file mode 100644 index 0000000000..55337cf810 --- /dev/null +++ b/tests/modeltests/fixtures_model_package/fixtures/fixture2.xml @@ -0,0 +1,11 @@ + + + + Poker on TV is great! + 2006-06-16 11:00:00 + + + XML identified as leading cause of cancer + 2006-06-16 16:00:00 + + diff --git a/tests/modeltests/fixtures_model_package/fixtures/initial_data.json b/tests/modeltests/fixtures_model_package/fixtures/initial_data.json new file mode 100644 index 0000000000..66cb5d7b87 --- /dev/null +++ b/tests/modeltests/fixtures_model_package/fixtures/initial_data.json @@ -0,0 +1,10 @@ +[ + { + "pk": "1", + "model": "fixtures_model_package.article", + "fields": { + "headline": "Python program becomes self aware", + "pub_date": "2006-06-16 11:00:00" + } + } +] diff --git a/tests/modeltests/fixtures_model_package/models/__init__.py b/tests/modeltests/fixtures_model_package/models/__init__.py new file mode 100644 index 0000000000..1581102b88 --- /dev/null +++ b/tests/modeltests/fixtures_model_package/models/__init__.py @@ -0,0 +1,54 @@ +from django.db import models +from django.conf import settings + +class Article(models.Model): + headline = models.CharField(max_length=100, default='Default headline') + pub_date = models.DateTimeField() + + def __unicode__(self): + return self.headline + + class Meta: + app_label = 'fixtures_model_package' + ordering = ('-pub_date', 'headline') + +__test__ = {'API_TESTS': """ +>>> from django.core import management +>>> from django.db.models import get_app + +# Reset the database representation of this app. +# This will return the database to a clean initial state. +>>> management.call_command('flush', verbosity=0, interactive=False) + +# Syncdb introduces 1 initial data object from initial_data.json. +>>> Article.objects.all() +[] + +# Load fixture 1. Single JSON file, with two objects. +>>> management.call_command('loaddata', 'fixture1.json', verbosity=0) +>>> Article.objects.all() +[, , ] + +# Load fixture 2. JSON file imported by default. Overwrites some existing objects +>>> management.call_command('loaddata', 'fixture2.json', verbosity=0) +>>> Article.objects.all() +[, , , ] + +# Load a fixture that doesn't exist +>>> management.call_command('loaddata', 'unknown.json', verbosity=0) + +# object list is unaffected +>>> Article.objects.all() +[, , , ] +"""} + + +from django.test import TestCase + +class SampleTestCase(TestCase): + fixtures = ['fixture1.json', 'fixture2.json'] + + def testClassFixtures(self): + "Check that test case has installed 4 fixture objects" + self.assertEqual(Article.objects.count(), 4) + self.assertEquals(str(Article.objects.all()), "[, , , ]")