From 10de06b6d416fc1dbaa48d991b258bfc9dca791a Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 23 Aug 2010 07:04:41 +0000 Subject: [PATCH] Fixed #14155 -- Refactored another doctest suite and call to flush. Thanks to Alex Gaynor for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13622 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../fixtures_model_package/models/__init__.py | 40 ----------- .../fixtures_model_package/tests.py | 71 +++++++++++++++++++ 2 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 tests/modeltests/fixtures_model_package/tests.py diff --git a/tests/modeltests/fixtures_model_package/models/__init__.py b/tests/modeltests/fixtures_model_package/models/__init__.py index 1581102b88..c0450b27bf 100644 --- a/tests/modeltests/fixtures_model_package/models/__init__.py +++ b/tests/modeltests/fixtures_model_package/models/__init__.py @@ -12,43 +12,3 @@ class Article(models.Model): 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()), "[, , , ]") diff --git a/tests/modeltests/fixtures_model_package/tests.py b/tests/modeltests/fixtures_model_package/tests.py new file mode 100644 index 0000000000..328f8b8498 --- /dev/null +++ b/tests/modeltests/fixtures_model_package/tests.py @@ -0,0 +1,71 @@ +from django.core import management +from django.test import TestCase + +from models import Article + + +class SampleTestCase(TestCase): + fixtures = ['fixture1.json', 'fixture2.json'] + + def testClassFixtures(self): + "Test cases can load fixture objects into models defined in packages" + self.assertEqual(Article.objects.count(), 4) + self.assertQuerysetEqual( + Article.objects.all(),[ + "Django conquers world!", + "Copyright is fine the way it is", + "Poker has no place on ESPN", + "Python program becomes self aware" + ], + lambda a: a.headline + ) + + +class FixtureTestCase(TestCase): + def test_initial_data(self): + "Fixtures can load initial data into models defined in packages" + #Syncdb introduces 1 initial data object from initial_data.json + self.assertQuerysetEqual( + Article.objects.all(), [ + "Python program becomes self aware" + ], + lambda a: a.headline + ) + + def test_loaddata(self): + "Fixtures can load data into models defined in packages" + # Load fixture 1. Single JSON file, with two objects + management.call_command("loaddata", "fixture1.json", verbosity=0) + self.assertQuerysetEqual( + Article.objects.all(), [ + "Time to reform copyright", + "Poker has no place on ESPN", + "Python program becomes self aware", + ], + lambda a: a.headline, + ) + + # Load fixture 2. JSON file imported by default. Overwrites some + # existing objects + management.call_command("loaddata", "fixture2.json", verbosity=0) + self.assertQuerysetEqual( + Article.objects.all(), [ + "Django conquers world!", + "Copyright is fine the way it is", + "Poker has no place on ESPN", + "Python program becomes self aware", + ], + lambda a: a.headline, + ) + + # Load a fixture that doesn't exist + management.call_command("loaddata", "unknown.json", verbosity=0) + self.assertQuerysetEqual( + Article.objects.all(), [ + "Django conquers world!", + "Copyright is fine the way it is", + "Poker has no place on ESPN", + "Python program becomes self aware", + ], + lambda a: a.headline, + )