1
from django.core import management
2
from django.test import TestCase
4
from models import Article
7
class SampleTestCase(TestCase):
8
fixtures = ['fixture1.json', 'fixture2.json']
10
def testClassFixtures(self):
11
"Test cases can load fixture objects into models defined in packages"
12
self.assertEqual(Article.objects.count(), 4)
13
self.assertQuerysetEqual(
14
Article.objects.all(),[
15
"Django conquers world!",
16
"Copyright is fine the way it is",
17
"Poker has no place on ESPN",
18
"Python program becomes self aware"
24
class FixtureTestCase(TestCase):
25
def test_initial_data(self):
26
"Fixtures can load initial data into models defined in packages"
27
#Syncdb introduces 1 initial data object from initial_data.json
28
self.assertQuerysetEqual(
29
Article.objects.all(), [
30
"Python program becomes self aware"
35
def test_loaddata(self):
36
"Fixtures can load data into models defined in packages"
37
# Load fixture 1. Single JSON file, with two objects
38
management.call_command("loaddata", "fixture1.json", verbosity=0, commit=False)
39
self.assertQuerysetEqual(
40
Article.objects.all(), [
41
"Time to reform copyright",
42
"Poker has no place on ESPN",
43
"Python program becomes self aware",
48
# Load fixture 2. JSON file imported by default. Overwrites some
50
management.call_command("loaddata", "fixture2.json", verbosity=0, commit=False)
51
self.assertQuerysetEqual(
52
Article.objects.all(), [
53
"Django conquers world!",
54
"Copyright is fine the way it is",
55
"Poker has no place on ESPN",
56
"Python program becomes self aware",
61
# Load a fixture that doesn't exist
62
management.call_command("loaddata", "unknown.json", verbosity=0, commit=False)
63
self.assertQuerysetEqual(
64
Article.objects.all(), [
65
"Django conquers world!",
66
"Copyright is fine the way it is",
67
"Poker has no place on ESPN",
68
"Python program becomes self aware",