X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=quotes%2Ftests.py;h=2a7bc4502ae84b04ce160ced0f6290c2bf03d6e7;hb=1403f44199bde09f56e466285e00462e88e344fd;hp=7ce503c2dd97ba78597f6ff6e4393132753573f6;hpb=e63e884391c4ac2c3af8ff72919a196f7bf9d83f;p=djsite.git diff --git a/quotes/tests.py b/quotes/tests.py index 7ce503c..2a7bc45 100644 --- a/quotes/tests.py +++ b/quotes/tests.py @@ -1,3 +1,80 @@ -from django.test import TestCase +import sys +from django.test import TestCase, Client # Create your tests here. +from .models import Author, Tag, Quote + +class QuoteTest(TestCase): + def setUp(self): + a1 = Author.objects.create(name="JFK") + q1 = Quote.objects.create(text="Ich bin...", author=a1) + + def test_one(self): + q = Quote.objects.filter(text__startswith="Ich") + self.assertEqual(q.count(), 1) + q = q[0] + self.assertEqual(q.author.name, "JFK") + + +class ViewsTest(TestCase): + def setUp(self): + a1 = Author.objects.create(name="Author with notes", + notes="Some notes") + t1 = Tag.objects.create(tag="tag01-1") + t2 = Tag.objects.create(tag="tag01-2") + q1 = Quote.objects.create(text="Quote01, two tags", author=a1) + q1.tags.add(t1, t2) + + a2 = Author.objects.create(name="Author without notes") + q2= Quote.objects.create(text="Quote02, no tags", author=a2) + + def test_views_all_data(self): + q = Quote.objects.filter(text__startswith="Quote01") + self.assertEqual(q.count(), 1) + q = q[0] + + c = Client() + response = c.get('/quotes/show/%s/' % q.id) + self.assertEqual(response.status_code, 200) + self.assertTrue('Quote01, two tags' in response.content) + self.assertTrue("author_notes" in response.content) + self.assertEqual(response.content.count("tag_link"), 2) + + response = c.get('/quotes/author/%s/' % q.author.id) + self.assertEqual(response.status_code, 200) + self.assertTrue('Quote01, two tags' in response.content) + self.assertTrue("author_notes" in response.content) + + for tag in q.tags.all(): + response = c.get('/quotes/tag/%s/' % tag.id) + self.assertEqual(response.status_code, 200) + self.assertTrue('Quote01, two tags' in response.content) + + def test_views_minimal_data(self): + q = Quote.objects.filter(text__startswith="Quote02") + self.assertEqual(q.count(), 1) + q = q[0] + + c = Client() + response = c.get('/quotes/show/%s/' % q.id) + self.assertEqual(response.status_code, 200) + self.assertTrue('Quote02' in response.content) + self.assertFalse("author_notes" in response.content) + self.assertEqual(response.content.count("tag_link"), 0) + + response = c.get('/quotes/author/%s/' % q.author.id) + self.assertEqual(response.status_code, 200) + self.assertFalse('Quote01, two tags' in response.content) + self.assertTrue('Quote02' in response.content) + + def test_view_author_notes_once(self): + # check that on the per-author view, the author notes aren't display + # for every quote + a = Author.objects.filter(name="Author with notes") + self.assertEqual(a.count(), 1) + a = a[0] + + c = Client() + response = c.get('/quotes/author/%s/' % a.id) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content.count("Some notes"), 1)