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
55 def test_random(self, c):
57 for q in Quote.objects.all():
60 # <a href="{% url 'quotes:onequote' quote.id %}">Permalink</a>
61 permalinkre = re.compile(r'([0-9]+).*Permalink')
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
70 for q in Quote.objects.all():
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"
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
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
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
105 for tag in q.tags.all():
106 tagpage = c.getPage('tag/%s/' % tag.id)
107 assert q.text in tagpage
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"
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
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
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
134 def test_view_author_notes_once(self, c):
135 # check that on the per-author view, the author notes aren't display
137 a = Author.objects.filter(name="Author with notes")
138 assert a.count() == 1
141 authorpage = c.getPage('author/%s/' % a.id)
142 assert authorpage.count("Some notes for the author") == 1