| 1 | from django.db import models |
| 2 | from django.conf import settings |
| 3 | |
| 4 | class Article(models.Model): |
| 5 | headline = models.CharField(max_length=100, default='Default headline') |
| 6 | pub_date = models.DateTimeField() |
| 7 | |
| 8 | def __unicode__(self): |
| 9 | return self.headline |
| 10 | |
| 11 | class Meta: |
| 12 | app_label = 'fixtures_model_package' |
| 13 | ordering = ('-pub_date', 'headline') |
| 14 | |
| 15 | __test__ = {'API_TESTS': """ |
| 16 | >>> from django.core import management |
| 17 | >>> from django.db.models import get_app |
| 18 | |
| 19 | # Reset the database representation of this app. |
| 20 | # This will return the database to a clean initial state. |
| 21 | >>> management.call_command('flush', verbosity=0, interactive=False) |
| 22 | |
| 23 | # Syncdb introduces 1 initial data object from initial_data.json. |
| 24 | >>> Article.objects.all() |
| 25 | [<Article: Python program becomes self aware>] |
| 26 | |
| 27 | # Load fixture 1. Single JSON file, with two objects. |
| 28 | >>> management.call_command('loaddata', 'fixture1.json', verbosity=0) |
| 29 | >>> Article.objects.all() |
| 30 | [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] |
| 31 | |
| 32 | # Load fixture 2. JSON file imported by default. Overwrites some existing objects |
| 33 | >>> management.call_command('loaddata', 'fixture2.json', verbosity=0) |
| 34 | >>> Article.objects.all() |
| 35 | [<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] |
| 36 | |
| 37 | # Load a fixture that doesn't exist |
| 38 | >>> management.call_command('loaddata', 'unknown.json', verbosity=0) |
| 39 | |
| 40 | # object list is unaffected |
| 41 | >>> Article.objects.all() |
| 42 | [<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] |
| 43 | """} |
| 44 | |
| 45 | |
| 46 | from django.test import TestCase |
| 47 | |
| 48 | class SampleTestCase(TestCase): |
| 49 | fixtures = ['fixture1.json', 'fixture2.json'] |
| 50 | |
| 51 | def testClassFixtures(self): |
| 52 | "Check that test case has installed 4 fixture objects" |
| 53 | self.assertEqual(Article.objects.count(), 4) |
| 54 | self.assertEquals(str(Article.objects.all()), "[<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]") |