diff options
author | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 09:53:47 +0100 |
---|---|---|
committer | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 14:36:57 +0100 |
commit | 89f40e36246100df6a11316c31a76712ebc6c501 (patch) | |
tree | 6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/fixtures_model_package | |
parent | b3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff) | |
download | django-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz |
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/fixtures_model_package')
-rw-r--r-- | tests/fixtures_model_package/__init__.py | 2 | ||||
-rw-r--r-- | tests/fixtures_model_package/fixtures/fixture1.json | 18 | ||||
-rw-r--r-- | tests/fixtures_model_package/fixtures/fixture2.json | 18 | ||||
-rw-r--r-- | tests/fixtures_model_package/fixtures/fixture2.xml | 11 | ||||
-rw-r--r-- | tests/fixtures_model_package/fixtures/initial_data.json | 9 | ||||
-rw-r--r-- | tests/fixtures_model_package/models/__init__.py | 21 | ||||
-rw-r--r-- | tests/fixtures_model_package/tests.py | 104 |
7 files changed, 183 insertions, 0 deletions
diff --git a/tests/fixtures_model_package/__init__.py b/tests/fixtures_model_package/__init__.py new file mode 100644 index 0000000000..139597f9cb --- /dev/null +++ b/tests/fixtures_model_package/__init__.py @@ -0,0 +1,2 @@ + + diff --git a/tests/fixtures_model_package/fixtures/fixture1.json b/tests/fixtures_model_package/fixtures/fixture1.json new file mode 100644 index 0000000000..7684d84609 --- /dev/null +++ b/tests/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/fixtures_model_package/fixtures/fixture2.json b/tests/fixtures_model_package/fixtures/fixture2.json new file mode 100644 index 0000000000..4997627385 --- /dev/null +++ b/tests/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/fixtures_model_package/fixtures/fixture2.xml b/tests/fixtures_model_package/fixtures/fixture2.xml new file mode 100644 index 0000000000..55337cf810 --- /dev/null +++ b/tests/fixtures_model_package/fixtures/fixture2.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="utf-8"?> +<django-objects version="1.0"> + <object pk="2" model="fixtures_model_package.article"> + <field type="CharField" name="headline">Poker on TV is great!</field> + <field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field> + </object> + <object pk="5" model="fixtures_model_package.article"> + <field type="CharField" name="headline">XML identified as leading cause of cancer</field> + <field type="DateTimeField" name="pub_date">2006-06-16 16:00:00</field> + </object> +</django-objects> diff --git a/tests/fixtures_model_package/fixtures/initial_data.json b/tests/fixtures_model_package/fixtures/initial_data.json new file mode 100644 index 0000000000..1d1cf15e03 --- /dev/null +++ b/tests/fixtures_model_package/fixtures/initial_data.json @@ -0,0 +1,9 @@ +[ + { + "pk": "10", + "model": "fixtures_model_package.book", + "fields": { + "name": "Achieving self-awareness of Python programs" + } + } +] diff --git a/tests/fixtures_model_package/models/__init__.py b/tests/fixtures_model_package/models/__init__.py new file mode 100644 index 0000000000..deeba48aa9 --- /dev/null +++ b/tests/fixtures_model_package/models/__init__.py @@ -0,0 +1,21 @@ +from django.db import models +from django.utils.encoding import python_2_unicode_compatible + + +@python_2_unicode_compatible +class Article(models.Model): + headline = models.CharField(max_length=100, default='Default headline') + pub_date = models.DateTimeField() + + def __str__(self): + return self.headline + + class Meta: + app_label = 'fixtures_model_package' + ordering = ('-pub_date', 'headline') + +class Book(models.Model): + name = models.CharField(max_length=100) + + class Meta: + ordering = ('name',) diff --git a/tests/fixtures_model_package/tests.py b/tests/fixtures_model_package/tests.py new file mode 100644 index 0000000000..d147fe68a7 --- /dev/null +++ b/tests/fixtures_model_package/tests.py @@ -0,0 +1,104 @@ +from __future__ import unicode_literals + +from django.core import management +from django.db import transaction +from django.test import TestCase, TransactionTestCase + +from .models import Article, Book + + +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(), 3) + self.assertQuerysetEqual( + Article.objects.all(),[ + "Django conquers world!", + "Copyright is fine the way it is", + "Poker has no place on ESPN", + ], + lambda a: a.headline + ) + + +class TestNoInitialDataLoading(TransactionTestCase): + def test_syncdb(self): + with transaction.commit_manually(): + Book.objects.all().delete() + + management.call_command( + 'syncdb', + verbosity=0, + load_initial_data=False + ) + self.assertQuerysetEqual(Book.objects.all(), []) + transaction.rollback() + + def test_flush(self): + # Test presence of fixture (flush called by TransactionTestCase) + self.assertQuerysetEqual( + Book.objects.all(), [ + 'Achieving self-awareness of Python programs' + ], + lambda a: a.name + ) + + with transaction.commit_manually(): + management.call_command( + 'flush', + verbosity=0, + interactive=False, + commit=False, + load_initial_data=False + ) + self.assertQuerysetEqual(Book.objects.all(), []) + transaction.rollback() + + +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( + Book.objects.all(), [ + 'Achieving self-awareness of Python programs' + ], + lambda a: a.name + ) + + 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, commit=False) + self.assertQuerysetEqual( + Article.objects.all(), [ + "Time to reform copyright", + "Poker has no place on ESPN", + ], + lambda a: a.headline, + ) + + # Load fixture 2. JSON file imported by default. Overwrites some + # existing objects + management.call_command("loaddata", "fixture2.json", verbosity=0, commit=False) + self.assertQuerysetEqual( + Article.objects.all(), [ + "Django conquers world!", + "Copyright is fine the way it is", + "Poker has no place on ESPN", + ], + lambda a: a.headline, + ) + + # Load a fixture that doesn't exist + management.call_command("loaddata", "unknown.json", verbosity=0, commit=False) + self.assertQuerysetEqual( + Article.objects.all(), [ + "Django conquers world!", + "Copyright is fine the way it is", + "Poker has no place on ESPN", + ], + lambda a: a.headline, + ) |