Commits

psviderski committed 9a0b6e9

Add and polish tests for Blog itemtype in test_Blog.

Comments (0)

Files changed (1)

MoinMoin/items/_tests/test_Blog.py

 
 from MoinMoin._tests import become_trusted, update_item
 from MoinMoin.items import Item
-from MoinMoin.config import CONTENTTYPE, ITEMTYPE, PTIME, ACL
+from MoinMoin.config import CONTENTTYPE, ITEMTYPE, PTIME, ACL, TAGS
 from MoinMoin.items.blog import ITEMTYPE_BLOG, ITEMTYPE_BLOG_ENTRY
 from MoinMoin.items.blog import Blog, BlogEntry
 
 
 class TestView(object):
-    def _test_view(self, item_name, data_tokens=[], exclude_data_tokens=[], regex=None):
+    def _test_view(self, item_name, req_args={}, data_tokens=[], exclude_data_tokens=[], regex=None):
         with self.app.test_client() as c:
-            rv = c.get(url_for('frontend.show_item', item_name=item_name))
+            rv = c.get(url_for('frontend.show_item', item_name=item_name, **req_args))
             for data in data_tokens:
                 assert data in rv.data
             for data in exclude_data_tokens:
 
 
 class TestBlog(TestView):
-    NO_ENTRIES_MSG = u"There are no entries in this blog"
+    NO_ENTRIES_MSG = u"There are no entries"
 
     name = u'NewBlogItem'
     contenttype = u'text/x.moin.wiki'
                {'name': name + u'/NewBlogEntryItem4', 'data': u"Fourth blog entry"}, ]
     entry_meta = {CONTENTTYPE: contenttype, ITEMTYPE: ITEMTYPE_BLOG_ENTRY}
 
+    def _publish_entry(self, entry, ptime, acl=None):
+        meta = self.entry_meta.copy()
+        meta[PTIME] = ptime
+        if acl is not None:
+            meta[ACL] = acl
+        update_item(entry['name'], meta, entry['data'])
+
     def test_create(self):
         item = Item.create(self.name, itemtype=ITEMTYPE_BLOG)
         item._save(self.meta, self.data, comment=self.comment)
         # still empty blog
         data_tokens = [self.data, self.NO_ENTRIES_MSG, ]
         self._test_view(self.name, data_tokens=data_tokens)
-        # publish the first three entries
-        entry_meta = self.entry_meta.copy()
-        entry_meta[PTIME] = 1000 # unix timestamp
-        update_item(self.entries[0]['name'], entry_meta, self.entries[0]['data'])
-        entry_meta[PTIME] = 3000
-        update_item(self.entries[1]['name'], entry_meta, self.entries[1]['data'])
-        entry_meta[PTIME] = 2000
-        update_item(self.entries[2]['name'], entry_meta, self.entries[2]['data'])
+        # publish the first three entries, ptime value is a UNIX timestamp
+        self._publish_entry(self.entries[0], ptime=1000)
+        self._publish_entry(self.entries[1], ptime=3000)
+        self._publish_entry(self.entries[2], ptime=2000)
         # the blog is not empty and the 4th entry is not published
         exclude_data_tokens = [self.NO_ENTRIES_MSG, self.entries[3]['data'], ]
-        self._test_view(self.name, exclude_data_tokens=exclude_data_tokens)
         # blog entries are published in reverse order relative to their PTIMEs
         ordered_data = [self.data,
                         self.entries[1]['data'],
                         self.entries[2]['data'],
                         self.entries[0]['data'], ]
         regex = re.compile(r'{0}.*{1}.*{2}.*{3}'.format(*ordered_data), re.DOTALL)
-        self._test_view(self.name, regex=regex)
+        self._test_view(self.name, exclude_data_tokens=exclude_data_tokens, regex=regex)
 
-    def test_entries_acls(self):
+    def test_filter_by_tag(self):
+        item = Item.create(self.name, itemtype=ITEMTYPE_BLOG)
+        item._save(self.meta, self.data, comment=self.comment)
+        # publish some entries with tags
+        entries_meta = [{ITEMTYPE: ITEMTYPE_BLOG_ENTRY, PTIME: 1000, TAGS: [u'foo', u'bar', u'moin', ]},
+                        {ITEMTYPE: ITEMTYPE_BLOG_ENTRY, PTIME: 3000, TAGS: [u'foo', u'bar', u'baz', ]},
+                        {ITEMTYPE: ITEMTYPE_BLOG_ENTRY, PTIME: 2000, TAGS: [u'baz', u'moin', ]}, ]
+        for i in xrange(len(entries_meta)):
+            entry = self.entries[i]
+            entry_meta = entries_meta[i]
+            item = Item.create(entry['name'], itemtype=ITEMTYPE_BLOG_ENTRY)
+            item._save(entry_meta, entry['data'], comment=self.comment)
+        # filter by non-existent tag 'non-existent'
+        data_tokens = [self.data, self.NO_ENTRIES_MSG, ]
+        exclude_data_tokens = [self.entries[0]['data'], self.entries[1]['data'], self.entries[2]['data'], ]
+        self._test_view(self.name, req_args={u'tag': u'non-existent'}, data_tokens=data_tokens, exclude_data_tokens=exclude_data_tokens)
+        # filter by tag 'moin'
+        exclude_data_tokens = [self.NO_ENTRIES_MSG, self.entries[1]['data'], ]
+        ordered_data = [self.data,
+                        self.entries[2]['data'],
+                        self.entries[0]['data'], ]
+        regex = re.compile(r'{0}.*{1}.*{2}'.format(*ordered_data), re.DOTALL)
+        self._test_view(self.name, req_args={u'tag': u'moin'}, exclude_data_tokens=exclude_data_tokens, regex=regex)
+
+    def test_filter_by_acls(self):
         item = Item.create(self.name, itemtype=ITEMTYPE_BLOG)
         item._save(self.meta, self.data, comment=self.comment)
         # store some unpublished entries
             item = Item.create(entry['name'], itemtype=ITEMTYPE_BLOG_ENTRY)
             item._save(self.entry_meta, entry['data'], comment=self.comment)
         # publish the first three entries with specific ACLs
-        entry_meta = self.entry_meta.copy()
-        entry_meta[PTIME] = 1000 # unix timestamp
-        # we are "anonymous"
-        entry_meta[ACL] = u"anonymous:read"
-        update_item(self.entries[0]['name'], entry_meta, self.entries[0]['data'])
-        entry_meta[PTIME] = 3000
-        entry_meta[ACL] = u"anonymous:read"
-        update_item(self.entries[1]['name'], entry_meta, self.entries[1]['data'])
-        entry_meta[PTIME] = 2000
+        # we are an "anonymous" user
+        self._publish_entry(self.entries[0], ptime=1000, acl=u"anonymous:read")
+        self._publish_entry(self.entries[1], ptime=3000, acl=u"anonymous:read")
         # specify no rights on the 3rd entry
-        entry_meta[ACL] = u"anonymous:"
-        update_item(self.entries[2]['name'], entry_meta, self.entries[2]['data'])
+        self._publish_entry(self.entries[2], ptime=2000, acl=u"anonymous:")
         # the blog is not empty and the 3rd entry is not displayed
         exclude_data_tokens = [self.NO_ENTRIES_MSG, self.entries[2]['data'], ]
-        self._test_view(self.name, exclude_data_tokens=exclude_data_tokens)
         ordered_data = [self.data,
                         self.entries[1]['data'],
                         self.entries[0]['data'], ]
         regex = re.compile(r'{0}.*{1}.*{2}'.format(*ordered_data), re.DOTALL)
-        self._test_view(self.name, regex=regex)
+        self._test_view(self.name, exclude_data_tokens=exclude_data_tokens, regex=regex)
 
 
 class TestBlogEntry(TestView):