3 from .models import QuoteTag, Author, Work, Quote
6 class Test_QuoteTest():
7 @pytest.fixture(scope='function')
9 a1 = Author.objects.create(name="JFK")
10 w1 = Work.objects.create(name="Berlin speech", author=a1)
11 q1 = Quote.objects.create(text="<p>Ich bin...</p>", work=w1)
14 @pytest.mark.django_db
15 def test_one(self, q1):
16 q = Quote.objects.filter(text__startswith="<p>Ich")
20 assert q.work.author.name == 'JFK'
23 @pytest.fixture(autouse=True, scope='function')
25 a1 = Author.objects.create(name="Author with notes",
26 notes="<script>Some notes for the author</script>")
27 w1 = Work.objects.create(name="Context with some notes",
29 notes="<p>Some notes for the work</p>")
30 q1 = Quote.objects.create(text="<p>Quote01, two tags</p>",
32 notes="<p>Some notes for the quote</p>")
33 t1 = QuoteTag.objects.create(tag="tag01-1")
34 t2 = QuoteTag.objects.create(tag="tag01-2")
37 a2 = Author.objects.create(name="Author without notes")
38 w2 = Work.objects.create(name="Work without notes", author=a2)
39 q2 = Quote.objects.create(text="<p>Quote02, no tags</p>", work=w2)
40 assert q2.tags.all().count() == 0
42 def test_all(self, c):
43 content = c.getPage('all/')
44 for a in Author.objects.all():
45 assert a.name in content
46 assert a.notes in content
47 for w in Work.objects.all():
48 assert w.name in content
49 assert w.notes in content
50 for q in Quote.objects.all():
51 assert q.text in content
52 assert q.notes in content
54 c.checkAllLinks('all/', content)
57 def test_random(self, c):
59 for q in Quote.objects.all():
62 # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
63 permalinkre = re.compile(r'([0-9]+).*Permalink')
66 content = c.getPage('random')
67 m = permalinkre.search(content)
68 assert m is not None, "Did not find the permalink in: "+content
69 quoteid = int(m.group(1))
70 assert quoteid in seen
72 for q in Quote.objects.all():
75 def test_views_all_data(self, c):
76 q = Quote.objects.filter(text__startswith="<p>Quote01")
77 assert q.count() == 1, "Couldn't find Quote01 after insertion"
80 # check the individual quote page; each of the note type is displayed
81 quotepage = c.getPage('show/%s/' % q.id)
82 assert q.text in quotepage
83 assert "author_notes" in quotepage
84 assert "work_notes" in quotepage
85 assert "tag_link" in quotepage
86 assert quotepage.count("tag_link") == 2
88 # check the work page; each of the note type is displayed, and
89 # the work notes are shown only once
90 workpage = c.getPage('work/%s/' % q.work.id)
91 assert q.text in workpage
92 assert "author_notes" in workpage
93 assert "work_notes" in workpage
94 assert workpage.count("work_notes") == 1
95 assert "tag_link" in workpage
97 # check the author page; each of the note type is displayed, and
98 # the author notes are shown only once
99 authorpage = c.getPage('author/%s/' % q.work.author.id)
100 assert q.text in authorpage
101 assert "author_notes" in authorpage
102 assert authorpage.count("author_notes") == 1
103 assert "work_notes" in authorpage
104 assert "tag_link" in authorpage
107 for tag in q.tags.all():
108 tagpage = c.getPage('tag/%s/' % tag.id)
109 assert q.text in tagpage
111 def test_views_minimal_data(self, c):
112 q = Quote.objects.filter(text__startswith="<p>Quote02")
113 assert q.count() == 1, \
114 "Couldn't find Quote02 after insertion"
117 quotepage = c.getPage('show/%s/' % q.id)
118 assert q.text in quotepage
119 assert "author_notes" not in quotepage
120 assert "work_notes" not in quotepage
121 assert "tag_link" not in quotepage
123 workpage = c.getPage('work/%s/' % q.work.id)
124 assert q.text in workpage
125 assert "author_notes" not in quotepage
126 assert "work_notes" not in quotepage
127 assert "tag_link" not in quotepage
129 authorpage = c.getPage('author/%s/' % q.work.author.id)
130 assert q.text in authorpage
131 assert "author_notes" not in authorpage
132 assert "work_notes" not in authorpage
133 assert "tag_link" not in authorpage
134 assert 'Quote01, two tags' not in authorpage
136 def test_view_author_notes_once(self, c):
137 # check that on the per-author view, the author notes aren't display
139 a = Author.objects.filter(name="Author with notes")
140 assert a.count() == 1
143 authorpage = c.getPage('author/%s/' % a.id)
144 assert authorpage.count("Some notes for the author") == 1