2010-08-23 15:04:41 +08:00
|
|
|
from django.core import management
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2011-11-27 21:00:56 +08:00
|
|
|
from .models import Article, Book
|
2010-08-23 15:04:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SampleTestCase(TestCase):
|
|
|
|
fixtures = ['fixture1.json', 'fixture2.json']
|
|
|
|
|
|
|
|
def testClassFixtures(self):
|
|
|
|
"Test cases can load fixture objects into models defined in packages"
|
2011-11-27 21:00:56 +08:00
|
|
|
self.assertEqual(Article.objects.count(), 3)
|
2010-08-23 15:04:41 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Article.objects.all(),[
|
2011-11-27 21:00:56 +08:00
|
|
|
u"Django conquers world!",
|
|
|
|
u"Copyright is fine the way it is",
|
|
|
|
u"Poker has no place on ESPN",
|
2010-08-23 15:04:41 +08:00
|
|
|
],
|
|
|
|
lambda a: a.headline
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-06-05 22:46:15 +08:00
|
|
|
class TestNoInitialDataLoading(TestCase):
|
|
|
|
def test_syncdb(self):
|
|
|
|
Book.objects.all().delete()
|
|
|
|
|
|
|
|
management.call_command(
|
|
|
|
'syncdb',
|
|
|
|
verbosity=0,
|
|
|
|
load_initial_data=False
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(Book.objects.all(), [])
|
|
|
|
|
|
|
|
def test_flush(self):
|
2012-06-06 19:53:12 +08:00
|
|
|
# Test presence of fixture (flush called by TransactionTestCase)
|
2012-06-05 22:46:15 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Book.objects.all(), [
|
|
|
|
u'Achieving self-awareness of Python programs'
|
|
|
|
],
|
|
|
|
lambda a: a.name
|
|
|
|
)
|
|
|
|
|
|
|
|
management.call_command(
|
|
|
|
'flush',
|
|
|
|
verbosity=0,
|
|
|
|
interactive=False,
|
|
|
|
load_initial_data=False
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(Book.objects.all(), [])
|
|
|
|
|
|
|
|
|
2010-08-23 15:04:41 +08:00
|
|
|
class FixtureTestCase(TestCase):
|
|
|
|
def test_initial_data(self):
|
|
|
|
"Fixtures can load initial data into models defined in packages"
|
2011-11-27 21:00:56 +08:00
|
|
|
# syncdb introduces 1 initial data object from initial_data.json
|
2010-08-23 15:04:41 +08:00
|
|
|
self.assertQuerysetEqual(
|
2011-11-27 21:00:56 +08:00
|
|
|
Book.objects.all(), [
|
|
|
|
u'Achieving self-awareness of Python programs'
|
2010-08-23 15:04:41 +08:00
|
|
|
],
|
2011-11-27 21:00:56 +08:00
|
|
|
lambda a: a.name
|
2010-08-23 15:04:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_loaddata(self):
|
|
|
|
"Fixtures can load data into models defined in packages"
|
|
|
|
# Load fixture 1. Single JSON file, with two objects
|
2010-09-11 03:16:43 +08:00
|
|
|
management.call_command("loaddata", "fixture1.json", verbosity=0, commit=False)
|
2010-08-23 15:04:41 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Article.objects.all(), [
|
2011-11-27 21:00:56 +08:00
|
|
|
u"Time to reform copyright",
|
|
|
|
u"Poker has no place on ESPN",
|
2010-08-23 15:04:41 +08:00
|
|
|
],
|
|
|
|
lambda a: a.headline,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Load fixture 2. JSON file imported by default. Overwrites some
|
|
|
|
# existing objects
|
2010-09-11 03:16:43 +08:00
|
|
|
management.call_command("loaddata", "fixture2.json", verbosity=0, commit=False)
|
2010-08-23 15:04:41 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Article.objects.all(), [
|
2011-11-27 21:00:56 +08:00
|
|
|
u"Django conquers world!",
|
|
|
|
u"Copyright is fine the way it is",
|
|
|
|
u"Poker has no place on ESPN",
|
2010-08-23 15:04:41 +08:00
|
|
|
],
|
|
|
|
lambda a: a.headline,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Load a fixture that doesn't exist
|
2010-09-11 03:16:43 +08:00
|
|
|
management.call_command("loaddata", "unknown.json", verbosity=0, commit=False)
|
2010-08-23 15:04:41 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Article.objects.all(), [
|
2011-11-27 21:00:56 +08:00
|
|
|
u"Django conquers world!",
|
|
|
|
u"Copyright is fine the way it is",
|
|
|
|
u"Poker has no place on ESPN",
|
2010-08-23 15:04:41 +08:00
|
|
|
],
|
|
|
|
lambda a: a.headline,
|
|
|
|
)
|