]> gitweb.fperrin.net Git - djsite.git/blob - quotes/test_quotes.py
d3e65e556aa6195dd87fa32f7c92d09b9b1b3a1d
[djsite.git] / quotes / test_quotes.py
1 # -*- encoding: utf-8 -*-
2
3 import pytest
4
5 from .models import QuoteTag, Author, Work, Quote
6 import re
7
8 class Test_QuoteTest():
9     @pytest.fixture(scope='function')
10     def q1(self, db):
11         a1 = Author.objects.create(name="JFK")
12         w1 = Work.objects.create(name="Berlin speech", author=a1)
13         q1 = Quote.objects.create(text="<p>Ich bin...</p>", work=w1)
14         return q1
15
16     @pytest.mark.django_db
17     def test_one(self, q1):
18         q = Quote.objects.filter(text__startswith="<p>Ich")
19         assert q.count() == 1
20         q = q[0]
21         assert q == q1
22         assert q.work.author.name == 'JFK'
23
24 class Test_Views():
25     @pytest.fixture(autouse=True, scope='function')
26     def quotes(self, db):
27         a1 = Author.objects.create(name="Author with notes",
28                                    notes="<script>Some notes for the author</script>")
29         w1 = Work.objects.create(name="Context with some notes",
30                                  author=a1,
31                                  notes="<p>Some notes for the work</p>")
32         q1 = Quote.objects.create(text="<p>Quote01, two tags</p>",
33                                   work=w1,
34                                   notes="<p>Some notes for the quote</p>")
35         t1 = QuoteTag.objects.create(tag="tag01-1")
36         t2 = QuoteTag.objects.create(tag="tag01-2")
37         q1.tags.add(t1, t2)
38
39         a2 = Author.objects.create(name="Author without notes")
40         w2 = Work.objects.create(name="Work without notes", author=a2)
41         q2 = Quote.objects.create(text="<p>Quote02, no tags</p>", work=w2)
42         assert q2.tags.all().count() == 0
43
44     def test_all(self, c):
45         content = c.getPage('all/')
46         for a in Author.objects.all():
47             assert a.name in content
48             assert a.notes in content
49         for w in Work.objects.all():
50             assert w.name in content
51             assert w.notes in content
52         for q in Quote.objects.all():
53             assert q.text in content
54             assert q.notes in content
55
56         c.checkAllLinks('all/', content)
57
58     @pytest.mark.slow
59     def test_random(self, c):
60         seen = {}
61         for q in Quote.objects.all():
62             seen[q.id] = False
63
64         # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
65         permalinkre = re.compile(r'([0-9]+).*Permalink')
66
67         for i in range(100):
68             content = c.getPage('random')
69             m = permalinkre.search(content)
70             assert m is not None, "Did not find the permalink in: "+content
71             quoteid = int(m.group(1))
72             assert quoteid in seen
73             seen[quoteid] = True
74         for q in Quote.objects.all():
75             assert seen[q.id]
76
77     def test_views_all_data(self, c):
78         q = Quote.objects.filter(text__startswith="<p>Quote01")
79         assert q.count() == 1, "Couldn't find Quote01 after insertion"
80         q = q[0]
81
82         # check the individual quote page; each of the note type is displayed
83         quotepage = c.getPage('show/%s/' % q.id)
84         assert q.text in quotepage
85         assert "author_notes" in quotepage
86         assert "work_notes" in quotepage
87         assert "tag_link" in quotepage
88         assert quotepage.count("tag_link") == 2
89
90         # check the work page; each of the note type is displayed, and
91         # the work notes are shown only once
92         workpage = c.getPage('work/%s/' % q.work.id)
93         assert q.text in workpage
94         assert "author_notes" in workpage
95         assert "work_notes" in workpage
96         assert workpage.count("work_notes") == 1
97         assert "tag_link" in workpage
98
99         # check the author page; each of the note type is displayed, and
100         # the author notes are shown only once
101         authorpage = c.getPage('author/%s/' % q.work.author.id)
102         assert q.text in authorpage
103         assert "author_notes" in authorpage
104         assert authorpage.count("author_notes") == 1
105         assert "work_notes" in authorpage
106         assert "tag_link" in authorpage
107
108         # check the tag page
109         for tag in q.tags.all():
110             tagpage = c.getPage('tag/%s/' % tag.id)
111             assert q.text in tagpage
112
113     def test_views_minimal_data(self, c):
114         q = Quote.objects.filter(text__startswith="<p>Quote02")
115         assert q.count() == 1, \
116             "Couldn't find Quote02 after insertion"
117         q = q[0]
118
119         quotepage = c.getPage('show/%s/' % q.id)
120         assert q.text in quotepage
121         assert "author_notes" not in quotepage
122         assert "work_notes" not in quotepage
123         assert "tag_link" not in quotepage
124
125         workpage = c.getPage('work/%s/' % q.work.id)
126         assert q.text in workpage
127         assert "author_notes" not in quotepage
128         assert "work_notes" not in quotepage
129         assert "tag_link" not in quotepage
130
131         authorpage = c.getPage('author/%s/' % q.work.author.id)
132         assert q.text in authorpage
133         assert "author_notes" not in authorpage
134         assert "work_notes" not in authorpage
135         assert "tag_link" not in authorpage
136         assert 'Quote01, two tags' not in authorpage
137
138     def test_view_author_notes_once(self, c):
139         # check that on the per-author view, the author notes aren't display
140         # for every quote
141         a = Author.objects.filter(name="Author with notes")
142         assert a.count() == 1
143         a = a[0]
144
145         authorpage = c.getPage('author/%s/' % a.id)
146         assert authorpage.count("Some notes for the author") == 1
147
148 class Test_Unicode():
149     def test_unicode(self, db):
150         a = Author.objects.create(name="ê è  “smart ''quotes,”")
151         w = Work.objects.create(name="¿who?If you’re creati'' ng a", author=a)
152         q = Quote.objects.create(text="µqwer If you’r'' ¨ë ẽ « or » e ", work=w)
153         t = QuoteTag.objects.create(tag=", “s tag 'a’ ß")
154
155         for t in QuoteTag.objects.all():
156             print t
157         for w in Work.objects.all():
158             print w
159         for a in Author.objects.all():
160             print a
161         for q in Quote.objects.all():
162             print q