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)