2007-03-01 13:11:08 +00:00
"""
2007-03-23 20:17:04 +00:00
37. Fixtures .
2007-03-01 13:11:08 +00:00
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
Fixtures are a way of loading data into the database in bulk . Fixure data
can be stored in any serializable format ( including JSON and XML ) . Fixtures
2007-03-01 13:11:08 +00:00
are identified by name , and are stored in either a directory named ' fixtures '
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
in the application directory , on in one of the directories named in the
2007-03-01 13:11:08 +00:00
FIXTURE_DIRS setting .
"""
from django . db import models
class Article ( models . Model ) :
2007-08-05 05:14:46 +00:00
headline = models . CharField ( max_length = 100 , default = ' Default headline ' )
2007-03-01 13:11:08 +00:00
pub_date = models . DateTimeField ( )
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
def __unicode__ ( self ) :
2007-03-01 13:11:08 +00:00
return self . headline
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
2007-03-01 13:11:08 +00:00
class Meta :
ordering = ( ' -pub_date ' , ' headline ' )
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
2007-03-01 13:11:08 +00:00
__test__ = { ' API_TESTS ' : """
>> > from django . core import management
>> > from django . db . models import get_app
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
# Reset the database representation of this app.
2007-03-01 13:11:08 +00:00
# This will return the database to a clean initial state.
2007-08-16 06:06:55 +00:00
>> > management . call_command ( ' flush ' , verbosity = 0 , interactive = False )
2007-03-01 13:11:08 +00:00
# Syncdb introduces 1 initial data object from initial_data.json.
>> > Article . objects . all ( )
[ < Article : Python program becomes self aware > ]
# Load fixture 1. Single JSON file, with two objects.
2007-08-16 06:06:55 +00:00
>> > management . call_command ( ' loaddata ' , ' fixture1.json ' , verbosity = 0 )
2007-03-01 13:11:08 +00:00
>> > Article . objects . all ( )
[ < Article : Time to reform copyright > , < Article : Poker has no place on ESPN > , < Article : Python program becomes self aware > ]
# Load fixture 2. JSON file imported by default. Overwrites some existing objects
2007-08-16 06:06:55 +00:00
>> > management . call_command ( ' loaddata ' , ' fixture2.json ' , verbosity = 0 )
2007-03-01 13:11:08 +00:00
>> > 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 > ]
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
# Load fixture 3, XML format.
2007-08-16 06:06:55 +00:00
>> > management . call_command ( ' loaddata ' , ' fixture3.xml ' , verbosity = 0 )
2007-03-01 13:11:08 +00:00
>> > Article . objects . all ( )
[ < 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 > ]
# Load a fixture that doesn't exist
2007-08-16 06:06:55 +00:00
>> > management . call_command ( ' loaddata ' , ' unknown.json ' , verbosity = 0 )
2007-03-01 13:11:08 +00:00
# object list is unaffected
>> > Article . objects . all ( )
[ < 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 > ]
# Reset the database representation of this app. This will delete all data.
2007-08-16 06:06:55 +00:00
>> > management . call_command ( ' flush ' , verbosity = 0 , interactive = False )
2007-03-01 13:11:08 +00:00
>> > Article . objects . all ( )
[ < Article : Python program becomes self aware > ]
# Load fixture 1 again, using format discovery
2007-08-16 06:06:55 +00:00
>> > management . call_command ( ' loaddata ' , ' fixture1 ' , verbosity = 0 )
2007-03-01 13:11:08 +00:00
>> > Article . objects . all ( )
[ < Article : Time to reform copyright > , < Article : Poker has no place on ESPN > , < Article : Python program becomes self aware > ]
# Try to load fixture 2 using format discovery; this will fail
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
# because there are two fixture2's in the fixtures directory
2007-08-16 06:06:55 +00:00
>> > management . call_command ( ' loaddata ' , ' fixture2 ' , verbosity = 0 ) # doctest: +ELLIPSIS
2007-03-13 00:33:15 +00:00
Multiple fixtures named ' fixture2 ' in ' ...fixtures ' . Aborting .
2007-03-01 13:11:08 +00:00
>> > Article . objects . all ( )
[ < Article : Time to reform copyright > , < Article : Poker has no place on ESPN > , < Article : Python program becomes self aware > ]
# Dump the current contents of the database as a JSON fixture
2007-08-17 14:02:40 +00:00
>> > management . call_command ( ' dumpdata ' , ' fixtures ' , format = ' json ' )
2007-09-15 05:32:29 +00:00
[ { " 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 " } } ]
2007-03-01 13:11:08 +00:00
""" }
from django . test import TestCase
class SampleTestCase ( TestCase ) :
fixtures = [ ' fixture1.json ' , ' fixture2.json ' ]
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
2007-03-01 13:11:08 +00:00
def testClassFixtures ( self ) :
" Check that test case has installed 4 fixture objects "
self . assertEqual ( Article . objects . count ( ) , 4 )
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>] " )