]> gitweb.fperrin.net Git - djsite.git/blob - quotes/test_quotes.py
Convert to using pytest
[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 len(q2.tags.all()) == 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     @pytest.mark.slow
55     def test_random(self, c):
56         seen = {}
57         for q in Quote.objects.all():
58             seen[q.id] = False
59
60         # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
61         permalinkre = re.compile(r'([0-9]+).*Permalink')
62
63         for i in range(100):
64             content = c.getPage('random')
65             m = permalinkre.search(content)
66             assert m is not None, "Did not find the permalink in: "+content
67             quoteid = int(m.group(1))
68             assert quoteid in seen
69             seen[quoteid] = True
70         for q in Quote.objects.all():
71             assert seen[q.id]
72
73     def test_views_all_data(self, c):
74         q = Quote.objects.filter(text__startswith="<p>Quote01")
75         assert q.count() == 1, "Couldn't find Quote01 after insertion"
76         q = q[0]
77
78         # check the individual quote page; each of the note type is displayed
79         quotepage = c.getPage('show/%s/' % q.id)
80         assert q.text in quotepage
81         assert "author_notes" in quotepage
82         assert "work_notes" in quotepage
83         assert "tag_link" in quotepage
84         assert quotepage.count("tag_link") == 2
85
86         # check the work page; each of the note type is displayed, and
87         # the work notes are shown only once
88         workpage = c.getPage('work/%s/' % q.work.id)
89         assert q.text in workpage
90         assert "author_notes" in workpage
91         assert "work_notes" in workpage
92         assert workpage.count("work_notes") == 1
93         assert "tag_link" in workpage
94
95         # check the author page; each of the note type is displayed, and
96         # the author notes are shown only once
97         authorpage = c.getPage('author/%s/' % q.work.author.id)
98         assert q.text in authorpage
99         assert "author_notes" in authorpage
100         assert authorpage.count("author_notes") == 1
101         assert "work_notes" in authorpage
102         assert "tag_link" in authorpage
103
104         # check the tag page
105         for tag in q.tags.all():
106             tagpage = c.getPage('tag/%s/' % tag.id)
107             assert q.text in tagpage
108
109     def test_views_minimal_data(self, c):
110         q = Quote.objects.filter(text__startswith="<p>Quote02")
111         assert q.count() == 1, \
112             "Couldn't find Quote02 after insertion"
113         q = q[0]
114
115         quotepage = c.getPage('show/%s/' % q.id)
116         assert q.text in quotepage
117         assert "author_notes" not in quotepage
118         assert "work_notes" not in quotepage
119         assert "tag_link" not in quotepage
120
121         workpage = c.getPage('work/%s/' % q.work.id)
122         assert q.text in workpage
123         assert "author_notes" not in quotepage
124         assert "work_notes" not in quotepage
125         assert "tag_link" not in quotepage
126
127         authorpage = c.getPage('author/%s/' % q.work.author.id)
128         assert q.text in authorpage
129         assert "author_notes" not in authorpage
130         assert "work_notes" not in authorpage
131         assert "tag_link" not in authorpage
132         assert 'Quote01, two tags' not in authorpage
133
134     def test_view_author_notes_once(self, c):
135         # check that on the per-author view, the author notes aren't display
136         # for every quote
137         a = Author.objects.filter(name="Author with notes")
138         assert a.count() == 1
139         a = a[0]
140
141         authorpage = c.getPage('author/%s/' % a.id)
142         assert authorpage.count("Some notes for the author") == 1