Ticket #13636: nicer-migration-patch.diff

File nicer-migration-patch.diff, 50.3 KB (added by ericholscher, 5 years ago)

A slightly updated patch.

  • django/core/management/base.py

    commit 451b38a6a4e7ef2e058f7d16e5acff5e639c3483
    Author: Eric Holscher <eric@ericholscher.com>
    Date:   Thu May 27 17:43:59 2010 +0200
    
        Change fixtures tests to unit tests. This should make these tests a lot
        faster on real databases.
    
    diff --git a/django/core/management/base.py b/django/core/management/base.py
    index 4016faa..0bce8c8 100644
    a b class BaseCommand(object): 
    215215        try:
    216216            if self.requires_model_validation:
    217217                self.validate()
     218            to_output = ""
    218219            output = self.handle(*args, **options)
    219220            if output:
    220221                if self.output_transaction:
    221222                    # This needs to be imported here, because it relies on settings.
    222223                    from django.db import connection
    223224                    if connection.ops.start_transaction_sql():
    224                         print self.style.SQL_KEYWORD(connection.ops.start_transaction_sql())
    225                 print output
     225                        to_output += self.style.SQL_KEYWORD(connection.ops.start_transaction_sql())
     226                to_output += output
    226227                if self.output_transaction:
    227                     print self.style.SQL_KEYWORD("COMMIT;")
     228                    to_output += self.style.SQL_KEYWORD("COMMIT;")
     229                if options.get('return_output'):
     230                    return to_output
     231                print to_output
    228232        except CommandError, e:
    229233            sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
    230234            sys.exit(1)
  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index 2f8acad..15b4c23 100644
    a b class TransactionTestCase(unittest.TestCase): 
    466466            msg_prefix + "Template '%s' was used unexpectedly in rendering"
    467467            " the response" % template_name)
    468468
     469    def assertQuerysetEqual(self, qs, vals, transform=repr):
     470        return self.assertEqual(map(transform, qs), vals)
     471
    469472def connections_support_transactions():
    470473    """
    471474    Returns True if all connections support transactions.  This is messy
  • tests/modeltests/fixtures/models.py

    diff --git a/tests/modeltests/fixtures/models.py b/tests/modeltests/fixtures/models.py
    index 46e07a5..216a8e2 100644
    a b class Book(models.Model): 
    9090
    9191    class Meta:
    9292        ordering = ('name',)
    93 
    94 __test__ = {'API_TESTS': """
    95 >>> from django.core import management
    96 >>> from django.db.models import get_app
    97 
    98 # Reset the database representation of this app.
    99 # This will return the database to a clean initial state.
    100 >>> management.call_command('flush', verbosity=0, interactive=False)
    101 
    102 # Syncdb introduces 1 initial data object from initial_data.json.
    103 >>> Article.objects.all()
    104 [<Article: Python program becomes self aware>]
    105 
    106 # Load fixture 1. Single JSON file, with two objects.
    107 >>> management.call_command('loaddata', 'fixture1.json', verbosity=0)
    108 >>> Article.objects.all()
    109 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
    110 
    111 # Dump the current contents of the database as a JSON fixture
    112 >>> management.call_command('dumpdata', 'fixtures', format='json')
    113 [{"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"}}]
    114 
    115 # Try just dumping the contents of fixtures.Category
    116 >>> management.call_command('dumpdata', 'fixtures.Category', format='json')
    117 [{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}]
    118 
    119 # ...and just fixtures.Article
    120 >>> management.call_command('dumpdata', 'fixtures.Article', format='json')
    121 [{"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"}}]
    122 
    123 # ...and both
    124 >>> management.call_command('dumpdata', 'fixtures.Category', 'fixtures.Article', format='json')
    125 [{"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"}}]
    126 
    127 # Specify a specific model twice
    128 >>> management.call_command('dumpdata', 'fixtures.Article', 'fixtures.Article', format='json')
    129 [{"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"}}]
    130 
    131 # Specify a dump that specifies Article both explicitly and implicitly
    132 >>> management.call_command('dumpdata', 'fixtures.Article', 'fixtures', format='json')
    133 [{"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"}}]
    134 
    135 # Same again, but specify in the reverse order
    136 >>> management.call_command('dumpdata', 'fixtures', 'fixtures.Article', format='json')
    137 [{"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"}}]
    138 
    139 # Specify one model from one application, and an entire other application.
    140 >>> management.call_command('dumpdata', 'fixtures.Category', 'sites', format='json')
    141 [{"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"}}]
    142 
    143 # Load fixture 2. JSON file imported by default. Overwrites some existing objects
    144 >>> management.call_command('loaddata', 'fixture2.json', verbosity=0)
    145 >>> Article.objects.all()
    146 [<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>]
    147 
    148 # Load fixture 3, XML format.
    149 >>> management.call_command('loaddata', 'fixture3.xml', verbosity=0)
    150 >>> Article.objects.all()
    151 [<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>]
    152 
    153 # Load fixture 6, JSON file with dynamic ContentType fields. Testing ManyToOne.
    154 >>> management.call_command('loaddata', 'fixture6.json', verbosity=0)
    155 >>> Tag.objects.all()
    156 [<Tag: <Article: Copyright is fine the way it is> tagged "copyright">, <Tag: <Article: Copyright is fine the way it is> tagged "law">]
    157 
    158 # Load fixture 7, XML file with dynamic ContentType fields. Testing ManyToOne.
    159 >>> management.call_command('loaddata', 'fixture7.xml', verbosity=0)
    160 >>> Tag.objects.all()
    161 [<Tag: <Article: Copyright is fine the way it is> tagged "copyright">, <Tag: <Article: Copyright is fine the way it is> tagged "legal">, <Tag: <Article: Django conquers world!> tagged "django">, <Tag: <Article: Django conquers world!> tagged "world domination">]
    162 
    163 # Load fixture 8, JSON file with dynamic Permission fields. Testing ManyToMany.
    164 >>> management.call_command('loaddata', 'fixture8.json', verbosity=0)
    165 >>> Visa.objects.all()
    166 [<Visa: Django Reinhardt Can add user, Can change user, Can delete user>, <Visa: Stephane Grappelli Can add user>, <Visa: Prince >]
    167 
    168 # Load fixture 9, XML file with dynamic Permission fields. Testing ManyToMany.
    169 >>> management.call_command('loaddata', 'fixture9.xml', verbosity=0)
    170 >>> Visa.objects.all()
    171 [<Visa: Django Reinhardt Can add user, Can change user, Can delete user>, <Visa: Stephane Grappelli Can add user, Can delete user>, <Visa: Artist formerly known as "Prince" Can change user>]
    172 
    173 >>> Book.objects.all()
    174 [<Book: Music for all ages by Artist formerly known as "Prince" and Django Reinhardt>]
    175 
    176 # Load a fixture that doesn't exist
    177 >>> management.call_command('loaddata', 'unknown.json', verbosity=0)
    178 
    179 # object list is unaffected
    180 >>> Article.objects.all()
    181 [<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>]
    182 
    183 # By default, you get raw keys on dumpdata
    184 >>> management.call_command('dumpdata', 'fixtures.book', format='json')
    185 [{"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [3, 1]}}]
    186 
    187 # But you can get natural keys if you ask for them and they are available
    188 >>> management.call_command('dumpdata', 'fixtures.book', format='json', use_natural_keys=True)
    189 [{"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]
    190 
    191 # Dump the current contents of the database as a JSON fixture
    192 >>> management.call_command('dumpdata', 'fixtures', format='json', use_natural_keys=True)
    193 [{"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"]]}}]
    194 
    195 # Dump the current contents of the database as an XML fixture
    196 >>> management.call_command('dumpdata', 'fixtures', format='xml', use_natural_keys=True)
    197 <?xml version="1.0" encoding="utf-8"?>
    198 <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>
    199 
    200 """}
    201 
    202 # Database flushing does not work on MySQL with the default storage engine
    203 # because it requires transaction support.
    204 if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
    205     __test__['API_TESTS'] += \
    206 """
    207 # Reset the database representation of this app. This will delete all data.
    208 >>> management.call_command('flush', verbosity=0, interactive=False)
    209 >>> Article.objects.all()
    210 [<Article: Python program becomes self aware>]
    211 
    212 # Load fixture 1 again, using format discovery
    213 >>> management.call_command('loaddata', 'fixture1', verbosity=0)
    214 >>> Article.objects.all()
    215 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
    216 
    217 # Try to load fixture 2 using format discovery; this will fail
    218 # because there are two fixture2's in the fixtures directory
    219 >>> management.call_command('loaddata', 'fixture2', verbosity=0) # doctest: +ELLIPSIS
    220 Multiple fixtures named 'fixture2' in '...fixtures'. Aborting.
    221 
    222 # object list is unaffected
    223 >>> Article.objects.all()
    224 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
    225 
    226 # Dump the current contents of the database as a JSON fixture
    227 >>> management.call_command('dumpdata', 'fixtures', format='json')
    228 [{"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"}}]
    229 
    230 # Load fixture 4 (compressed), using format discovery
    231 >>> management.call_command('loaddata', 'fixture4', verbosity=0)
    232 >>> Article.objects.all()
    233 [<Article: Django pets kitten>, <Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
    234 
    235 >>> management.call_command('flush', verbosity=0, interactive=False)
    236 
    237 # Load fixture 4 (compressed), using format specification
    238 >>> management.call_command('loaddata', 'fixture4.json', verbosity=0)
    239 >>> Article.objects.all()
    240 [<Article: Django pets kitten>, <Article: Python program becomes self aware>]
    241 
    242 >>> management.call_command('flush', verbosity=0, interactive=False)
    243 
    244 # Load fixture 5 (compressed), using format *and* compression specification
    245 >>> management.call_command('loaddata', 'fixture5.json.zip', verbosity=0)
    246 >>> Article.objects.all()
    247 [<Article: WoW subscribers now outnumber readers>, <Article: Python program becomes self aware>]
    248 
    249 >>> management.call_command('flush', verbosity=0, interactive=False)
    250 
    251 # Load fixture 5 (compressed), only compression specification
    252 >>> management.call_command('loaddata', 'fixture5.zip', verbosity=0)
    253 >>> Article.objects.all()
    254 [<Article: WoW subscribers now outnumber readers>, <Article: Python program becomes self aware>]
    255 
    256 >>> management.call_command('flush', verbosity=0, interactive=False)
    257 
    258 # Try to load fixture 5 using format and compression discovery; this will fail
    259 # because there are two fixture5's in the fixtures directory
    260 >>> management.call_command('loaddata', 'fixture5', verbosity=0) # doctest: +ELLIPSIS
    261 Multiple fixtures named 'fixture5' in '...fixtures'. Aborting.
    262 
    263 >>> management.call_command('flush', verbosity=0, interactive=False)
    264 
    265 # Load db fixtures 1 and 2. These will load using the 'default' database identifier implicitly
    266 >>> management.call_command('loaddata', 'db_fixture_1', verbosity=0)
    267 >>> management.call_command('loaddata', 'db_fixture_2', verbosity=0)
    268 >>> Article.objects.all()
    269 [<Article: Who needs more than one database?>, <Article: Who needs to use compressed data?>, <Article: Python program becomes self aware>]
    270 
    271 >>> management.call_command('flush', verbosity=0, interactive=False)
    272 
    273 # Load db fixtures 1 and 2. These will load using the 'default' database identifier explicitly
    274 >>> management.call_command('loaddata', 'db_fixture_1', verbosity=0, using='default')
    275 >>> management.call_command('loaddata', 'db_fixture_2', verbosity=0, using='default')
    276 >>> Article.objects.all()
    277 [<Article: Who needs more than one database?>, <Article: Who needs to use compressed data?>, <Article: Python program becomes self aware>]
    278 
    279 >>> management.call_command('flush', verbosity=0, interactive=False)
    280 
    281 # Try to load db fixture 3. This won't load because the database identifier doesn't match
    282 >>> management.call_command('loaddata', 'db_fixture_3', verbosity=0)
    283 >>> Article.objects.all()
    284 [<Article: Python program becomes self aware>]
    285 
    286 >>> management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default')
    287 >>> Article.objects.all()
    288 [<Article: Python program becomes self aware>]
    289 
    290 >>> management.call_command('flush', verbosity=0, interactive=False)
    291 
    292 # Load back in fixture 1, we need the articles from it
    293 >>> management.call_command('loaddata', 'fixture1', verbosity=0)
    294 
    295 # Try to load fixture 6 using format discovery
    296 >>> management.call_command('loaddata', 'fixture6', verbosity=0)
    297 >>> Tag.objects.all()
    298 [<Tag: <Article: Time to reform copyright> tagged "copyright">, <Tag: <Article: Time to reform copyright> tagged "law">]
    299 
    300 # Dump the current contents of the database as a JSON fixture
    301 >>> management.call_command('dumpdata', 'fixtures', format='json', use_natural_keys=True)
    302 [{"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"}}]
    303 
    304 # Dump the current contents of the database as an XML fixture
    305 >>> management.call_command('dumpdata', 'fixtures', format='xml', use_natural_keys=True)
    306 <?xml version="1.0" encoding="utf-8"?>
    307 <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>
    308 
    309 """
    310 
    311 from django.test import TestCase
    312 
    313 class SampleTestCase(TestCase):
    314     fixtures = ['fixture1.json', 'fixture2.json']
    315 
    316     def testClassFixtures(self):
    317         "Check that test case has installed 4 fixture objects"
    318         self.assertEqual(Article.objects.count(), 4)
    319         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>]")
  • new file tests/modeltests/fixtures/tests.py

    diff --git a/tests/modeltests/fixtures/tests.py b/tests/modeltests/fixtures/tests.py
    new file mode 100644
    index 0000000..b20d264
    - +  
     1import StringIO
     2import sys
     3
     4from django.test import TestCase, TransactionTestCase
     5from django.core import management
     6
     7from models import Article, Blog, Book, Category, Person, Tag, Visa
     8
     9class FixtureLoadingTests(TestCase):
     10    fixtures = ['fixture1.json', 'fixture2.json']
     11
     12    def testClassFixtures(self):
     13        "Check that test case has installed 4 fixture objects"
     14        self.assertEqual(Article.objects.count(), 4)
     15        self.assertQuerysetEqual(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>'])
     16
     17class MainFixtureLoadingTests(TestCase):
     18
     19    def test_initial_data(self):
     20        # Syncdb introduces 1 initial data object from initial_data.json.
     21        self.assertQuerysetEqual(Article.objects.all(), ['Python program becomes self aware'], str)
     22
     23    def test_loading_and_dumping(self):
     24
     25        # Load fixture 1. Single JSON file, with two objects.
     26        management.call_command('loaddata', 'fixture1.json', verbosity=0, commit=False)
     27        self.assertQuerysetEqual(Article.objects.all(), ['Time to reform copyright', 'Poker has no place on ESPN', 'Python program becomes self aware'], str)
     28
     29        # Dump the current contents of the database as a JSON fixture
     30        self.assertEqual(management.call_command('dumpdata', 'fixtures', format='json', return_output=True), '[{"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"}}]')
     31
     32        # Try just dumping the contents of fixtures.Category
     33        self.assertEqual(management.call_command('dumpdata', 'fixtures.Category', format='json', return_output=True), '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}]')
     34
     35        # ...and just fixtures.Article
     36        self.assertEqual(management.call_command('dumpdata', 'fixtures.Article', format='json', return_output=True), '[{"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"}}]')
     37
     38        # ...and both
     39        self.assertEqual(management.call_command('dumpdata', 'fixtures.Category', 'fixtures.Article', format='json', return_output=True), '[{"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"}}]')
     40
     41        # Specify a specific model twice
     42        self.assertEqual(management.call_command('dumpdata', 'fixtures.Article', 'fixtures.Article', format='json', return_output=True), '[{"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"}}]')
     43
     44        # Specify a dump that specifies Article both explicitly and implicitly
     45        self.assertEqual(management.call_command('dumpdata', 'fixtures.Article', 'fixtures', format='json', return_output=True), '[{"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"}}]')
     46
     47        # Same again, but specify in the reverse order
     48        self.assertEqual(management.call_command('dumpdata', 'fixtures', 'fixtures.Article', format='json', return_output=True), '[{"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
     50        # Specify one model from one application, and an entire other application.
     51        self.assertEqual(management.call_command('dumpdata', 'fixtures.Category', 'sites', format='json', return_output=True), '[{"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"}}]')
     52
     53        # Load fixture 2. JSON file imported by default. Overwrites some existing objects
     54        management.call_command('loaddata', 'fixture2.json', verbosity=0, commit=False)
     55        self.assertQuerysetEqual(Article.objects.all(), ['Django conquers world!', 'Copyright is fine the way it is', 'Poker has no place on ESPN', 'Python program becomes self aware'], str)
     56
     57        # Load fixture 3, XML format.
     58        management.call_command('loaddata', 'fixture3.xml', verbosity=0, commit=False)
     59        self.assertQuerysetEqual(Article.objects.all(), ['XML identified as leading cause of cancer', 'Django conquers world!', 'Copyright is fine the way it is', 'Poker on TV is great!', 'Python program becomes self aware'], str)
     60
     61        # Load fixture 6, JSON file with dynamic ContentType fields. Testing ManyToOne.
     62        management.call_command('loaddata', 'fixture6.json', verbosity=0, commit=False)
     63        self.assertQuerysetEqual(Tag.objects.all(), ['<Article: Copyright is fine the way it is> tagged "copyright"', '<Article: Copyright is fine the way it is> tagged "law"'], str)
     64
     65        # Load fixture 7, XML file with dynamic ContentType fields. Testing ManyToOne.
     66        management.call_command('loaddata', 'fixture7.xml', verbosity=0, commit=False)
     67        self.assertQuerysetEqual(Tag.objects.all(), ['<Article: Copyright is fine the way it is> tagged "copyright"', '<Article: Copyright is fine the way it is> tagged "legal"', '<Article: Django conquers world!> tagged "django"', '<Article: Django conquers world!> tagged "world domination"'], str)
     68
     69        # Load fixture 8, JSON file with dynamic Permission fields. Testing ManyToMany.
     70        management.call_command('loaddata', 'fixture8.json', verbosity=0, commit=False)
     71        self.assertQuerysetEqual(Visa.objects.all(), ['Django Reinhardt Can add user, Can change user, Can delete user', 'Stephane Grappelli Can add user', 'Prince '], str)
     72
     73        # Load fixture 9, XML file with dynamic Permission fields. Testing ManyToMany.
     74        management.call_command('loaddata', 'fixture9.xml', verbosity=0, commit=False)
     75        self.assertQuerysetEqual(Visa.objects.all(), ['Django Reinhardt Can add user, Can change user, Can delete user', 'Stephane Grappelli Can add user, Can delete user', 'Artist formerly known as "Prince" Can change user'], str)
     76
     77        self.assertQuerysetEqual(Book.objects.all(), ['Music for all ages by Artist formerly known as "Prince" and Django Reinhardt'], str)
     78
     79        # Load a fixture that doesn't exist
     80        management.call_command('loaddata', 'unknown.json', verbosity=0, commit=False)
     81
     82        # object list is unaffected
     83        self.assertQuerysetEqual(Article.objects.all(), ['XML identified as leading cause of cancer', 'Django conquers world!', 'Copyright is fine the way it is', 'Poker on TV is great!', 'Python program becomes self aware'], str)
     84
     85        # By default, you get raw keys on dumpdata
     86        self.assertEqual(management.call_command('dumpdata', 'fixtures.book', format='json', return_output=True), '[{"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [3, 1]}}]')
     87
     88        # But you can get natural keys if you ask for them and they are available
     89        self.assertEqual(management.call_command('dumpdata', 'fixtures.book', format='json', use_natural_keys=True, return_output=True), '[{"pk": 1, "model": "fixtures.book", "fields": {"name": "Music for all ages", "authors": [["Artist formerly known as \\"Prince\\""], ["Django Reinhardt"]]}}]')
     90
     91        # Dump the current contents of the database as a JSON fixture
     92        self.assertEqual(management.call_command('dumpdata', 'fixtures', format='json', use_natural_keys=True, return_output=True), '[{"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"]]}}]')
     93
     94        # Dump the current contents of the database as an XML fixture
     95        self.assertEqual(management.call_command('dumpdata', 'fixtures', format='xml', use_natural_keys=True, return_output=True), """<?xml version="1.0" encoding="utf-8"?>
     96<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>""")
     97
     98
     99    def test_compress_format_loading(self):
     100        # Load fixture 4 (compressed), using format specification
     101        management.call_command('loaddata', 'fixture4.json', verbosity=0, commit=False)
     102        self.assertQuerysetEqual(Article.objects.all(), ['Django pets kitten', 'Python program becomes self aware'], str)
     103
     104    def test_compressed_specified_loading(self):
     105        # Load fixture 5 (compressed), using format *and* compression specification
     106        management.call_command('loaddata', 'fixture5.json.zip', verbosity=0, commit=False)
     107        self.assertQuerysetEqual(Article.objects.all(), ['WoW subscribers now outnumber readers', 'Python program becomes self aware'], str)
     108
     109    def test_compressed_loading(self):
     110        # Load fixture 5 (compressed), only compression specification
     111        management.call_command('loaddata', 'fixture5.zip', verbosity=0, commit=False)
     112        self.assertQuerysetEqual(Article.objects.all(), ['WoW subscribers now outnumber readers', 'Python program becomes self aware'], str)
     113
     114    def test_db_loading(self):
     115        # Load db fixtures 1 and 2. These will load using the 'default' database identifier implicitly
     116        management.call_command('loaddata', 'db_fixture_1', verbosity=0, commit=False)
     117        management.call_command('loaddata', 'db_fixture_2', verbosity=0, commit=False)
     118        self.assertQuerysetEqual(Article.objects.all(), ['Who needs more than one database?', 'Who needs to use compressed data?', 'Python program becomes self aware'], str)
     119
     120    def test_loading_using(self):
     121
     122        # Load db fixtures 1 and 2. These will load using the 'default' database identifier explicitly
     123        management.call_command('loaddata', 'db_fixture_1', verbosity=0, using='default', commit=False)
     124        management.call_command('loaddata', 'db_fixture_2', verbosity=0, using='default', commit=False)
     125        self.assertQuerysetEqual(Article.objects.all(), ['Who needs more than one database?', 'Who needs to use compressed data?', 'Python program becomes self aware'], str)
     126
     127    def test_unmatched_identifier_loading(self):
     128
     129        # Try to load db fixture 3. This won't load because the database identifier doesn't match
     130        management.call_command('loaddata', 'db_fixture_3', verbosity=0, commit=False)
     131        self.assertQuerysetEqual(Article.objects.all(), ['Python program becomes self aware'], str)
     132
     133        management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default', commit=False)
     134        self.assertQuerysetEqual(Article.objects.all(), ['Python program becomes self aware'], str)
     135
     136    def test_xml_json_dumping(self):
     137
     138        # Load back in fixture 1, we need the articles from it
     139        management.call_command('loaddata', 'fixture1', verbosity=0, commit=False)
     140
     141        # Try to load fixture 6 using format discovery
     142        management.call_command('loaddata', 'fixture6', verbosity=0, commit=False)
     143        self.assertQuerysetEqual(Tag.objects.all(), ['<Article: Time to reform copyright> tagged "copyright"', '<Article: Time to reform copyright> tagged "law"'], str)
     144
     145        # Dump the current contents of the database as a JSON fixture
     146        self.assertEqual(management.call_command('dumpdata', 'fixtures', format='json', use_natural_keys=True, return_output=True), '[{"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"}}]')
     147
     148        # Dump the current contents of the database as an XML fixture
     149        self.assertEqual(management.call_command('dumpdata', 'fixtures', format='xml', use_natural_keys=True, return_output=True), """<?xml version="1.0" encoding="utf-8"?>
     150<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>""")
     151
     152class FixtureTransactionTests(TransactionTestCase):
     153
     154    def test_format_discovery(self):
     155
     156        # Load fixture 1 again, using format discovery
     157        management.call_command('loaddata', 'fixture1', verbosity=0, commit=False)
     158        self.assertQuerysetEqual(Article.objects.all(), ['Time to reform copyright', 'Poker has no place on ESPN', 'Python program becomes self aware'], str)
     159
     160        # Try to load fixture 2 using format discovery; this will fail
     161        # because there are two fixture2's in the fixtures directory
     162        old_io = sys.stdout
     163        new_io = StringIO.StringIO()
     164        sys.stdout = new_io
     165        management.call_command('loaddata', 'fixture2', verbosity=0)
     166        output = new_io.getvalue().strip()
     167        self.assertTrue("Multiple fixtures named 'fixture2'" in output)
     168        sys.stdout = old_io
     169
     170        # object list is unaffected
     171        self.assertQuerysetEqual(Article.objects.all(), ['Time to reform copyright', 'Poker has no place on ESPN', 'Python program becomes self aware'], str)
     172
     173        # Dump the current contents of the database as a JSON fixture
     174        self.assertEqual(management.call_command('dumpdata', 'fixtures', format='json', return_output=True), '[{"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"}}]')
     175
     176        # Load fixture 4 (compressed), using format discovery
     177        management.call_command('loaddata', 'fixture4', verbosity=0, commit=False)
     178        self.assertQuerysetEqual(Article.objects.all(), ['Django pets kitten', 'Time to reform copyright', 'Poker has no place on ESPN', 'Python program becomes self aware'], str)
     179 No newline at end of file
Back to Top