2013-07-29 17:19:04 +00:00
from __future__ import unicode_literals
2011-10-13 18:04:12 +00:00
2014-03-23 05:10:12 +00:00
import os
2013-05-19 09:20:10 +00:00
import warnings
2011-03-09 23:46:28 +00:00
from django . contrib . sites . models import Site
2010-06-05 05:26:04 +00:00
from django . core import management
2012-06-07 08:31:08 +00:00
from django . db import connection , IntegrityError
2010-10-11 12:55:17 +00:00
from django . test import TestCase , TransactionTestCase , skipUnlessDBFeature
2014-10-14 21:15:43 +00:00
from django . utils . encoding import force_text
2012-09-07 17:17:09 +00:00
from django . utils import six
2010-06-05 05:26:04 +00:00
2011-10-13 18:04:12 +00:00
from . models import Article , Book , Spy , Tag , Visa
2010-06-05 05:26:04 +00:00
2010-10-11 12:55:17 +00:00
2010-06-05 05:26:04 +00:00
class TestCaseFixtureLoadingTests ( TestCase ) :
fixtures = [ ' fixture1.json ' , ' fixture2.json ' ]
def testClassFixtures ( self ) :
2011-11-27 13:00:56 +00:00
" Check that test case has installed 3 fixture objects "
self . assertEqual ( Article . objects . count ( ) , 3 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Article . objects . all ( ) , [
' <Article: Django conquers world!> ' ,
' <Article: Copyright is fine the way it is> ' ,
' <Article: Poker has no place on ESPN> ' ,
] )
2012-09-07 17:17:09 +00:00
2013-09-11 13:20:15 +00:00
class SubclassTestCaseFixtureLoadingTests ( TestCaseFixtureLoadingTests ) :
"""
Make sure that subclasses can remove fixtures from parent class ( # 2 1 0 8 9 ) .
"""
fixtures = [ ]
def testClassFixtures ( self ) :
" Check that there were no fixture objects installed "
self . assertEqual ( Article . objects . count ( ) , 0 )
2012-09-28 07:20:01 +00:00
class DumpDataAssertMixin ( object ) :
2010-06-05 05:26:04 +00:00
2014-03-23 05:10:12 +00:00
def _dumpdata_assert ( self , args , output , format = ' json ' , filename = None ,
2012-08-01 01:49:01 +00:00
natural_foreign_keys = False , natural_primary_keys = False ,
2013-05-29 17:47:49 +00:00
use_base_manager = False , exclude_list = [ ] , primary_keys = ' ' ) :
2012-09-07 17:17:09 +00:00
new_io = six . StringIO ( )
management . call_command ( ' dumpdata ' , * args , * * { ' format ' : format ,
' stdout ' : new_io ,
' stderr ' : new_io ,
2014-03-23 05:10:12 +00:00
' output ' : filename ,
2012-08-01 01:49:01 +00:00
' use_natural_foreign_keys ' : natural_foreign_keys ,
' use_natural_primary_keys ' : natural_primary_keys ,
2012-09-07 17:17:09 +00:00
' use_base_manager ' : use_base_manager ,
2013-05-19 10:39:14 +00:00
' exclude ' : exclude_list ,
' primary_keys ' : primary_keys } )
2014-03-23 05:10:12 +00:00
if filename :
2014-03-30 18:50:18 +00:00
with open ( filename , " r " ) as f :
command_output = f . read ( )
2014-03-23 05:10:12 +00:00
os . remove ( filename )
else :
command_output = new_io . getvalue ( ) . strip ( )
2012-09-28 07:20:01 +00:00
if format == " json " :
self . assertJSONEqual ( command_output , output )
elif format == " xml " :
self . assertXMLEqual ( command_output , output )
else :
self . assertEqual ( command_output , output )
class FixtureLoadingTests ( DumpDataAssertMixin , TestCase ) :
2010-06-05 05:26:04 +00:00
def test_initial_data ( self ) :
2013-09-03 15:51:34 +00:00
# migrate introduces 1 initial data object from initial_data.json.
2014-07-26 17:15:54 +00:00
# this behavior is deprecated and will be removed in Django 1.9
2011-11-27 13:00:56 +00:00
self . assertQuerysetEqual ( Book . objects . all ( ) , [
' <Book: Achieving self-awareness of Python programs> '
2010-06-05 05:26:04 +00:00
] )
def test_loading_and_dumping ( self ) :
2011-03-09 23:46:28 +00:00
Site . objects . all ( ) . delete ( )
2010-06-05 05:26:04 +00:00
# Load fixture 1. Single JSON file, with two objects.
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture1.json ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Article . objects . all ( ) , [
' <Article: Time to reform copyright> ' ,
' <Article: Poker has no place on ESPN> ' ,
] )
# Dump the current contents of the database as a JSON fixture
2012-05-26 09:43:37 +00:00
self . _dumpdata_assert ( [ ' fixtures ' ] , ' [ { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}, { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Time to reform copyright " , " pub_date " : " 2006-06-16T13:00:00 " }}, { " pk " : 10, " model " : " fixtures.book " , " fields " : { " name " : " Achieving self-awareness of Python programs " , " authors " : []}}] ' )
2010-06-05 05:26:04 +00:00
# Try just dumping the contents of fixtures.Category
self . _dumpdata_assert ( [ ' fixtures.Category ' ] , ' [ { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}] ' )
# ...and just fixtures.Article
2012-05-26 09:43:37 +00:00
self . _dumpdata_assert ( [ ' fixtures.Article ' ] , ' [ { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Time to reform copyright " , " pub_date " : " 2006-06-16T13:00:00 " }}] ' )
2010-06-05 05:26:04 +00:00
# ...and both
2012-05-26 09:43:37 +00:00
self . _dumpdata_assert ( [ ' fixtures.Category ' , ' fixtures.Article ' ] , ' [ { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}, { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Time to reform copyright " , " pub_date " : " 2006-06-16T13:00:00 " }}] ' )
2010-06-05 05:26:04 +00:00
# Specify a specific model twice
2012-05-26 09:43:37 +00:00
self . _dumpdata_assert ( [ ' fixtures.Article ' , ' fixtures.Article ' ] , ' [ { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Time to reform copyright " , " pub_date " : " 2006-06-16T13:00:00 " }}] ' )
2010-06-05 05:26:04 +00:00
# Specify a dump that specifies Article both explicitly and implicitly
2012-05-26 09:43:37 +00:00
self . _dumpdata_assert ( [ ' fixtures.Article ' , ' fixtures ' ] , ' [ { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}, { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Time to reform copyright " , " pub_date " : " 2006-06-16T13:00:00 " }}, { " pk " : 10, " model " : " fixtures.book " , " fields " : { " name " : " Achieving self-awareness of Python programs " , " authors " : []}}] ' )
2010-06-05 05:26:04 +00:00
2014-02-11 14:59:57 +00:00
# Specify a dump that specifies Article both explicitly and implicitly,
# but lists the app first (#22025).
self . _dumpdata_assert ( [ ' fixtures ' , ' fixtures.Article ' ] , ' [ { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}, { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Time to reform copyright " , " pub_date " : " 2006-06-16T13:00:00 " }}, { " pk " : 10, " model " : " fixtures.book " , " fields " : { " name " : " Achieving self-awareness of Python programs " , " authors " : []}}] ' )
2010-06-05 05:26:04 +00:00
# Same again, but specify in the reverse order
2012-05-26 09:43:37 +00:00
self . _dumpdata_assert ( [ ' fixtures ' ] , ' [ { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}, { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Time to reform copyright " , " pub_date " : " 2006-06-16T13:00:00 " }}, { " pk " : 10, " model " : " fixtures.book " , " fields " : { " name " : " Achieving self-awareness of Python programs " , " authors " : []}}] ' )
2010-06-05 05:26:04 +00:00
# Specify one model from one application, and an entire other application.
self . _dumpdata_assert ( [ ' fixtures.Category ' , ' sites ' ] , ' [ { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}, { " pk " : 1, " model " : " sites.site " , " fields " : { " domain " : " example.com " , " name " : " example.com " }}] ' )
# Load fixture 2. JSON file imported by default. Overwrites some existing objects
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture2.json ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Article . objects . all ( ) , [
' <Article: Django conquers world!> ' ,
' <Article: Copyright is fine the way it is> ' ,
' <Article: Poker has no place on ESPN> ' ,
] )
# Load fixture 3, XML format.
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture3.xml ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( 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!> ' ,
] )
# Load fixture 6, JSON file with dynamic ContentType fields. Testing ManyToOne.
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture6.json ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Tag . objects . all ( ) , [
' <Tag: <Article: Copyright is fine the way it is> tagged " copyright " > ' ,
2012-12-13 11:33:11 +00:00
' <Tag: <Article: Copyright is fine the way it is> tagged " law " > ' ,
] , ordered = False )
2010-06-05 05:26:04 +00:00
# Load fixture 7, XML file with dynamic ContentType fields. Testing ManyToOne.
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture7.xml ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Tag . objects . all ( ) , [
' <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 " > ' ,
2012-12-13 11:33:11 +00:00
' <Tag: <Article: Django conquers world!> tagged " world domination " > ' ,
] , ordered = False )
2010-06-05 05:26:04 +00:00
# Load fixture 8, JSON file with dynamic Permission fields. Testing ManyToMany.
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture8.json ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Visa . objects . all ( ) , [
' <Visa: Django Reinhardt Can add user, Can change user, Can delete user> ' ,
' <Visa: Stephane Grappelli Can add user> ' ,
' <Visa: Prince > '
2012-12-13 11:33:11 +00:00
] , ordered = False )
2010-06-05 05:26:04 +00:00
# Load fixture 9, XML file with dynamic Permission fields. Testing ManyToMany.
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture9.xml ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Visa . objects . all ( ) , [
' <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> '
2012-12-13 11:33:11 +00:00
] , ordered = False )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Book . objects . all ( ) , [
2011-11-27 13:00:56 +00:00
' <Book: Achieving self-awareness of Python programs> ' ,
2010-06-05 05:26:04 +00:00
' <Book: Music for all ages by Artist formerly known as " Prince " and Django Reinhardt> '
] )
# object list is unaffected
self . assertQuerysetEqual ( 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!> ' ,
] )
# By default, you get raw keys on dumpdata
2012-05-26 09:43:37 +00:00
self . _dumpdata_assert ( [ ' fixtures.book ' ] , ' [ { " pk " : 1, " model " : " fixtures.book " , " fields " : { " name " : " Music for all ages " , " authors " : [3, 1]}}, { " pk " : 10, " model " : " fixtures.book " , " fields " : { " name " : " Achieving self-awareness of Python programs " , " authors " : []}}] ' )
2010-06-05 05:26:04 +00:00
# But you can get natural keys if you ask for them and they are available
2012-08-01 01:49:01 +00:00
self . _dumpdata_assert ( [ ' fixtures.book ' ] , ' [ { " pk " : 1, " model " : " fixtures.book " , " fields " : { " name " : " Music for all ages " , " authors " : [[ " Artist formerly known as \\ " Prince \\ " " ], [ " Django Reinhardt " ]]}}, { " pk " : 10, " model " : " fixtures.book " , " fields " : { " name " : " Achieving self-awareness of Python programs " , " authors " : []}}] ' , natural_foreign_keys = True )
# You can also omit the primary keys for models that we can get later with natural keys.
self . _dumpdata_assert ( [ ' fixtures.person ' ] , ' [ { " fields " : { " name " : " Django Reinhardt " }, " model " : " fixtures.person " }, { " fields " : { " name " : " Stephane Grappelli " }, " model " : " fixtures.person " }, { " fields " : { " name " : " Artist formerly known as \\ " Prince \\ " " }, " model " : " fixtures.person " }] ' , natural_primary_keys = True )
2010-06-05 05:26:04 +00:00
# Dump the current contents of the database as a JSON fixture
2012-08-01 01:49:01 +00:00
self . _dumpdata_assert ( [ ' fixtures ' ] , ' [ { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}, { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker on TV is great! " , " pub_date " : " 2006-06-16T11:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Copyright is fine the way it is " , " pub_date " : " 2006-06-16T14:00:00 " }}, { " pk " : 4, " model " : " fixtures.article " , " fields " : { " headline " : " Django conquers world! " , " pub_date " : " 2006-06-16T15:00:00 " }}, { " pk " : 5, " model " : " fixtures.article " , " fields " : { " headline " : " XML identified as leading cause of cancer " , " pub_date " : " 2006-06-16T16: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 " : 1, " model " : " fixtures.person " , " fields " : { " name " : " Django Reinhardt " }}, { " pk " : 2, " model " : " fixtures.person " , " fields " : { " name " : " Stephane Grappelli " }}, { " pk " : 3, " model " : " fixtures.person " , " fields " : { " name " : " Artist formerly known as \\ " Prince \\ " " }}, { " 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 " ]]}}, { " pk " : 10, " model " : " fixtures.book " , " fields " : { " name " : " Achieving self-awareness of Python programs " , " authors " : []}}] ' , natural_foreign_keys = True )
2010-06-05 05:26:04 +00:00
# Dump the current contents of the database as an XML fixture
self . _dumpdata_assert ( [ ' fixtures ' ] , """ <?xml version= " 1.0 " encoding= " utf-8 " ?>
2012-08-01 01:49:01 +00:00
< 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 = " 2 " model = " fixtures.article " > < field type = " CharField " name = " headline " > Poker on TV is great ! < / field > < field type = " DateTimeField " name = " pub_date " > 2006 - 06 - 16 T11 : 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 T14 : 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 T15 : 00 : 00 < / 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 T16 : 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 = " 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 = " 3 " model = " fixtures.person " > < field type = " CharField " name = " name " > Artist formerly known as " Prince " < / 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 > < object pk = " 10 " model = " fixtures.
2010-06-05 05:26:04 +00:00
2010-08-06 16:48:07 +00:00
def test_dumpdata_with_excludes ( self ) :
# Load fixture1 which has a site, two articles, and a category
2011-03-09 23:46:28 +00:00
Site . objects . all ( ) . delete ( )
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture1.json ' , verbosity = 0 )
2010-08-06 16:48:07 +00:00
# Excluding fixtures app should only leave sites
self . _dumpdata_assert (
[ ' sites ' , ' fixtures ' ] ,
' [ { " pk " : 1, " model " : " sites.site " , " fields " : { " domain " : " example.com " , " name " : " example.com " }}] ' ,
exclude_list = [ ' fixtures ' ] )
2011-11-27 13:00:56 +00:00
# Excluding fixtures.Article/Book should leave fixtures.Category
2010-08-06 16:48:07 +00:00
self . _dumpdata_assert (
[ ' sites ' , ' fixtures ' ] ,
' [ { " pk " : 1, " model " : " sites.site " , " fields " : { " domain " : " example.com " , " name " : " example.com " }}, { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}] ' ,
2011-11-27 13:00:56 +00:00
exclude_list = [ ' fixtures.Article ' , ' fixtures.Book ' ] )
2010-08-06 16:48:07 +00:00
2011-11-27 13:00:56 +00:00
# Excluding fixtures and fixtures.Article/Book should be a no-op
2010-08-06 16:48:07 +00:00
self . _dumpdata_assert (
[ ' sites ' , ' fixtures ' ] ,
' [ { " pk " : 1, " model " : " sites.site " , " fields " : { " domain " : " example.com " , " name " : " example.com " }}, { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}] ' ,
2011-11-27 13:00:56 +00:00
exclude_list = [ ' fixtures.Article ' , ' fixtures.Book ' ] )
2010-08-06 16:48:07 +00:00
2011-11-27 13:00:56 +00:00
# Excluding sites and fixtures.Article/Book should only leave fixtures.Category
2010-08-06 16:48:07 +00:00
self . _dumpdata_assert (
[ ' sites ' , ' fixtures ' ] ,
' [ { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}] ' ,
2011-11-27 13:00:56 +00:00
exclude_list = [ ' fixtures.Article ' , ' fixtures.Book ' , ' sites ' ] )
2010-08-06 16:48:07 +00:00
# Excluding a bogus app should throw an error
2012-09-07 17:17:09 +00:00
with six . assertRaisesRegex ( self , management . CommandError ,
2012-06-07 12:36:54 +00:00
" Unknown app in excludes: foo_app " ) :
self . _dumpdata_assert ( [ ' fixtures ' , ' sites ' ] , ' ' , exclude_list = [ ' foo_app ' ] )
2010-08-06 16:48:07 +00:00
# Excluding a bogus model should throw an error
2012-09-07 17:17:09 +00:00
with six . assertRaisesRegex ( self , management . CommandError ,
2012-06-07 12:36:54 +00:00
" Unknown model in excludes: fixtures.FooModel " ) :
self . _dumpdata_assert ( [ ' fixtures ' , ' sites ' ] , ' ' , exclude_list = [ ' fixtures.FooModel ' ] )
2010-08-06 16:48:07 +00:00
2010-08-30 11:58:26 +00:00
def test_dumpdata_with_filtering_manager ( self ) :
2011-03-09 23:46:28 +00:00
spy1 = Spy . objects . create ( name = ' Paul ' )
spy2 = Spy . objects . create ( name = ' Alex ' , cover_blown = True )
2010-08-30 11:58:26 +00:00
self . assertQuerysetEqual ( Spy . objects . all ( ) ,
[ ' <Spy: Paul> ' ] )
# Use the default manager
2012-09-07 17:17:09 +00:00
self . _dumpdata_assert ( [ ' fixtures.Spy ' ] , ' [ { " pk " : %d , " model " : " fixtures.spy " , " fields " : { " cover_blown " : false}}] ' % spy1 . pk )
2010-08-30 11:58:26 +00:00
# Dump using Django's base manager. Should return all objects,
# even those normally filtered by the manager
2011-03-09 23:46:28 +00:00
self . _dumpdata_assert ( [ ' fixtures.Spy ' ] , ' [ { " pk " : %d , " model " : " fixtures.spy " , " fields " : { " cover_blown " : true}}, { " pk " : %d , " model " : " fixtures.spy " , " fields " : { " cover_blown " : false}}] ' % ( spy2 . pk , spy1 . pk ) , use_base_manager = True )
2010-08-30 11:58:26 +00:00
2013-05-19 10:39:14 +00:00
def test_dumpdata_with_pks ( self ) :
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture1.json ' , verbosity = 0 )
management . call_command ( ' loaddata ' , ' fixture2.json ' , verbosity = 0 )
2013-05-19 10:39:14 +00:00
self . _dumpdata_assert (
[ ' fixtures.Article ' ] ,
' [ { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Copyright is fine the way it is " , " pub_date " : " 2006-06-16T14:00:00 " }}] ' ,
primary_keys = ' 2,3 '
)
self . _dumpdata_assert (
[ ' fixtures.Article ' ] ,
' [ { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}] ' ,
primary_keys = ' 2 '
)
with six . assertRaisesRegex ( self , management . CommandError ,
" You can only use --pks option with one model " ) :
self . _dumpdata_assert (
[ ' fixtures ' ] ,
' [ { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Copyright is fine the way it is " , " pub_date " : " 2006-06-16T14:00:00 " }}] ' ,
primary_keys = ' 2,3 '
)
with six . assertRaisesRegex ( self , management . CommandError ,
" You can only use --pks option with one model " ) :
self . _dumpdata_assert (
' ' ,
' [ { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Copyright is fine the way it is " , " pub_date " : " 2006-06-16T14:00:00 " }}] ' ,
primary_keys = ' 2,3 '
)
with six . assertRaisesRegex ( self , management . CommandError ,
" You can only use --pks option with one model " ) :
self . _dumpdata_assert (
[ ' fixtures.Article ' , ' fixtures.category ' ] ,
' [ { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Copyright is fine the way it is " , " pub_date " : " 2006-06-16T14:00:00 " }}] ' ,
primary_keys = ' 2,3 '
)
2014-03-23 05:10:12 +00:00
def test_dumpdata_with_file_output ( self ) :
management . call_command ( ' loaddata ' , ' fixture1.json ' , verbosity = 0 )
self . _dumpdata_assert ( [ ' fixtures ' ] , ' [ { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}, { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Time to reform copyright " , " pub_date " : " 2006-06-16T13:00:00 " }}, { " pk " : 10, " model " : " fixtures.book " , " fields " : { " name " : " Achieving self-awareness of Python programs " , " authors " : []}}] ' ,
filename = ' dumpdata.json ' )
2010-06-05 05:26:04 +00:00
def test_compress_format_loading ( self ) :
# Load fixture 4 (compressed), using format specification
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture4.json ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Article . objects . all ( ) , [
' <Article: Django pets kitten> ' ,
] )
def test_compressed_specified_loading ( self ) :
# Load fixture 5 (compressed), using format *and* compression specification
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture5.json.zip ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Article . objects . all ( ) , [
' <Article: WoW subscribers now outnumber readers> ' ,
] )
def test_compressed_loading ( self ) :
# Load fixture 5 (compressed), only compression specification
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture5.zip ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Article . objects . all ( ) , [
' <Article: WoW subscribers now outnumber readers> ' ,
] )
def test_ambiguous_compressed_fixture ( self ) :
2014-03-02 14:25:53 +00:00
# The name "fixture5" is ambiguous, so loading it will raise an error
2012-12-08 10:13:52 +00:00
with self . assertRaises ( management . CommandError ) as cm :
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture5 ' , verbosity = 0 )
2012-12-08 10:13:52 +00:00
self . assertIn ( " Multiple fixtures named ' fixture5 ' " , cm . exception . args [ 0 ] )
2010-06-05 05:26:04 +00:00
def test_db_loading ( self ) :
# Load db fixtures 1 and 2. These will load using the 'default' database identifier implicitly
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' db_fixture_1 ' , verbosity = 0 )
management . call_command ( ' loaddata ' , ' db_fixture_2 ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Article . objects . all ( ) , [
' <Article: Who needs more than one database?> ' ,
' <Article: Who needs to use compressed data?> ' ,
] )
2011-09-22 04:30:20 +00:00
def test_loaddata_error_message ( self ) :
"""
Verifies that loading a fixture which contains an invalid object
outputs an error message which contains the pk of the object
that triggered the error .
"""
2011-10-16 14:30:43 +00:00
# MySQL needs a little prodding to reject invalid data.
# This won't affect other tests because the database connection
# is closed at the end of each test.
if connection . vendor == ' mysql ' :
connection . cursor ( ) . execute ( " SET sql_mode = ' TRADITIONAL ' " )
2012-12-08 10:13:52 +00:00
with self . assertRaises ( IntegrityError ) as cm :
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' invalid.json ' , verbosity = 0 )
2012-12-08 10:13:52 +00:00
self . assertIn ( " Could not load fixtures.Article(pk=1): " , cm . exception . args [ 0 ] )
2011-09-22 04:30:20 +00:00
2014-02-09 12:22:29 +00:00
def test_loaddata_app_option ( self ) :
"""
Verifies that the - - app option works .
"""
2014-03-09 20:37:05 +00:00
with warnings . catch_warnings ( ) :
# Ignore: No fixture named ...
warnings . filterwarnings ( " ignore " , category = UserWarning )
management . call_command ( ' loaddata ' , ' db_fixture_1 ' , verbosity = 0 , app_label = " someotherapp " )
2014-02-09 12:22:29 +00:00
self . assertQuerysetEqual ( Article . objects . all ( ) , [ ] )
management . call_command ( ' loaddata ' , ' db_fixture_1 ' , verbosity = 0 , app_label = " fixtures " )
self . assertQuerysetEqual ( Article . objects . all ( ) , [
' <Article: Who needs more than one database?> ' ,
] )
2010-06-05 05:26:04 +00:00
def test_loading_using ( self ) :
# Load db fixtures 1 and 2. These will load using the 'default' database identifier explicitly
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' db_fixture_1 ' , verbosity = 0 , using = ' default ' )
management . call_command ( ' loaddata ' , ' db_fixture_2 ' , verbosity = 0 , using = ' default ' )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Article . objects . all ( ) , [
' <Article: Who needs more than one database?> ' ,
' <Article: Who needs to use compressed data?> ' ,
] )
def test_unmatched_identifier_loading ( self ) :
# Try to load db fixture 3. This won't load because the database identifier doesn't match
2014-03-09 20:37:05 +00:00
with warnings . catch_warnings ( ) :
warnings . filterwarnings ( " ignore " , category = UserWarning )
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' db_fixture_3 ' , verbosity = 0 )
management . call_command ( ' loaddata ' , ' db_fixture_3 ' , verbosity = 0 , using = ' default ' )
2011-11-27 13:00:56 +00:00
self . assertQuerysetEqual ( Article . objects . all ( ) , [ ] )
2010-06-05 05:26:04 +00:00
def test_output_formats ( self ) :
# Load back in fixture 1, we need the articles from it
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture1 ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
# Try to load fixture 6 using format discovery
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture6 ' , verbosity = 0 )
2010-06-05 05:26:04 +00:00
self . assertQuerysetEqual ( Tag . objects . all ( ) , [
' <Tag: <Article: Time to reform copyright> tagged " copyright " > ' ,
' <Tag: <Article: Time to reform copyright> tagged " law " > '
2012-12-13 11:33:11 +00:00
] , ordered = False )
2010-06-05 05:26:04 +00:00
# Dump the current contents of the database as a JSON fixture
2012-08-01 01:49:01 +00:00
self . _dumpdata_assert ( [ ' fixtures ' ] , ' [ { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}, { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Time to reform copyright " , " pub_date " : " 2006-06-16T13: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 " : 2, " model " : " fixtures.person " , " fields " : { " name " : " Stephane Grappelli " }}, { " pk " : 3, " model " : " fixtures.person " , " fields " : { " name " : " Prince " }}, { " pk " : 10, " model " : " fixtures.book " , " fields " : { " name " : " Achieving self-awareness of Python programs " , " authors " : []}}] ' , natural_foreign_keys = True )
2010-06-05 05:26:04 +00:00
# Dump the current contents of the database as an XML fixture
self . _dumpdata_assert ( [ ' fixtures ' ] , """ <?xml version= " 1.0 " encoding= " utf-8 " ?>
2012-08-01 01:49:01 +00:00
< 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 = " 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 T12 : 00 : 00 < / 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 T13 : 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 = " 2 " model = " fixtures.person " > < field type = " CharField " name = " name " > Stephane Grappelli < / field > < / object > < object pk = " 3 " model = " fixtures.person " > < field type = " CharField " name = " name " > Prince < / field > < / object > < object pk = " 10 " model = " fixtures.book " > < field type = " CharField " name = " name " > Achieving self - awareness of Python programs < / field > < field to = " fixtures.person " name = " authors " rel = " ManyToManyRel " > < / field > < / object > < / django - objects > """ , format= ' xml ' , natural_foreign_keys=True)
2010-06-05 05:26:04 +00:00
2012-09-07 17:17:09 +00:00
2014-10-14 21:15:43 +00:00
class NonExistentFixtureTests ( TestCase ) :
"""
Custom class to limit fixture dirs .
"""
available_apps = [ ' django.contrib.auth ' , ' django.contrib.contenttypes ' ]
def test_loaddata_not_existent_fixture_file ( self ) :
stdout_output = six . StringIO ( )
with warnings . catch_warnings ( record = True ) as w :
warnings . simplefilter ( " always " )
# With verbosity=2, we get both stdout output and a warning
management . call_command (
' loaddata ' ,
' this_fixture_doesnt_exist ' ,
verbosity = 2 ,
stdout = stdout_output ,
)
self . assertIn ( " No fixture ' this_fixture_doesnt_exist ' in " ,
force_text ( stdout_output . getvalue ( ) ) )
self . assertEqual ( len ( w ) , 1 )
self . assertEqual ( force_text ( w [ 0 ] . message ) ,
" No fixture named ' this_fixture_doesnt_exist ' found. " )
# An attempt to load a non-existent 'initial_data' fixture doesn't produce any warning
with warnings . catch_warnings ( record = True ) as w :
management . call_command ( ' loaddata ' , ' initial_data.json ' , verbosity = 0 )
self . assertEqual ( len ( w ) , 0 )
2012-09-28 07:20:01 +00:00
class FixtureTransactionTests ( DumpDataAssertMixin , TransactionTestCase ) :
2010-10-11 12:55:17 +00:00
2013-06-04 06:09:29 +00:00
available_apps = [
' fixtures ' ,
' django.contrib.contenttypes ' ,
' django.contrib.auth ' ,
' django.contrib.sites ' ,
]
2010-10-11 12:55:17 +00:00
@skipUnlessDBFeature ( ' supports_forward_references ' )
def test_format_discovery ( self ) :
# Load fixture 1 again, using format discovery
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture1 ' , verbosity = 0 )
2010-10-11 12:55:17 +00:00
self . assertQuerysetEqual ( Article . objects . all ( ) , [
' <Article: Time to reform copyright> ' ,
' <Article: Poker has no place on ESPN> ' ,
] )
# Try to load fixture 2 using format discovery; this will fail
# because there are two fixture2's in the fixtures directory
2012-12-08 10:13:52 +00:00
with self . assertRaises ( management . CommandError ) as cm :
2012-06-07 08:31:08 +00:00
management . call_command ( ' loaddata ' , ' fixture2 ' , verbosity = 0 )
2012-12-08 10:13:52 +00:00
self . assertIn ( " Multiple fixtures named ' fixture2 ' " , cm . exception . args [ 0 ] )
2010-10-11 12:55:17 +00:00
# object list is unaffected
self . assertQuerysetEqual ( Article . objects . all ( ) , [
' <Article: Time to reform copyright> ' ,
' <Article: Poker has no place on ESPN> ' ,
] )
# Dump the current contents of the database as a JSON fixture
2012-05-26 09:43:37 +00:00
self . _dumpdata_assert ( [ ' fixtures ' ] , ' [ { " pk " : 1, " model " : " fixtures.category " , " fields " : { " description " : " Latest news stories " , " title " : " News Stories " }}, { " pk " : 2, " model " : " fixtures.article " , " fields " : { " headline " : " Poker has no place on ESPN " , " pub_date " : " 2006-06-16T12:00:00 " }}, { " pk " : 3, " model " : " fixtures.article " , " fields " : { " headline " : " Time to reform copyright " , " pub_date " : " 2006-06-16T13:00:00 " }}, { " pk " : 10, " model " : " fixtures.book " , " fields " : { " name " : " Achieving self-awareness of Python programs " , " authors " : []}}] ' )
2010-10-11 12:55:17 +00:00
# Load fixture 4 (compressed), using format discovery
2013-06-30 12:17:33 +00:00
management . call_command ( ' loaddata ' , ' fixture4 ' , verbosity = 0 )
2010-10-11 12:55:17 +00:00
self . assertQuerysetEqual ( Article . objects . all ( ) , [
' <Article: Django pets kitten> ' ,
' <Article: Time to reform copyright> ' ,
' <Article: Poker has no place on ESPN> ' ,
] )