]> gitweb.fperrin.net Git - djsite.git/blob - quotes/tests.py
556e9252c3377477911b077da4e64315b8f067b9
[djsite.git] / quotes / tests.py
1 import sys
2 from django.test import TestCase, Client
3
4 # Create your tests here.
5 from .models import Author, Tag, Quote
6
7 class QuoteTest(TestCase):
8     def setUp(self):
9         a1 = Author.objects.create(name="JFK")
10         q1 = Quote.objects.create(text="Ich bin...", author=a1)
11
12     def test_one(self):
13         q = Quote.objects.filter(text__startswith="Ich")
14         self.assertEqual(q.count(), 1)
15         q = q[0]
16         self.assertEqual(q.author.name, "JFK")
17
18
19 class ViewsTest(TestCase):
20     def setUp(self):
21         a1 = Author.objects.create(name="Author with notes",
22                                    notes="Some notes")
23         t1 = Tag.objects.create(tag="tag01-1")
24         t2 = Tag.objects.create(tag="tag01-2")
25         q1 = Quote.objects.create(text="Quote01, two tags", author=a1)
26         q1.tags.add(t1, t2)
27
28         a2 = Author.objects.create(name="Author without notes")
29         q2= Quote.objects.create(text="Quote02, no tags", author=a2)
30
31     def test_views_all_data(self):
32         q = Quote.objects.filter(text__startswith="Quote01")
33         self.assertEqual(q.count(), 1)
34         q = q[0]
35
36         c = Client()
37         response = c.get('/quotes/show/%s/' % q.id)
38         self.assertEqual(response.status_code, 200)
39         self.assertTrue('Quote01, two tags' in response.content)
40         self.assertTrue("author_notes" in response.content)
41         self.assertEqual(response.content.count("tag_link"), 2)
42
43         response = c.get('/quotes/author/%s/' % q.author.id)
44         self.assertEqual(response.status_code, 200)
45         self.assertTrue('Quote01, two tags' in response.content)
46         self.assertTrue("author_notes" in response.content)
47
48         for tag in q.tags.all():
49             response = c.get('/quotes/tag/%s/' % tag.id)
50             self.assertEqual(response.status_code, 200)
51             self.assertTrue('Quote01, two tags' in response.content)
52
53     def test_views_minimal_data(self):
54         q = Quote.objects.filter(text__startswith="Quote02")
55         self.assertEqual(q.count(), 1)
56         q = q[0]
57
58         c = Client()
59         response = c.get('/quotes/show/%s/' % q.id)
60         self.assertEqual(response.status_code, 200)
61         self.assertTrue('Quote02' in response.content)
62         self.assertFalse("author_notes" in response.content)
63         self.assertEqual(response.content.count("tag_link"), 0)
64
65         response = c.get('/quotes/author/%s/' % q.author.id)
66         self.assertEqual(response.status_code, 200)
67         self.assertFalse('Quote01, two tags' in response.content)
68         self.assertTrue('Quote02' in response.content)