Django

Code

root/django/trunk/tests/modeltests/fixtures/models.py

Revision 8325, 4.4 kB (checked in by gwilson, 2 weeks ago)

Fixed a couple typos in the modeltests' descriptions and made use of ReST inline literal markup for code snippets.

  • Property svn:eol-style set to native
Line 
1 """
2 37. Fixtures.
3
4 Fixtures are a way of loading data into the database in bulk. Fixure data
5 can be stored in any serializable format (including JSON and XML). Fixtures
6 are identified by name, and are stored in either a directory named 'fixtures'
7 in the application directory, on in one of the directories named in the
8 ``FIXTURE_DIRS`` setting.
9 """
10
11 from django.db import models
12 from django.conf import settings
13
14 class Article(models.Model):
15     headline = models.CharField(max_length=100, default='Default headline')
16     pub_date = models.DateTimeField()
17
18     def __unicode__(self):
19         return self.headline
20
21     class Meta:
22         ordering = ('-pub_date', 'headline')
23
24 __test__ = {'API_TESTS': """
25 >>> from django.core import management
26 >>> from django.db.models import get_app
27
28 # Reset the database representation of this app.
29 # This will return the database to a clean initial state.
30 >>> management.call_command('flush', verbosity=0, interactive=False)
31
32 # Syncdb introduces 1 initial data object from initial_data.json.
33 >>> Article.objects.all()
34 [<Article: Python program becomes self aware>]
35
36 # Load fixture 1. Single JSON file, with two objects.
37 >>> management.call_command('loaddata', 'fixture1.json', verbosity=0)
38 >>> Article.objects.all()
39 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
40
41 # Load fixture 2. JSON file imported by default. Overwrites some existing objects
42 >>> management.call_command('loaddata', 'fixture2.json', verbosity=0)
43 >>> Article.objects.all()
44 [<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>]
45
46 # Load fixture 3, XML format.
47 >>> management.call_command('loaddata', 'fixture3.xml', verbosity=0)
48 >>> Article.objects.all()
49 [<Article: XML identified as leading cause of cancer>, <Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker on TV is great!>, <Article: Python program becomes self aware>]
50
51 # Load a fixture that doesn't exist
52 >>> management.call_command('loaddata', 'unknown.json', verbosity=0)
53
54 # object list is unaffected
55 >>> Article.objects.all()
56 [<Article: XML identified as leading cause of cancer>, <Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker on TV is great!>, <Article: Python program becomes self aware>]
57 """}
58
59 # Database flushing does not work on MySQL with the default storage engine
60 # because it requires transaction support.
61 if settings.DATABASE_ENGINE != 'mysql':
62     __test__['API_TESTS'] += \
63 """
64 # Reset the database representation of this app. This will delete all data.
65 >>> management.call_command('flush', verbosity=0, interactive=False)
66 >>> Article.objects.all()
67 [<Article: Python program becomes self aware>]
68
69 # Load fixture 1 again, using format discovery
70 >>> management.call_command('loaddata', 'fixture1', verbosity=0)
71 >>> Article.objects.all()
72 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
73
74 # Try to load fixture 2 using format discovery; this will fail
75 # because there are two fixture2's in the fixtures directory
76 >>> management.call_command('loaddata', 'fixture2', verbosity=0) # doctest: +ELLIPSIS
77 Multiple fixtures named 'fixture2' in '...fixtures'. Aborting.
78
79 >>> Article.objects.all()
80 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
81
82 # Dump the current contents of the database as a JSON fixture
83 >>> management.call_command('dumpdata', 'fixtures', format='json')
84 [{"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
85 """
86
87 from django.test import TestCase
88
89 class SampleTestCase(TestCase):
90     fixtures = ['fixture1.json', 'fixture2.json']
91
92     def testClassFixtures(self):
93         "Check that test case has installed 4 fixture objects"
94         self.assertEqual(Article.objects.count(), 4)
95         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>]")
Note: See TracBrowser for help on using the browser.