|
1 import StringIO |
|
2 import sys |
|
3 |
|
4 from django.test import TestCase, TransactionTestCase |
|
5 from django.conf import settings |
|
6 from django.core import management |
|
7 from django.db import DEFAULT_DB_ALIAS |
|
8 |
|
9 from models import Article, Blog, Book, Category, Person, Tag, Visa |
|
10 |
|
11 class TestCaseFixtureLoadingTests(TestCase): |
|
12 fixtures = ['fixture1.json', 'fixture2.json'] |
|
13 |
|
14 def testClassFixtures(self): |
|
15 "Check that test case has installed 4 fixture objects" |
|
16 self.assertEqual(Article.objects.count(), 4) |
|
17 self.assertQuerysetEqual(Article.objects.all(), [ |
|
18 '<Article: Django conquers world!>', |
|
19 '<Article: Copyright is fine the way it is>', |
|
20 '<Article: Poker has no place on ESPN>', |
|
21 '<Article: Python program becomes self aware>' |
|
22 ]) |
|
23 |
|
24 class FixtureLoadingTests(TestCase): |
|
25 |
|
26 def _dumpdata_assert(self, args, output, format='json', natural_keys=False): |
|
27 new_io = StringIO.StringIO() |
|
28 management.call_command('dumpdata', *args, **{'format':format, 'stdout':new_io, 'use_natural_keys':natural_keys}) |
|
29 command_output = new_io.getvalue().strip() |
|
30 self.assertEqual(command_output, output) |
|
31 |
|
32 def test_initial_data(self): |
|
33 # Syncdb introduces 1 initial data object from initial_data.json. |
|
34 self.assertQuerysetEqual(Article.objects.all(), [ |
|
35 '<Article: Python program becomes self aware>' |
|
36 ]) |
|
37 |
|
38 def test_loading_and_dumping(self): |
|
39 new_io = StringIO.StringIO() |
|
40 |
|
41 # Load fixture 1. Single JSON file, with two objects. |
|
42 management.call_command('loaddata', 'fixture1.json', verbosity=0, commit=False) |
|
43 self.assertQuerysetEqual(Article.objects.all(), [ |
|
44 '<Article: Time to reform copyright>', |
|
45 '<Article: Poker has no place on ESPN>', |
|
46 '<Article: Python program becomes self aware>' |
|
47 ]) |
|
48 |
|
49 # Dump the current contents of the database as a JSON fixture |
|
50 self._dumpdata_assert(['fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"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"}}]') |
|
51 |
|
52 # Try just dumping the contents of fixtures.Category |
|
53 self._dumpdata_assert(['fixtures.Category'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}]') |
|
54 |
|
55 # ...and just fixtures.Article |
|
56 self._dumpdata_assert(['fixtures.Article'], '[{"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"}}]') |
|
57 |
|
58 # ...and both |
|
59 self._dumpdata_assert(['fixtures.Category', 'fixtures.Article'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"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"}}]') |
|
60 |
|
61 # Specify a specific model twice |
|
62 self._dumpdata_assert(['fixtures.Article', 'fixtures.Article'], '[{"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"}}]') |
|
63 |
|
64 # Specify a dump that specifies Article both explicitly and implicitly |
|
65 self._dumpdata_assert(['fixtures.Article', 'fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"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"}}]') |
|
66 |
|
67 # Same again, but specify in the reverse order |
|
68 self._dumpdata_assert(['fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"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"}}]') |
|
69 |
|
70 # Specify one model from one application, and an entire other application. |
|
71 self._dumpdata_assert(['fixtures.Category', 'sites'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 1, "model": "sites.site", "fields": {"domain": "example.com", "name": "example.com"}}]') |
|
72 |
|
73 # Load fixture 2. JSON file imported by default. Overwrites some existing objects |
|
74 management.call_command('loaddata', 'fixture2.json', verbosity=0, commit=False) |
|
75 self.assertQuerysetEqual(Article.objects.all(), [ |
|
76 '<Article: Django conquers world!>', |
|
77 '<Article: Copyright is fine the way it is>', |
|
78 '<Article: Poker has no place on ESPN>', |
|
79 '<Article: Python program becomes self aware>' |
|
80 ]) |
|
81 |
|
82 # Load fixture 3, XML format. |
|
83 management.call_command('loaddata', 'fixture3.xml', verbosity=0, commit=False) |
|
84 self.assertQuerysetEqual(Article.objects.all(), [ |
|
85 '<Article: XML identified as leading cause of cancer>', |
|
86 '<Article: Django conquers world!>', |
|
87 '<Article: Copyright is fine the way it is>', |
|
88 '<Article: Poker on TV is great!>', |
|
89 '<Article: Python program becomes self aware>' |
|
90 ]) |
|
91 |
|
92 # Load fixture 6, JSON file with dynamic ContentType fields. Testing ManyToOne. |
|
93 management.call_command('loaddata', 'fixture6.json', verbosity=0, commit=False) |
|
94 self.assertQuerysetEqual(Tag.objects.all(), [ |
|
95 '<Tag: <Article: Copyright is fine the way it is> tagged "copyright">', |
|
96 '<Tag: <Article: Copyright is fine the way it is> tagged "law">' |
|
97 ]) |
|
98 |
|
99 # Load fixture 7, XML file with dynamic ContentType fields. Testing ManyToOne. |
|
100 management.call_command('loaddata', 'fixture7.xml', verbosity=0, commit=False) |
|
101 self.assertQuerysetEqual(Tag.objects.all(), [ |
|
102 '<Tag: <Article: Copyright is fine the way it is> tagged "copyright">', |
|
103 '<Tag: <Article: Copyright is fine the way it is> tagged "legal">', |
|
104 '<Tag: <Article: Django conquers world!> tagged "django">', |
|
105 '<Tag: <Article: Django conquers world!> tagged "world domination">' |
|
106 ]) |
|
107 |
|
108 # Load fixture 8, JSON file with dynamic Permission fields. Testing ManyToMany. |
|
109 management.call_command('loaddata', 'fixture8.json', verbosity=0, commit=False) |
|
110 self.assertQuerysetEqual(Visa.objects.all(), [ |
|
111 '<Visa: Django Reinhardt Can add user, Can change user, Can delete user>', |
|
112 '<Visa: Stephane Grappelli Can add user>', |
|
113 '<Visa: Prince >' |
|
114 ]) |
|
115 |
|
116 # Load fixture 9, XML file with dynamic Permission fields. Testing ManyToMany. |
|
117 management.call_command('loaddata', 'fixture9.xml', verbosity=0, commit=False) |
|
118 self.assertQuerysetEqual(Visa.objects.all(), [ |
|
119 '<Visa: Django Reinhardt Can add user, Can change user, Can delete user>', |
|
120 '<Visa: Stephane Grappelli Can add user, Can delete user>', |
|
121 '<Visa: Artist formerly known as "Prince" Can change user>' |
|
122 ]) |
|
123 |
|
124 self.assertQuerysetEqual(Book.objects.all(), [ |
|
125 '<Book: Music for all ages by Artist formerly known as "Prince" and Django Reinhardt>' |
|
126 ]) |
|
127 |
|
128 # Load a fixture that doesn't exist |
|
129 management.call_command('loaddata', 'unknown.json', verbosity=0, commit=False) |
|
130 |
|
131 # object list is unaffected |
|
132 self.assertQuerysetEqual(Article.objects.all(), [ |
|
133 '<Article: XML identified as leading cause of cancer>', |
|
134 '<Article: Django conquers world!>', |
|
135 '<Article: Copyright is fine the way it is>', |
|
136 '<Article: Poker on TV is great!>', |
|
137 '<Article: Python program becomes self aware>' |
|
138 ]) |
|
139 |
|
140 # By default, you get raw keys on dumpdata |
|
141 self._dumpdata_assert(['fixtures.book'], '[{"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [3, 1]}}]') |
|
142 |
|
143 # But you can get natural keys if you ask for them and they are available |
|
144 self._dumpdata_assert(['fixtures.book'], '[{"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]', natural_keys=True) |
|
145 |
|
146 # Dump the current contents of the database as a JSON fixture |
|
147 self._dumpdata_assert(['fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 5, "model": "fixtures.article", "fields": {"headline": "XML identified as leading cause of cancer", "pub_date": "2006-06-16 16:00:00"}}, {"pk": 4, "model": "fixtures.article", "fields": {"headline": "Django conquers world!", "pub_date": "2006-06-16 15:00:00"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Copyright is fine the way it is", "pub_date": "2006-06-16 14:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker on TV is great!", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}, {"pk": 1, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "copyright", "tagged_id": 3}}, {"pk": 2, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "legal", "tagged_id": 3}}, {"pk": 3, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "django", "tagged_id": 4}}, {"pk": 4, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "world domination", "tagged_id": 4}}, {"pk": 3, "model": "fixtures.person", "fields": {"name": "Artist formerly known as \\"Prince\\""}}, {"pk": 1, "model": "fixtures.person", "fields": {"name": "Django Reinhardt"}}, {"pk": 2, "model": "fixtures.person", "fields": {"name": "Stephane Grappelli"}}, {"pk": 1, "model": "fixtures.visa", "fields": {"person": ["Django Reinhardt"], "permissions": [["add_user", "auth", "user"], ["change_user", "auth", "user"], ["delete_user", "auth", "user"]]}}, {"pk": 2, "model": "fixtures.visa", "fields": {"person": ["Stephane Grappelli"], "permissions": [["add_user", "auth", "user"], ["delete_user", "auth", "user"]]}}, {"pk": 3, "model": "fixtures.visa", "fields": {"person": ["Artist formerly known as \\"Prince\\""], "permissions": [["change_user", "auth", "user"]]}}, {"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]', natural_keys=True) |
|
148 |
|
149 # Dump the current contents of the database as an XML fixture |
|
150 self._dumpdata_assert(['fixtures'], """<?xml version="1.0" encoding="utf-8"?> |
|
151 <django-objects version="1.0"><object pk="1" model="fixtures.category"><field type="CharField" name="title">News Stories</field><field type="TextField" name="description">Latest news stories</field></object><object pk="5" model="fixtures.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><object pk="4" model="fixtures.article"><field type="CharField" name="headline">Django conquers world!</field><field type="DateTimeField" name="pub_date">2006-06-16 15:00:00</field></object><object pk="3" model="fixtures.article"><field type="CharField" name="headline">Copyright is fine the way it is</field><field type="DateTimeField" name="pub_date">2006-06-16 14:00:00</field></object><object pk="2" model="fixtures.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="1" model="fixtures.article"><field type="CharField" name="headline">Python program becomes self aware</field><field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field></object><object pk="1" model="fixtures.tag"><field type="CharField" name="name">copyright</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="2" model="fixtures.tag"><field type="CharField" name="name">legal</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="3" model="fixtures.tag"><field type="CharField" name="name">django</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">4</field></object><object pk="4" model="fixtures.tag"><field type="CharField" name="name">world domination</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">4</field></object><object pk="3" model="fixtures.person"><field type="CharField" name="name">Artist formerly known as "Prince"</field></object><object pk="1" model="fixtures.person"><field type="CharField" name="name">Django Reinhardt</field></object><object pk="2" model="fixtures.person"><field type="CharField" name="name">Stephane Grappelli</field></object><object pk="1" model="fixtures.visa"><field to="fixtures.person" name="person" rel="ManyToOneRel"><natural>Django Reinhardt</natural></field><field to="auth.permission" name="permissions" rel="ManyToManyRel"><object><natural>add_user</natural><natural>auth</natural><natural>user</natural></object><object><natural>change_user</natural><natural>auth</natural><natural>user</natural></object><object><natural>delete_user</natural><natural>auth</natural><natural>user</natural></object></field></object><object pk="2" model="fixtures.visa"><field to="fixtures.person" name="person" rel="ManyToOneRel"><natural>Stephane Grappelli</natural></field><field to="auth.permission" name="permissions" rel="ManyToManyRel"><object><natural>add_user</natural><natural>auth</natural><natural>user</natural></object><object><natural>delete_user</natural><natural>auth</natural><natural>user</natural></object></field></object><object pk="3" model="fixtures.visa"><field to="fixtures.person" name="person" rel="ManyToOneRel"><natural>Artist formerly known as "Prince"</natural></field><field to="auth.permission" name="permissions" rel="ManyToManyRel"><object><natural>change_user</natural><natural>auth</natural><natural>user</natural></object></field></object><object pk="1" model="fixtures.book"><field type="CharField" name="name">Music for all ages</field><field to="fixtures.person" name="authors" rel="ManyToManyRel"><object><natural>Artist formerly known as "Prince"</natural></object><object><natural>Django Reinhardt</natural></object></field></object></django-objects>""", format='xml', natural_keys=True) |
|
152 |
|
153 def test_compress_format_loading(self): |
|
154 # Load fixture 4 (compressed), using format specification |
|
155 management.call_command('loaddata', 'fixture4.json', verbosity=0, commit=False) |
|
156 self.assertQuerysetEqual(Article.objects.all(), [ |
|
157 '<Article: Django pets kitten>', |
|
158 '<Article: Python program becomes self aware>' |
|
159 ]) |
|
160 |
|
161 def test_compressed_specified_loading(self): |
|
162 # Load fixture 5 (compressed), using format *and* compression specification |
|
163 management.call_command('loaddata', 'fixture5.json.zip', verbosity=0, commit=False) |
|
164 self.assertQuerysetEqual(Article.objects.all(), [ |
|
165 '<Article: WoW subscribers now outnumber readers>', |
|
166 '<Article: Python program becomes self aware>' |
|
167 ]) |
|
168 |
|
169 def test_compressed_loading(self): |
|
170 # Load fixture 5 (compressed), only compression specification |
|
171 management.call_command('loaddata', 'fixture5.zip', verbosity=0, commit=False) |
|
172 self.assertQuerysetEqual(Article.objects.all(), [ |
|
173 '<Article: WoW subscribers now outnumber readers>', |
|
174 '<Article: Python program becomes self aware>' |
|
175 ]) |
|
176 |
|
177 def test_ambiguous_compressed_fixture(self): |
|
178 # The name "fixture5" is ambigous, so loading it will raise an error |
|
179 new_io = StringIO.StringIO() |
|
180 management.call_command('loaddata', 'fixture5', verbosity=0, stderr=new_io, commit=False) |
|
181 output = new_io.getvalue().strip().split('\n') |
|
182 self.assertEqual(len(output), 1) |
|
183 self.assertTrue(output[0].startswith("Multiple fixtures named 'fixture5'")) |
|
184 |
|
185 def test_db_loading(self): |
|
186 # Load db fixtures 1 and 2. These will load using the 'default' database identifier implicitly |
|
187 management.call_command('loaddata', 'db_fixture_1', verbosity=0, commit=False) |
|
188 management.call_command('loaddata', 'db_fixture_2', verbosity=0, commit=False) |
|
189 self.assertQuerysetEqual(Article.objects.all(), [ |
|
190 '<Article: Who needs more than one database?>', |
|
191 '<Article: Who needs to use compressed data?>', |
|
192 '<Article: Python program becomes self aware>' |
|
193 ]) |
|
194 |
|
195 def test_loading_using(self): |
|
196 # Load db fixtures 1 and 2. These will load using the 'default' database identifier explicitly |
|
197 management.call_command('loaddata', 'db_fixture_1', verbosity=0, using='default', commit=False) |
|
198 management.call_command('loaddata', 'db_fixture_2', verbosity=0, using='default', commit=False) |
|
199 self.assertQuerysetEqual(Article.objects.all(), [ |
|
200 '<Article: Who needs more than one database?>', |
|
201 '<Article: Who needs to use compressed data?>', |
|
202 '<Article: Python program becomes self aware>' |
|
203 ]) |
|
204 |
|
205 def test_unmatched_identifier_loading(self): |
|
206 # Try to load db fixture 3. This won't load because the database identifier doesn't match |
|
207 management.call_command('loaddata', 'db_fixture_3', verbosity=0, commit=False) |
|
208 self.assertQuerysetEqual(Article.objects.all(), [ |
|
209 '<Article: Python program becomes self aware>' |
|
210 ]) |
|
211 |
|
212 management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default', commit=False) |
|
213 self.assertQuerysetEqual(Article.objects.all(), [ |
|
214 '<Article: Python program becomes self aware>' |
|
215 ]) |
|
216 |
|
217 def test_output_formats(self): |
|
218 # Load back in fixture 1, we need the articles from it |
|
219 management.call_command('loaddata', 'fixture1', verbosity=0, commit=False) |
|
220 |
|
221 # Try to load fixture 6 using format discovery |
|
222 management.call_command('loaddata', 'fixture6', verbosity=0, commit=False) |
|
223 self.assertQuerysetEqual(Tag.objects.all(), [ |
|
224 '<Tag: <Article: Time to reform copyright> tagged "copyright">', |
|
225 '<Tag: <Article: Time to reform copyright> tagged "law">' |
|
226 ]) |
|
227 |
|
228 # Dump the current contents of the database as a JSON fixture |
|
229 self._dumpdata_assert(['fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"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"}}, {"pk": 1, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "copyright", "tagged_id": 3}}, {"pk": 2, "model": "fixtures.tag", "fields": {"tagged_type": ["fixtures", "article"], "name": "law", "tagged_id": 3}}, {"pk": 1, "model": "fixtures.person", "fields": {"name": "Django Reinhardt"}}, {"pk": 3, "model": "fixtures.person", "fields": {"name": "Prince"}}, {"pk": 2, "model": "fixtures.person", "fields": {"name": "Stephane Grappelli"}}]', natural_keys=True) |
|
230 |
|
231 # Dump the current contents of the database as an XML fixture |
|
232 self._dumpdata_assert(['fixtures'], """<?xml version="1.0" encoding="utf-8"?> |
|
233 <django-objects version="1.0"><object pk="1" model="fixtures.category"><field type="CharField" name="title">News Stories</field><field type="TextField" name="description">Latest news stories</field></object><object pk="3" model="fixtures.article"><field type="CharField" name="headline">Time to reform copyright</field><field type="DateTimeField" name="pub_date">2006-06-16 13:00:00</field></object><object pk="2" model="fixtures.article"><field type="CharField" name="headline">Poker has no place on ESPN</field><field type="DateTimeField" name="pub_date">2006-06-16 12:00:00</field></object><object pk="1" model="fixtures.article"><field type="CharField" name="headline">Python program becomes self aware</field><field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field></object><object pk="1" model="fixtures.tag"><field type="CharField" name="name">copyright</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="2" model="fixtures.tag"><field type="CharField" name="name">law</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="1" model="fixtures.person"><field type="CharField" name="name">Django Reinhardt</field></object><object pk="3" model="fixtures.person"><field type="CharField" name="name">Prince</field></object><object pk="2" model="fixtures.person"><field type="CharField" name="name">Stephane Grappelli</field></object></django-objects>""", format='xml', natural_keys=True) |
|
234 |
|
235 if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql': |
|
236 class FixtureTransactionTests(TransactionTestCase): |
|
237 def _dumpdata_assert(self, args, output, format='json'): |
|
238 new_io = StringIO.StringIO() |
|
239 management.call_command('dumpdata', *args, **{'format':format, 'stdout':new_io}) |
|
240 command_output = new_io.getvalue().strip() |
|
241 self.assertEqual(command_output, output) |
|
242 |
|
243 def test_format_discovery(self): |
|
244 # Load fixture 1 again, using format discovery |
|
245 management.call_command('loaddata', 'fixture1', verbosity=0, commit=False) |
|
246 self.assertQuerysetEqual(Article.objects.all(), [ |
|
247 '<Article: Time to reform copyright>', |
|
248 '<Article: Poker has no place on ESPN>', |
|
249 '<Article: Python program becomes self aware>' |
|
250 ]) |
|
251 |
|
252 # Try to load fixture 2 using format discovery; this will fail |
|
253 # because there are two fixture2's in the fixtures directory |
|
254 new_io = StringIO.StringIO() |
|
255 management.call_command('loaddata', 'fixture2', verbosity=0, stderr=new_io) |
|
256 output = new_io.getvalue().strip().split('\n') |
|
257 self.assertEqual(len(output), 1) |
|
258 self.assertTrue(output[0].startswith("Multiple fixtures named 'fixture2'")) |
|
259 |
|
260 # object list is unaffected |
|
261 self.assertQuerysetEqual(Article.objects.all(), [ |
|
262 '<Article: Time to reform copyright>', |
|
263 '<Article: Poker has no place on ESPN>', |
|
264 '<Article: Python program becomes self aware>' |
|
265 ]) |
|
266 |
|
267 # Dump the current contents of the database as a JSON fixture |
|
268 self._dumpdata_assert(['fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"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"}}]') |
|
269 |
|
270 # Load fixture 4 (compressed), using format discovery |
|
271 management.call_command('loaddata', 'fixture4', verbosity=0, commit=False) |
|
272 self.assertQuerysetEqual(Article.objects.all(), [ |
|
273 '<Article: Django pets kitten>', |
|
274 '<Article: Time to reform copyright>', |
|
275 '<Article: Poker has no place on ESPN>', |
|
276 '<Article: Python program becomes self aware>' |
|
277 ]) |