PageRenderTime 365ms CodeModel.GetById 31ms RepoModel.GetById 9ms app.codeStats 1ms

/tests/modeltests/fixtures/tests.py

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