X-Git-Url: http://gitweb.fperrin.net/?p=djsite.git;a=blobdiff_plain;f=quotes%2Ftests.py;h=556e9252c3377477911b077da4e64315b8f067b9;hp=7ce503c2dd97ba78597f6ff6e4393132753573f6;hb=af3f857eea25b939f927832741f377e4de401f9c;hpb=0b5eb1c3de4b96a048f784ab7c210462f26ac8da diff --git a/quotes/tests.py b/quotes/tests.py index 7ce503c..556e925 100644 --- a/quotes/tests.py +++ b/quotes/tests.py @@ -1,3 +1,68 @@ -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)