Ticket #13636: fixtures-doctests-remove.diff

File fixtures-doctests-remove.diff, 51.1 KB (added by Eric Holscher, 14 years ago)
  • django/core/management/base.py

    commit eab20e90901ffa645534b869d3730eff90e9ba3e
    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)
  • 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..e9b9b3e
    - +  
     1import StringIO
     2import sys
     3
     4from django.test import TestCase, TransactionTestCase
     5from django.core import management
     6
     7from models import *
     8
     9class SampleTestCase(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.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>]")
     16
     17class AnotherTestCase(TestCase):
     18
     19    def test_initial_data(self):
     20        # Syncdb introduces 1 initial data object from initial_data.json.
     21        self.assertEqual([str(obj) for obj in Article.objects.all()], ['Python program becomes self aware'])
     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        # FIXME: Had to convert classes to strings. Ensure this looks right.
     28        self.assertEqual([str(obj) for obj in Article.objects.all()], ['Time to reform copyright', 'Poker has no place on ESPN', 'Python program becomes self aware'])
     29
     30        # Dump the current contents of the database as a JSON fixture
     31        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"}}]')
     32
     33        # Try just dumping the contents of fixtures.Category
     34        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"}}]')
     35
     36        # ...and just fixtures.Article
     37        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"}}]')
     38
     39        # ...and both
     40        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"}}]')
     41
     42        # Specify a specific model twice
     43        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"}}]')
     44
     45        # Specify a dump that specifies Article both explicitly and implicitly
     46        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"}}]')
     47
     48        # Same again, but specify in the reverse order
     49        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"}}]')
     50
     51        # Specify one model from one application, and an entire other application.
     52        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"}}]')
     53
     54        # Load fixture 2. JSON file imported by default. Overwrites some existing objects
     55        management.call_command('loaddata', 'fixture2.json', verbosity=0, commit=False)
     56        # FIXME: Had to convert classes to strings. Ensure this looks right.
     57        self.assertEqual([str(obj) for obj in Article.objects.all()], ['Django conquers world!', 'Copyright is fine the way it is', 'Poker has no place on ESPN', 'Python program becomes self aware'])
     58
     59        # Load fixture 3, XML format.
     60        management.call_command('loaddata', 'fixture3.xml', verbosity=0, commit=False)
     61        # FIXME: Had to convert classes to strings. Ensure this looks right.
     62        self.assertEqual([str(obj) for obj in 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'])
     63
     64        # Load fixture 6, JSON file with dynamic ContentType fields. Testing ManyToOne.
     65        management.call_command('loaddata', 'fixture6.json', verbosity=0, commit=False)
     66        # FIXME: Had to convert classes to strings. Ensure this looks right.
     67        self.assertEqual([str(obj) for obj in Tag.objects.all()], ['<Article: Copyright is fine the way it is> tagged "copyright"', '<Article: Copyright is fine the way it is> tagged "law"'])
     68
     69        # Load fixture 7, XML file with dynamic ContentType fields. Testing ManyToOne.
     70        management.call_command('loaddata', 'fixture7.xml', verbosity=0, commit=False)
     71        # FIXME: Had to convert classes to strings. Ensure this looks right.
     72        self.assertEqual([str(obj) for obj in 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"'])
     73
     74        # Load fixture 8, JSON file with dynamic Permission fields. Testing ManyToMany.
     75        management.call_command('loaddata', 'fixture8.json', verbosity=0, commit=False)
     76        # FIXME: Had to convert classes to strings. Ensure this looks right.
     77        self.assertEqual([str(obj) for obj in Visa.objects.all()], ['Django Reinhardt Can add user, Can change user, Can delete user', 'Stephane Grappelli Can add user', 'Prince '])
     78
     79        # Load fixture 9, XML file with dynamic Permission fields. Testing ManyToMany.
     80        management.call_command('loaddata', 'fixture9.xml', verbosity=0, commit=False)
     81        # FIXME: Had to convert classes to strings. Ensure this looks right.
     82        self.assertEqual([str(obj) for obj in 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'])
     83
     84        # FIXME: Had to convert classes to strings. Ensure this looks right.
     85        self.assertEqual([str(obj) for obj in Book.objects.all()], ['Music for all ages by Artist formerly known as "Prince" and Django Reinhardt'])
     86
     87        # Load a fixture that doesn't exist
     88        management.call_command('loaddata', 'unknown.json', verbosity=0, commit=False)
     89
     90        # object list is unaffected
     91        # FIXME: Had to convert classes to strings. Ensure this looks right.
     92        self.assertEqual([str(obj) for obj in 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'])
     93
     94        # By default, you get raw keys on dumpdata
     95        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]}}]')
     96
     97        # But you can get natural keys if you ask for them and they are available
     98        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"]]}}]')
     99
     100        # Dump the current contents of the database as a JSON fixture
     101        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"]]}}]')
     102
     103        # Dump the current contents of the database as an XML fixture
     104        self.assertEqual(management.call_command('dumpdata', 'fixtures', format='xml', use_natural_keys=True, return_output=True), """<?xml version="1.0" encoding="utf-8"?>
     105<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>""")
     106
     107
     108    def test_awesome(self):
     109        # Load fixture 4 (compressed), using format specification
     110        management.call_command('loaddata', 'fixture4.json', verbosity=0, commit=False)
     111        # FIXME: Had to convert classes to strings. Ensure this looks right.
     112        self.assertEqual([str(obj) for obj in Article.objects.all()], ['Django pets kitten', 'Python program becomes self aware'])
     113
     114    def test_more_awesome(self):
     115        # Load fixture 5 (compressed), using format *and* compression specification
     116        management.call_command('loaddata', 'fixture5.json.zip', verbosity=0, commit=False)
     117        # FIXME: Had to convert classes to strings. Ensure this looks right.
     118        self.assertEqual([str(obj) for obj in Article.objects.all()], ['WoW subscribers now outnumber readers', 'Python program becomes self aware'])
     119
     120    def test_most_awesome(self):
     121
     122        # Load fixture 5 (compressed), only compression specification
     123        management.call_command('loaddata', 'fixture5.zip', verbosity=0, commit=False)
     124        # FIXME: Had to convert classes to strings. Ensure this looks right.
     125        self.assertEqual([str(obj) for obj in Article.objects.all()], ['WoW subscribers now outnumber readers', 'Python program becomes self aware'])
     126
     127    def test_bojangling(self):
     128
     129        # Load db fixtures 1 and 2. These will load using the 'default' database identifier implicitly
     130        management.call_command('loaddata', 'db_fixture_1', verbosity=0, commit=False)
     131        management.call_command('loaddata', 'db_fixture_2', verbosity=0, commit=False)
     132        # FIXME: Had to convert classes to strings. Ensure this looks right.
     133        self.assertEqual([str(obj) for obj in Article.objects.all()], ['Who needs more than one database?', 'Who needs to use compressed data?', 'Python program becomes self aware'])
     134
     135    def test_zomgwtf(self):
     136
     137        # Load db fixtures 1 and 2. These will load using the 'default' database identifier explicitly
     138        management.call_command('loaddata', 'db_fixture_1', verbosity=0, using='default', commit=False)
     139        management.call_command('loaddata', 'db_fixture_2', verbosity=0, using='default', commit=False)
     140        # FIXME: Had to convert classes to strings. Ensure this looks right.
     141        self.assertEqual([str(obj) for obj in Article.objects.all()], ['Who needs more than one database?', 'Who needs to use compressed data?', 'Python program becomes self aware'])
     142
     143    def test_no_more_flushing(self):
     144
     145        # Try to load db fixture 3. This won't load because the database identifier doesn't match
     146        management.call_command('loaddata', 'db_fixture_3', verbosity=0, commit=False)
     147        # FIXME: Had to convert classes to strings. Ensure this looks right.
     148        self.assertEqual([str(obj) for obj in Article.objects.all()], ['Python program becomes self aware'])
     149
     150        management.call_command('loaddata', 'db_fixture_3', verbosity=0, using='default', commit=False)
     151        # FIXME: Had to convert classes to strings. Ensure this looks right.
     152        self.assertEqual([str(obj) for obj in Article.objects.all()], ['Python program becomes self aware'])
     153
     154    def test_down_with_flushing(self):
     155
     156        # Load back in fixture 1, we need the articles from it
     157        management.call_command('loaddata', 'fixture1', verbosity=0, commit=False)
     158
     159        # Try to load fixture 6 using format discovery
     160        management.call_command('loaddata', 'fixture6', verbosity=0, commit=False)
     161        # FIXME: Had to convert classes to strings. Ensure this looks right.
     162        self.assertEqual([str(obj) for obj in Tag.objects.all()], ['<Article: Time to reform copyright> tagged "copyright"', '<Article: Time to reform copyright> tagged "law"'])
     163
     164        # Dump the current contents of the database as a JSON fixture
     165        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"}}]')
     166
     167        # Dump the current contents of the database as an XML fixture
     168        self.assertEqual(management.call_command('dumpdata', 'fixtures', format='xml', use_natural_keys=True, return_output=True), """<?xml version="1.0" encoding="utf-8"?>
     169<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>""")
     170
     171class TransTests(TransactionTestCase):
     172
     173    def test_format_discovery(self):
     174
     175        # Load fixture 1 again, using format discovery
     176        management.call_command('loaddata', 'fixture1', verbosity=0, commit=False)
     177        self.assertEqual([str(obj) for obj in Article.objects.all()], ['Time to reform copyright', 'Poker has no place on ESPN', 'Python program becomes self aware'])
     178
     179        # Try to load fixture 2 using format discovery; this will fail
     180        # because there are two fixture2's in the fixtures directory
     181        old_io = sys.stdout
     182        new_io = StringIO.StringIO()
     183        sys.stdout = new_io
     184        management.call_command('loaddata', 'fixture2', verbosity=0)
     185        output = new_io.getvalue().strip()
     186        self.assertTrue("Multiple fixtures named 'fixture2'" in output)
     187        sys.stdout = old_io
     188
     189        # object list is unaffected
     190        self.assertEqual([str(obj) for obj in Article.objects.all()], ['Time to reform copyright', 'Poker has no place on ESPN', 'Python program becomes self aware'])
     191
     192        # Dump the current contents of the database as a JSON fixture
     193        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"}}]')
     194
     195        # Load fixture 4 (compressed), using format discovery
     196        management.call_command('loaddata', 'fixture4', verbosity=0, commit=False)
     197        # FIXME: Had to convert classes to strings. Ensure this looks right.
     198        self.assertEqual([str(obj) for obj in Article.objects.all()], ['Django pets kitten', 'Time to reform copyright', 'Poker has no place on ESPN', 'Python program becomes self aware'])
     199 No newline at end of file
Back to Top