]> gitweb.fperrin.net Git - djsite.git/blobdiff - quotes/test_cloud.py
First iteration towards doing a tag cloud
[djsite.git] / quotes / test_cloud.py
diff --git a/quotes/test_cloud.py b/quotes/test_cloud.py
new file mode 100644 (file)
index 0000000..906aa5b
--- /dev/null
@@ -0,0 +1,45 @@
+import pytest
+import re
+
+from quotes.models import QuoteTag, Quote, Work, Author
+
+class Test_Cloud(object):
+    @pytest.fixture
+    def size_range_small(self):
+        return [1, 5, 10, 7, 3]
+
+    def fill_db(self, size_range):
+        maxsize = max(size_range)
+
+        a = Author.objects.create(name="blah")
+        w = Work.objects.create(name="foo", author=a)
+
+        for i in range(maxsize):
+            Quote.objects.create(text="some text for quote %d" % i,
+                                 work = w)
+
+        for size in size_range:
+            tag = QuoteTag.objects.create(tag="tag-%d-" % size)
+            quotelist = Quote.objects.all()[:size]
+
+            for i in range(size):
+                quotelist[i].tags.add(tag)
+
+    def check_cloud(self, c, size_range):
+        top20 = sorted(size_range, reverse=True)[0:20]
+
+        cloud = c.getPage("cloud/")
+        for size in top20:
+            assert re.search("tag-%d-, %d quotes" % (size, size), cloud)
+
+    def test_cloud(self, size_range_small, db, c):
+        self.fill_db(size_range_small)
+        self.check_cloud(c, size_range_small)
+
+    @pytest.fixture
+    def size_range_large(self):
+        return range(1, 50)
+
+    def test_cloud_large(self, size_range_large, db, c):
+        self.fill_db(size_range_large)
+        self.check_cloud(c, size_range_large)