# -*- encoding: utf-8 -*- import pytest from .models import QuoteTag, Author, Work, Quote import re class Test_QuoteTest(): @pytest.fixture(scope='function') def q1(self, db): a1 = Author.objects.create(name="JFK") w1 = Work.objects.create(name="Berlin speech", author=a1) q1 = Quote.objects.create(text="

Ich bin...

", work=w1) return q1 @pytest.mark.django_db def test_one(self, q1): q = Quote.objects.filter(text__startswith="

Ich") assert q.count() == 1 q = q[0] assert q == q1 assert q.work.author.name == 'JFK' class Test_Views(): @pytest.fixture(autouse=True, scope='function') def quotes(self, db): a1 = Author.objects.create(name="Author with notes", notes="") 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

", work=w1, notes="

Some notes for the quote

") 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) assert q2.tags.all().count() == 0 def test_all(self, c): content = c.getPage('all/') for a in Author.objects.all(): assert a.name in content assert a.notes in content for w in Work.objects.all(): assert w.name in content assert w.notes in content for q in Quote.objects.all(): assert q.text in content assert q.notes in content c.checkAllLinks('all/', content) @pytest.mark.slow def test_random(self, c): seen = {} for q in Quote.objects.all(): seen[q.id] = False # Permalink permalinkre = re.compile(r'([0-9]+).*Permalink') for i in range(100): content = c.getPage('random') m = permalinkre.search(content) assert m is not None, "Did not find the permalink in: "+content quoteid = int(m.group(1)) assert quoteid in seen seen[quoteid] = True for q in Quote.objects.all(): assert seen[q.id] def test_views_all_data(self, c): q = Quote.objects.filter(text__startswith="

Quote01") assert q.count() == 1, "Couldn't find Quote01 after insertion" q = q[0] # check the individual quote page; each of the note type is displayed quotepage = c.getPage('show/%s/' % q.id) assert q.text in quotepage assert "author_notes" in quotepage assert "work_notes" in quotepage assert "tag_link" in quotepage assert quotepage.count("tag_link") == 2 # check the work page; each of the note type is displayed, and # the work notes are shown only once workpage = c.getPage('work/%s/' % q.work.id) assert q.text in workpage assert "author_notes" in workpage assert "work_notes" in workpage assert workpage.count("work_notes") == 1 assert "tag_link" in workpage # check the author page; each of the note type is displayed, and # the author notes are shown only once authorpage = c.getPage('author/%s/' % q.work.author.id) assert q.text in authorpage assert "author_notes" in authorpage assert authorpage.count("author_notes") == 1 assert "work_notes" in authorpage assert "tag_link" in authorpage # check the tag page for tag in q.tags.all(): tagpage = c.getPage('tag/%s/' % tag.id) assert q.text in tagpage def test_views_minimal_data(self, c): q = Quote.objects.filter(text__startswith="

Quote02") assert q.count() == 1, \ "Couldn't find Quote02 after insertion" q = q[0] quotepage = c.getPage('show/%s/' % q.id) assert q.text in quotepage assert "author_notes" not in quotepage assert "work_notes" not in quotepage assert "tag_link" not in quotepage workpage = c.getPage('work/%s/' % q.work.id) assert q.text in workpage assert "author_notes" not in quotepage assert "work_notes" not in quotepage assert "tag_link" not in quotepage authorpage = c.getPage('author/%s/' % q.work.author.id) assert q.text in authorpage assert "author_notes" not in authorpage assert "work_notes" not in authorpage assert "tag_link" not in authorpage assert 'Quote01, two tags' not in authorpage def test_view_author_notes_once(self, c): # check that on the per-author view, the author notes aren't display # for every quote a = Author.objects.filter(name="Author with notes") assert a.count() == 1 a = a[0] authorpage = c.getPage('author/%s/' % a.id) assert authorpage.count("Some notes for the author") == 1 class Test_Unicode(): def test_unicode(self, db): a = Author.objects.create(name="ê è “smart ''quotes,”") w = Work.objects.create(name="¿who?If you’re creati'' ng a", author=a) q = Quote.objects.create(text="µqwer If you’r'' ¨ë ẽ « or » e ", work=w) t = QuoteTag.objects.create(tag=", “s tag 'a’ ß") for t in QuoteTag.objects.all(): print t for w in Work.objects.all(): print w for a in Author.objects.all(): print a for q in Quote.objects.all(): print q