]> gitweb.fperrin.net Git - djsite.git/blob - quotes/test_quotes.py
b194ec111e3f18d1c73bb9817de038df8957c0bc
[djsite.git] / quotes / test_quotes.py
1 import pytest
2
3 from .models import QuoteTag, Author, Work, Quote
4 import re
5
6 class Test_QuoteTest():
7     @pytest.fixture(scope='function')
8     def q1(self, db):
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)
12         return q1
13
14     @pytest.mark.django_db
15     def test_one(self, q1):
16         q = Quote.objects.filter(text__startswith="<p>Ich")
17         assert q.count() == 1
18         q = q[0]
19         assert q == q1
20         assert q.work.author.name == 'JFK'
21
22 class Test_Views():
23     @pytest.fixture(autouse=True, scope='function')
24     def quotes(self, db):
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",
28                                  author=a1,
29                                  notes="<p>Some notes for the work</p>")
30         q1 = Quote.objects.create(text="<p>Quote01, two tags</p>",
31                                   work=w1,
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")
35         q1.tags.add(t1, t2)
36
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
41
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
53
54         c.checkAllLinks('all/', content)
55
56     @pytest.mark.slow
57     def test_random(self, c):
58         seen = {}
59         for q in Quote.objects.all():
60             seen[q.id] = False
61
62         # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
63         permalinkre = re.compile(r'([0-9]+).*Permalink')
64
65         for i in range(100):
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
71             seen[quoteid] = True
72         for q in Quote.objects.all():
73             assert seen[q.id]
74
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"
78         q = q[0]
79
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
87
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
96
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
105
106         # check the tag page
107         for tag in q.tags.all():
108             tagpage = c.getPage('tag/%s/' % tag.id)
109             assert q.text in tagpage
110
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"
115         q = q[0]
116
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
122
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
128
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
135
136     def test_view_author_notes_once(self, c):
137         # check that on the per-author view, the author notes aren't display
138         # for every quote
139         a = Author.objects.filter(name="Author with notes")
140         assert a.count() == 1
141         a = a[0]
142
143         authorpage = c.getPage('author/%s/' % a.id)
144         assert authorpage.count("Some notes for the author") == 1