|
1 from django.core import management |
|
2 from django.test import TestCase |
|
3 |
|
4 from models import Article |
|
5 |
|
6 |
|
7 class SampleTestCase(TestCase): |
|
8 fixtures = ['fixture1.json', 'fixture2.json'] |
|
9 |
|
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" |
|
19 ], |
|
20 lambda a: a.headline |
|
21 ) |
|
22 |
|
23 |
|
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" |
|
31 ], |
|
32 lambda a: a.headline |
|
33 ) |
|
34 |
|
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", |
|
44 ], |
|
45 lambda a: a.headline, |
|
46 ) |
|
47 |
|
48 # Load fixture 2. JSON file imported by default. Overwrites some |
|
49 # existing objects |
|
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", |
|
57 ], |
|
58 lambda a: a.headline, |
|
59 ) |
|
60 |
|
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", |
|
69 ], |
|
70 lambda a: a.headline, |
|
71 ) |