Require PIL.
[redakcja.git] / lib / test_vstorage.py
index 00df2f6..23375f0 100644 (file)
@@ -2,7 +2,7 @@
 # -*- coding: utf-8 -*-
 #
 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
 # -*- coding: utf-8 -*-
 #
 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
-# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.  
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 
 import os
 #
 
 import os
@@ -12,6 +12,8 @@ from nose.core import runmodule
 
 import vstorage
 
 
 import vstorage
 
+NULL_PARENT = -1
+
 
 def clear_directory(top):
     for root, dirs, files in os.walk(top, topdown=False):
 
 def clear_directory(top):
     for root, dirs, files in os.walk(top, topdown=False):
@@ -29,50 +31,54 @@ class TestVersionedStorage(object):
     def setUp(self):
         self.repo_path = tempfile.mkdtemp()
         self.repo = vstorage.VersionedStorage(self.repo_path)
     def setUp(self):
         self.repo_path = tempfile.mkdtemp()
         self.repo = vstorage.VersionedStorage(self.repo_path)
-        
+
     def tearDown(self):
         clear_directory(self.repo_path)
     def tearDown(self):
         clear_directory(self.repo_path)
-    
+
     def test_save_text(self):
         text = u"test text"
         title = u"test title"
         author = u"test author"
         comment = u"test comment"
     def test_save_text(self):
         text = u"test text"
         title = u"test title"
         author = u"test author"
         comment = u"test comment"
-        
-        self.repo.save_text(title = title, 
-                    text = text, author = author, 
-                    comment = comment, parent=-1)
-        
+
+        self.repo.save_text(
+            title=title,
+            text=text,
+            author=author,
+            comment=comment,
+            parent=NULL_PARENT,
+        )
+
         saved = self.repo.open_page(title).read()
         saved = self.repo.open_page(title).read()
-        assert saved == text
+        assert_equal(saved, text)
 
     def test_save_text_noparent(self):
         text = u"test text"
         title = u"test title"
         author = u"test author"
         comment = u"test comment"
 
     def test_save_text_noparent(self):
         text = u"test text"
         title = u"test title"
         author = u"test author"
         comment = u"test comment"
-        
-        self.repo.save_text(title = title, 
-                    text = text, author = author, 
-                    comment = comment, parent=None)
-        
+
+        self.repo.save_text(title=title,
+                    text=text, author=author,
+                    comment=comment, parent=None)
+
         saved = self.repo.open_page(title).read()
         saved = self.repo.open_page(title).read()
-        assert saved == text
+        assert_equal(saved, text)
 
     def test_save_merge_no_conflict(self):
         text = u"test\ntext"
         title = u"test title"
         author = u"test author"
         comment = u"test comment"
 
     def test_save_merge_no_conflict(self):
         text = u"test\ntext"
         title = u"test title"
         author = u"test author"
         comment = u"test comment"
-        self.repo.save_text(title = title, 
-                    text = text, author = author, 
-                    comment = comment, parent=-1)
-        self.repo.save_text(title = title, 
-                    text = text, author = author, 
-                    comment = comment, parent=-1)
+        self.repo.save_text(title=title,
+                    text=text, author=author,
+                    comment=comment, parent=NULL_PARENT)
+        self.repo.save_text(title=title,
+                    text=text, author=author,
+                    comment=comment, parent=NULL_PARENT)
         saved = self.repo.open_page(title).read()
         saved = self.repo.open_page(title).read()
-        assert saved == text
-    
+        assert_equal(saved, text)
+
     def test_save_merge_line_conflict(self):
         text = u"test\ntest\n"
         text1 = u"test\ntext\n"
     def test_save_merge_line_conflict(self):
         text = u"test\ntest\n"
         text1 = u"test\ntext\n"
@@ -80,21 +86,21 @@ class TestVersionedStorage(object):
         title = u"test title"
         author = u"test author"
         comment = u"test comment"
         title = u"test title"
         author = u"test author"
         comment = u"test comment"
-        
-        self.repo.save_text(title = title, 
-                    text = text, author = author, 
-                    comment = comment, parent=-1)
-        
-        self.repo.save_text(title = title, 
-                    text = text1, author = author, 
-                    comment = comment, parent=0)
-        
-        self.repo.save_text(title = title, 
-                    text = text2, author = author, 
-                    comment = comment, parent=0)
-        
+
+        self.repo.save_text(title=title,
+                    text=text, author=author,
+                    comment=comment, parent=NULL_PARENT)
+
+        self.repo.save_text(title=title,
+                    text=text1, author=author,
+                    comment=comment, parent=0)
+
+        self.repo.save_text(title=title,
+                    text=text2, author=author,
+                    comment=comment, parent=0)
+
         saved = self.repo.open_page(title).read()
         saved = self.repo.open_page(title).read()
-        
+
         # Other conflict markers placement can also be correct
         assert_equal(saved, u'''\
 text
         # Other conflict markers placement can also be correct
         assert_equal(saved, u'''\
 text
@@ -105,20 +111,19 @@ text
 >>>>>>> other
 ''')
 
 >>>>>>> other
 ''')
 
-
     def test_delete(self):
         text = u"text test"
         title = u"test title"
         author = u"test author"
         comment = u"test comment"
     def test_delete(self):
         text = u"text test"
         title = u"test title"
         author = u"test author"
         comment = u"test comment"
-        self.repo.save_text(title = title, 
-                    text = text, author = author, 
-                    comment = comment, parent=-1)
-        
+        self.repo.save_text(title=title,
+                    text=text, author=author,
+                    comment=comment, parent=NULL_PARENT)
+
         assert title in self.repo
         assert title in self.repo
-        
+
         self.repo.delete_page(title, author, comment)
         self.repo.delete_page(title, author, comment)
-        
+
         assert title not in self.repo
 
     @raises(vstorage.DocumentNotFound)
         assert title not in self.repo
 
     @raises(vstorage.DocumentNotFound)
@@ -126,11 +131,88 @@ text
         self.repo.open_page(u'unknown entity')
 
     def test_open_existing_repository(self):
         self.repo.open_page(u'unknown entity')
 
     def test_open_existing_repository(self):
-        self.repo.save_text(title = u'Python!', text = u'ham and spam')
+        self.repo.save_text(title=u'Python!', text=u'ham and spam')
         current_repo_revision = self.repo.repo_revision()
         same_repo = vstorage.VersionedStorage(self.repo_path)
         current_repo_revision = self.repo.repo_revision()
         same_repo = vstorage.VersionedStorage(self.repo_path)
-        assert same_repo.repo_revision() == current_repo_revision
+        assert_equal(same_repo.repo_revision(), current_repo_revision)
+
+    def test_history(self):
+        COMMITS = [
+            {"author": "bunny", "text":"1", "comment": "Oh yeah!"},
+            {"author": "frank", "text":"2", "comment": "Second is the best!"},
+            {"text":"3", "comment": "Third"},
+            {"author": "welma", "text":"4", "comment": "Fourth"},
+        ]
+
+        for commit in COMMITS:
+            self.repo.save_text(title=u"Sample", **commit)
+
+        for n, entry in enumerate(reversed(list(self.repo.page_history(u"Sample")))):
+            assert_equal(entry["version"], n)
+            assert_equal(entry["author"], COMMITS[n].get("author", "anonymous"))
+            assert_equal(entry["description"], COMMITS[n]["comment"])
+            assert_equal(entry["tag"], [])
+
+
+class TestVSTags(object):
+
+    TITLE_1 = "Sample"
+
+    COMMITS = [
+        {"author": "bunny", "text":"1", "comment": "Oh yeah!"},
+        {"author": "frank", "text":"2", "comment": "Second is the best!"},
+        {"text":"3", "comment": "Third"},
+        {"author": "welma", "text":"4", "comment": "Fourth"},
+    ]
+
+    def setUp(self):
+        self.repo_path = tempfile.mkdtemp()
+        self.repo = vstorage.VersionedStorage(self.repo_path)
+
+        # generate some history
+        for commit in self.COMMITS:
+            self.repo.save_text(title=u"Sample", **commit)
+
+        # verify
+        for n, entry in enumerate(reversed(list(self.repo.page_history(self.TITLE_1)))):
+            assert_equal(entry["tag"], [])
+
+    def tearDown(self):
+        clear_directory(self.repo_path)
+
+    def test_add_tag(self):
+        TAG_USER = "mike_the_tagger"
+        TAG_NAME = "production"
+        TAG_VERSION = 2
+
+        # Add tag
+        self.repo.add_page_tag(self.TITLE_1, TAG_VERSION, TAG_NAME, TAG_USER)
+
+        # check history again
+        history = list(self.repo.page_history(self.TITLE_1))
+        for entry in reversed(history):
+            if entry["version"] == TAG_VERSION:
+                assert_equal(entry["tag"], [TAG_NAME])
+            else:
+                assert_equal(entry["tag"], [])
+
+    def test_add_many_tags(self):
+        TAG_USER = "mike_the_tagger"
+        tags = [
+            (2, "production", "mike"),
+            (2, "finished", "jeremy"),
+            (0, "original", "jeremy"),
+        ]
+
+        for rev, name, user in tags:
+            self.repo.add_page_tag(self.TITLE_1, rev, name, user)
+
+        # check history again
+        history = list(self.repo.page_history(self.TITLE_1))
+        for entry in reversed(history):
+            expected = [tag[1] for tag in tags if tag[0] == entry["version"]]
+            assert_equal(set(entry["tag"]), set(expected))
 
 
 if __name__ == '__main__':
 
 
 if __name__ == '__main__':
-    runmodule()
\ No newline at end of file
+    runmodule()