from django.test import TestCase, Client
# Create your tests here.
-from .models import Tag, Author, Work, Quote
+from .models import QuoteTag, Author, Work, Quote
import re
+import lxml.etree
class QuoteTest(TestCase):
def setUp(self):
a1 = Author.objects.create(name="JFK")
w1 = Work.objects.create(name="Berlin speech", author=a1)
- q1 = Quote.objects.create(text="Ich bin...", work=w1)
+ q1 = Quote.objects.create(text="<p>Ich bin...</p>", work=w1)
+ self.q1 = q1
def test_one(self):
- q = Quote.objects.filter(text__startswith="Ich")
+ q = Quote.objects.filter(text__startswith="<p>Ich")
self.assertEqual(q.count(), 1)
q = q[0]
+ self.assertEqual(q, self.q1)
self.assertEqual(q.work.author.name, "JFK")
class ViewsTest(TestCase):
response = c.get('/quotes/' + url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.charset, 'utf-8')
- return response.content.decode(response.charset)
+ document = response.content.decode(response.charset)
+ try:
+ lxml.etree.fromstring(document)
+ except lxml.etree.XMLSyntaxError as e:
+ self.assertTrue(False, 'Errors in page at %s: %s' % (url, e))
+ self.assertFalse('<script>' in document)
+ return document
def setUp(self):
a1 = Author.objects.create(name="Author with notes",
- notes="Some notes for the author")
+ notes="<script>Some notes for the author</script>")
w1 = Work.objects.create(name="Context with some notes",
author=a1,
- notes="Some notes for the work")
- q1 = Quote.objects.create(text="Quote01, two tags",
+ notes="<p>Some notes for the work</p>")
+ q1 = Quote.objects.create(text="<p>Quote01, two tags</p>",
work=w1,
- notes="Some notes for the quote")
- t1 = Tag.objects.create(tag="tag01-1")
- t2 = Tag.objects.create(tag="tag01-2")
+ notes="<p>Some notes for the quote</p>")
+ t1 = QuoteTag.objects.create(tag="tag01-1")
+ t2 = QuoteTag.objects.create(tag="tag01-2")
q1.tags.add(t1, t2)
a2 = Author.objects.create(name="Author without notes")
w2 = Work.objects.create(name="Work without notes", author=a2)
- q2 = Quote.objects.create(text="Quote02, no tags", work=w2)
+ q2 = Quote.objects.create(text="<p>Quote02, no tags</p>", work=w2)
self.assertSequenceEqual(q2.tags.all(), [])
def test_all(self):
# <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
permalinkre = re.compile(r'([0-9]+).*Permalink')
- for i in xrange(100):
+ for i in range(100):
content = self.getPage('random')
m = permalinkre.search(content)
self.assertIsNotNone(m, content)
self.assertTrue(seen[q.id])
def test_views_all_data(self):
- q = Quote.objects.filter(text__startswith="Quote01")
- self.assertEqual(q.count(), 1)
+ q = Quote.objects.filter(text__startswith="<p>Quote01")
+ self.assertEqual(q.count(), 1, "Couldn't find Quote01 after insertion")
q = q[0]
# check the individual quote page; each of the note type is displayed
self.assertIn(q.text, tagpage)
def test_views_minimal_data(self):
- q = Quote.objects.filter(text__startswith="Quote02")
- self.assertEqual(q.count(), 1)
+ q = Quote.objects.filter(text__startswith="<p>Quote02")
+ self.assertEqual(q.count(), 1,
+ "Couldn't find Quote02 after insertion")
q = q[0]
quotepage = self.getPage('show/%s/' % q.id)