]> gitweb.fperrin.net Git - djsite.git/blobdiff - quotes/test_massimport.py
Add a massimport page
[djsite.git] / quotes / test_massimport.py
diff --git a/quotes/test_massimport.py b/quotes/test_massimport.py
new file mode 100644 (file)
index 0000000..0a9fb4f
--- /dev/null
@@ -0,0 +1,121 @@
+import pytest
+
+from .models import Author, Work, Quote, QuoteTag
+
+class Test_MassImport():
+    @pytest.fixture(scope='function')
+    def q1(self, db):
+        a1 = Author.objects.create(name="JFK")
+        w1 = Work.objects.create(name="Berlin speech", author=a1)
+        q1 = Quote.objects.create(text="<p>Ich bin...</p>", work=w1)
+        return q1
+
+    @pytest.mark.django_db
+    def test_massimport_1(self, q1, c):
+        allquotes = """\
+Ich bin ein Berliner
+---
+JFK
+---
+Berlin speech
+===
+To be or not to be, that is the question
+---
+William Shakespeare
+---
+Hamlet
+===
+To thine own self, be true
+---
+William Shakespeare
+---
+Hamlet
+---
+tag1, tag2, tag3
+===
+A rose by any other name...
+---
+William Shakespeare
+---
+Romeo and Juliet
+---
+tag1, tag555
+==="""
+
+        results = c.postPage('massimport/', {'quotes': allquotes})
+
+        assert "rejected" not in results
+
+        assert Quote.objects.get(text="<p>To thine own self, be true</p>")
+        assert Quote.objects.get(text__contains="To be or not to be")
+
+        assert Author.objects.get(name="JFK")
+        assert Author.objects.get(name="William Shakespeare")
+        assert Author.objects.all().count() == 2
+
+        hamlet = Work.objects.get(name="Hamlet")
+        assert hamlet
+        assert hamlet.author == Author.objects.get(name="William Shakespeare")
+
+        for quote in Quote.objects.all():
+            assert c.getPage(quote.get_absolute_url())
+
+    @pytest.mark.django_db
+    def test_massimport_2(self, q1, c):
+        allquotes = """\
+<script>somethingevil()</script>
+A rose by any other name...
+---
+William Shakespeare
+---
+Romeo and Juliet
+---
+tag1, tag555
+==="""
+
+        results = c.postPage('massimport/', {'quotes': allquotes})
+        assert "<script>" not in results
+        with pytest.raises(Quote.DoesNotExist):
+            Quote.objects.get(text__contains="<script>")
+
+    @pytest.mark.django_db
+    def test_massimport_3(self, c):
+        """Whitespace and stuff"""
+        allquotes = """\
+A rose by any other name...
+---
+William Shakespeare
+---
+Romeo and Juliet
+---
+tag1, tag555
+===
+
+To be, or not to be, that is the question:
+Whether 'tis Nobler in the mind to suffer
+The Slings and Arrows of outrageous Fortune,
+Or to take Arms against a Sea of troubles,
+And by opposing end them: to die, to sleep
+No more;
+
+---
+   William Shakespeare  
+
+---
+Hamlet  
+
+"""
+
+        results = c.postPage('massimport/', {'quotes': allquotes})
+        assert "rejected" not in results
+        assert Quote.objects.get(text="<p>A rose by any other name...</p>")
+        assert Author.objects.get(name="William Shakespeare")
+        assert Work.objects.get(name="Romeo and Juliet")
+        assert QuoteTag.objects.get(tag="tag1")
+        assert QuoteTag.objects.get(tag="tag555")
+
+        tirade = Quote.objects.get(text__contains="To be, or not to be")
+        assert tirade.work.name == "Hamlet"
+        assert tirade.work.author.name == "William Shakespeare"
+        assert Author.objects.filter(name__contains="Shakespeare").count() == 1
+