2 from django.test import TestCase, Client
4 # Create your tests here.
5 from .models import Author, Tag, Quote
7 class QuoteTest(TestCase):
9 a1 = Author.objects.create(name="JFK")
10 q1 = Quote.objects.create(text="Ich bin...", author=a1)
13 q = Quote.objects.filter(text__startswith="Ich")
14 self.assertEqual(q.count(), 1)
16 self.assertEqual(q.author.name, "JFK")
19 class ViewsTest(TestCase):
21 a1 = Author.objects.create(name="Author with 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)
28 a2 = Author.objects.create(name="Author without notes")
29 q2= Quote.objects.create(text="Quote02, no tags", author=a2)
31 def test_views_all_data(self):
32 q = Quote.objects.filter(text__startswith="Quote01")
33 self.assertEqual(q.count(), 1)
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)
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)
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)
53 def test_views_minimal_data(self):
54 q = Quote.objects.filter(text__startswith="Quote02")
55 self.assertEqual(q.count(), 1)
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)
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)