2 # -*- coding: utf-8 -*-
4 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
5 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
10 from nose.tools import *
11 from nose.core import runmodule
15 def clear_directory(top):
16 for root, dirs, files in os.walk(top, topdown=False):
18 os.remove(os.path.join(root, name))
20 os.rmdir(os.path.join(root, name))
27 class TestVersionedStorage(object):
29 self.repo_path = tempfile.mkdtemp()
30 self.repo = vstorage.VersionedStorage(self.repo_path)
33 clear_directory(self.repo_path)
35 def test_save_text(self):
38 author = u"test author"
39 comment = u"test comment"
49 saved_text, rev = self.repo.page_text(title)
50 assert_equal(saved_text, text)
53 def test_save_text_noparent(self):
56 author = u"test author"
57 comment = u"test comment"
59 self.repo.save_text(title=title,
60 text=text, author=author,
61 comment=comment, parent=None)
63 saved_text, rev = self.repo.page_text(title)
64 assert_equal(saved_text, text)
67 def test_save_merge_no_conflict(self):
70 author = u"test author"
71 comment = u"test comment"
72 self.repo.save_text(title=title,
73 text=text, author=author,
74 comment=comment, parent=None)
75 self.repo.save_text(title=title,
76 text=text, author=author,
77 comment=comment, parent=None)
79 saved_text, rev = self.repo.page_text(title)
80 assert_equal(saved_text, text)
83 def test_save_merge_line_conflict(self):
84 text = u"test\ntest\n"
85 text1 = u"test\ntext\n"
86 text2 = u"text\ntest\n"
88 author = u"test author"
89 comment = u"test comment"
91 self.repo.save_text(title=title,
92 text=text, author=author,
93 comment=comment, parent=None)
95 saved_text, rev = self.repo.page_text(title)
96 assert_equal(saved_text, text)
99 self.repo.save_text(title=title,
100 text=text1, author=author,
101 comment=comment, parent=0)
103 saved_text, rev = self.repo.page_text(title)
104 assert_equal(saved_text, text1)
107 self.repo.save_text(title=title,
108 text=text2, author=author,
109 comment=comment, parent=0)
111 saved_text, rev = self.repo.page_text(title)
112 # Other conflict markers placement can also be correct
113 assert_equal(saved_text, u'''\
122 def test_delete(self):
124 title = u"test title"
125 author = u"test author"
126 comment = u"test comment"
127 self.repo.save_text(title=title,
128 text=text, author=author,
129 comment=comment, parent=None)
131 assert title in self.repo
133 self.repo.delete_page(title, author, comment)
135 assert title not in self.repo
137 @raises(vstorage.DocumentNotFound)
138 def test_document_not_found(self):
139 self.repo.page_text(u'unknown entity')
141 def test_open_existing_repository(self):
142 self.repo.save_text(title=u'Python!', text=u'ham and spam')
143 current_repo_revision = self.repo.repo_revision()
144 same_repo = vstorage.VersionedStorage(self.repo_path)
145 assert_equal(same_repo.repo_revision(), current_repo_revision)
147 def test_history(self):
149 {"author": "bunny", "text":"1", "comment": "Oh yeah!"},
150 {"author": "frank", "text":"2", "comment": "Second is the best!"},
151 {"text":"3", "comment": "Third"},
152 {"author": "welma", "text":"4", "comment": "Fourth"},
155 for commit in COMMITS:
156 self.repo.save_text(title=u"Sample", **commit)
158 for n, entry in enumerate(reversed(list(self.repo.page_history(u"Sample")))):
159 assert_equal(entry["version"], n)
160 assert_equal(entry["author"], COMMITS[n].get("author", "anonymous"))
161 assert_equal(entry["description"], COMMITS[n]["comment"])
162 assert_equal(entry["tag"], [])
165 class TestVSTags(object):
170 {"author": "bunny", "text":"1", "comment": "Oh yeah!"},
171 {"author": "frank", "text":"2", "comment": "Second is the best!"},
172 {"text":"3", "comment": "Third"},
173 {"author": "welma", "text":"4", "comment": "Fourth"},
177 self.repo_path = tempfile.mkdtemp()
178 self.repo = vstorage.VersionedStorage(self.repo_path)
180 # generate some history
181 for commit in self.COMMITS:
182 self.repo.save_text(title=u"Sample", **commit)
185 for n, entry in enumerate(reversed(list(self.repo.page_history(self.TITLE_1)))):
186 assert_equal(entry["tag"], [])
189 clear_directory(self.repo_path)
191 def test_add_tag(self):
192 TAG_USER = "mike_the_tagger"
193 TAG_NAME = "production"
197 self.repo.add_page_tag(self.TITLE_1, TAG_VERSION, TAG_NAME, TAG_USER)
199 # check history again
200 history = list(self.repo.page_history(self.TITLE_1))
201 for entry in reversed(history):
202 if entry["version"] == TAG_VERSION:
203 assert_equal(entry["tag"], [TAG_NAME])
205 assert_equal(entry["tag"], [])
207 def test_add_many_tags(self):
208 TAG_USER = "mike_the_tagger"
210 (2, "production", "mike"),
211 (2, "finished", "jeremy"),
212 (0, "original", "jeremy"),
215 for rev, name, user in tags:
216 self.repo.add_page_tag(self.TITLE_1, rev, name, user)
218 # check history again
219 history = list(self.repo.page_history(self.TITLE_1))
220 for entry in reversed(history):
221 expected = [tag[1] for tag in tags if tag[0] == entry["version"]]
222 assert_equal(set(entry["tag"]), set(expected))