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 |
|
13 class Article(models.Model): |
|
14 headline = models.CharField(maxlength=100, default='Default headline') |
|
15 pub_date = models.DateTimeField() |
|
16 |
|
17 def __str__(self): |
|
18 return self.headline |
|
19 |
|
20 class Meta: |
|
21 ordering = ('-pub_date', 'headline') |
|
22 |
|
23 __test__ = {'API_TESTS': """ |
|
24 >>> from django.core import management |
|
25 >>> from django.db.models import get_app |
|
26 |
|
27 # Reset the database representation of this app. |
|
28 # This will return the database to a clean initial state. |
|
29 >>> management.flush(verbosity=0, interactive=False) |
|
30 |
|
31 # Syncdb introduces 1 initial data object from initial_data.json. |
|
32 >>> Article.objects.all() |
|
33 [<Article: Python program becomes self aware>] |
|
34 |
|
35 # Load fixture 1. Single JSON file, with two objects. |
|
36 >>> management.load_data(['fixture1.json'], verbosity=0) |
|
37 >>> Article.objects.all() |
|
38 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] |
|
39 |
|
40 # Load fixture 2. JSON file imported by default. Overwrites some existing objects |
|
41 >>> management.load_data(['fixture2.json'], verbosity=0) |
|
42 >>> Article.objects.all() |
|
43 [<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>] |
|
44 |
|
45 # Load fixture 3, XML format. |
|
46 >>> management.load_data(['fixture3.xml'], verbosity=0) |
|
47 >>> Article.objects.all() |
|
48 [<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>] |
|
49 |
|
50 # Load a fixture that doesn't exist |
|
51 >>> management.load_data(['unknown.json'], verbosity=0) |
|
52 |
|
53 # object list is unaffected |
|
54 >>> Article.objects.all() |
|
55 [<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>] |
|
56 |
|
57 # Reset the database representation of this app. This will delete all data. |
|
58 >>> management.flush(verbosity=0, interactive=False) |
|
59 >>> Article.objects.all() |
|
60 [<Article: Python program becomes self aware>] |
|
61 |
|
62 # Load fixture 1 again, using format discovery |
|
63 >>> management.load_data(['fixture1'], verbosity=0) |
|
64 >>> Article.objects.all() |
|
65 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] |
|
66 |
|
67 # Try to load fixture 2 using format discovery; this will fail |
|
68 # because there are two fixture2's in the fixtures directory |
|
69 >>> management.load_data(['fixture2'], verbosity=0) # doctest: +ELLIPSIS |
|
70 Multiple fixtures named 'fixture2' in '...fixtures'. Aborting. |
|
71 |
|
72 >>> Article.objects.all() |
|
73 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] |
|
74 |
|
75 # Dump the current contents of the database as a JSON fixture |
|
76 >>> print management.dump_data(['fixtures'], format='json') |
|
77 [{"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"}}] |
|
78 """} |
|
79 |
|
80 from django.test import TestCase |
|
81 |
|
82 class SampleTestCase(TestCase): |
|
83 fixtures = ['fixture1.json', 'fixture2.json'] |
|
84 |
|
85 def testClassFixtures(self): |
|
86 "Check that test case has installed 4 fixture objects" |
|
87 self.assertEqual(Article.objects.count(), 4) |
|
88 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>]") |
|