]> gitweb.fperrin.net Git - djsite.git/blob - quotes/test_massimport.py
Add a massimport page
[djsite.git] / quotes / test_massimport.py
1 import pytest
2
3 from .models import Author, Work, Quote, QuoteTag
4
5 class Test_MassImport():
6     @pytest.fixture(scope='function')
7     def q1(self, db):
8         a1 = Author.objects.create(name="JFK")
9         w1 = Work.objects.create(name="Berlin speech", author=a1)
10         q1 = Quote.objects.create(text="<p>Ich bin...</p>", work=w1)
11         return q1
12
13     @pytest.mark.django_db
14     def test_massimport_1(self, q1, c):
15         allquotes = """\
16 Ich bin ein Berliner
17 ---
18 JFK
19 ---
20 Berlin speech
21 ===
22 To be or not to be, that is the question
23 ---
24 William Shakespeare
25 ---
26 Hamlet
27 ===
28 To thine own self, be true
29 ---
30 William Shakespeare
31 ---
32 Hamlet
33 ---
34 tag1, tag2, tag3
35 ===
36 A rose by any other name...
37 ---
38 William Shakespeare
39 ---
40 Romeo and Juliet
41 ---
42 tag1, tag555
43 ==="""
44
45         results = c.postPage('massimport/', {'quotes': allquotes})
46
47         assert "rejected" not in results
48
49         assert Quote.objects.get(text="<p>To thine own self, be true</p>")
50         assert Quote.objects.get(text__contains="To be or not to be")
51
52         assert Author.objects.get(name="JFK")
53         assert Author.objects.get(name="William Shakespeare")
54         assert Author.objects.all().count() == 2
55
56         hamlet = Work.objects.get(name="Hamlet")
57         assert hamlet
58         assert hamlet.author == Author.objects.get(name="William Shakespeare")
59
60         for quote in Quote.objects.all():
61             assert c.getPage(quote.get_absolute_url())
62
63     @pytest.mark.django_db
64     def test_massimport_2(self, q1, c):
65         allquotes = """\
66 <script>somethingevil()</script>
67 A rose by any other name...
68 ---
69 William Shakespeare
70 ---
71 Romeo and Juliet
72 ---
73 tag1, tag555
74 ==="""
75
76         results = c.postPage('massimport/', {'quotes': allquotes})
77         assert "<script>" not in results
78         with pytest.raises(Quote.DoesNotExist):
79             Quote.objects.get(text__contains="<script>")
80
81     @pytest.mark.django_db
82     def test_massimport_3(self, c):
83         """Whitespace and stuff"""
84         allquotes = """\
85 A rose by any other name...
86 ---
87 William Shakespeare
88 ---
89 Romeo and Juliet
90 ---
91 tag1, tag555
92 ===
93
94 To be, or not to be, that is the question:
95 Whether 'tis Nobler in the mind to suffer
96 The Slings and Arrows of outrageous Fortune,
97 Or to take Arms against a Sea of troubles,
98 And by opposing end them: to die, to sleep
99 No more;
100
101 ---
102    William Shakespeare  
103
104 ---
105 Hamlet  
106
107 """
108
109         results = c.postPage('massimport/', {'quotes': allquotes})
110         assert "rejected" not in results
111         assert Quote.objects.get(text="<p>A rose by any other name...</p>")
112         assert Author.objects.get(name="William Shakespeare")
113         assert Work.objects.get(name="Romeo and Juliet")
114         assert QuoteTag.objects.get(tag="tag1")
115         assert QuoteTag.objects.get(tag="tag555")
116
117         tirade = Quote.objects.get(text__contains="To be, or not to be")
118         assert tirade.work.name == "Hamlet"
119         assert tirade.work.author.name == "William Shakespeare"
120         assert Author.objects.filter(name__contains="Shakespeare").count() == 1
121