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)
64 saved_text, rev = self.repo.page_text(title)
65 assert_equal(saved_text, text)
68 def test_save_merge_no_conflict(self):
71 author = u"test author"
72 comment = u"test comment"
73 self.repo.save_text(title=title,
74 text=text, author=author,
75 comment=comment, parent=None)
76 self.repo.save_text(title=title,
77 text=text, author=author,
78 comment=comment, parent=None)
80 saved_text, rev = self.repo.page_text(title)
81 assert_equal(saved_text, text)
84 def test_save_merge_line_conflict(self):
85 text = u"test\ntest\n"
86 text1 = u"test\ntext\n"
87 text2 = u"text\ntest\n"
89 author = u"test author"
90 comment = u"test comment"
92 self.repo.save_text(title=title,
93 text=text, author=author,
94 comment=comment, parent=None)
96 saved_text, rev = self.repo.page_text(title)
97 assert_equal(saved_text, text)
100 self.repo.save_text(title=title,
101 text=text1, author=author,
102 comment=comment, parent=0)
104 saved_text, rev = self.repo.page_text(title)
105 assert_equal(saved_text, text1)
108 self.repo.save_text(title=title,
109 text=text2, author=author,
110 comment=comment, parent=0)
112 saved_text, rev = self.repo.page_text(title)
113 # Other conflict markers placement can also be correct
114 assert_equal(saved_text, u'''\
123 def test_delete(self):
125 title = u"test title"
126 author = u"test author"
127 comment = u"test comment"
128 self.repo.save_text(title=title,
129 text=text, author=author,
130 comment=comment, parent=None)
132 ok_(title in self.repo, "Document not in repository.")
134 self.repo.delete_page(title, author, comment)
136 ok_(title not in self.repo, "Document in repository after delete")
138 @raises(vstorage.DocumentNotFound)
139 def test_document_not_found(self):
140 self.repo.page_text(u'unknown entity')
142 def test_open_existing_repository(self):
143 self.repo.save_text(title=u'Python!', text=u'ham and spam')
144 current_repo_revision = self.repo.repo_revision()
145 same_repo = vstorage.VersionedStorage(self.repo_path)
146 assert_equal(same_repo.repo_revision(), current_repo_revision)
148 def test_history(self):
150 {"author": "bunny", "text":"1", "comment": "Oh yeah!"},
151 {"author": "frank", "text":"2", "comment": "Second is the best!"},
152 {"text":"3", "comment": "Third"},
153 {"author": "welma", "text":"4", "comment": "Fourth"},
156 for commit in COMMITS:
157 self.repo.save_text(title=u"Sample", **commit)
159 for n, entry in enumerate(reversed(list(self.repo.page_history(u"Sample")))):
160 assert_equal(entry["version"], n)
161 assert_equal(entry["author"], COMMITS[n].get("author", "anonymous"))
162 assert_equal(entry["description"], COMMITS[n]["comment"])
163 assert_equal(entry["tag"], [])
165 def test_data_revert(self):
167 {u"title": u"one", "author": "bunny", "text":"1.1", "comment": "1"},
168 {u"title": u"one", "author": "frank", "text":"1.2", "comment": "2"},
169 {u"title": u"two", "author": "bunny", "text":"2.1", "comment": "3"},
170 {u"title": u"one", "author": "frank", "text":"1.3", "comment": "4"},
173 for commit in COMMITS:
174 self.repo.save_text(**commit)
176 # now revert last change on one
177 self.repo.revert(u"one", 0)
178 assert_equal(self.repo.page_text(u"one"), (u"1.1", 3))
179 assert_equal(self.repo.page_text(u"two"), (u"2.1", 0))
181 self.repo.revert(u"one", 2)
182 assert_equal(self.repo.page_text(u"one"), (u"1.3", 4))
184 class TestVSTags(object):
189 {"author": "bunny", "text":"1", "comment": "Oh yeah!"},
190 {"author": "frank", "text":"2", "comment": "Second is the best!"},
191 {"text":"3", "comment": "Third"},
192 {"author": "welma", "text":"4", "comment": "Fourth"},
196 self.repo_path = tempfile.mkdtemp()
197 self.repo = vstorage.VersionedStorage(self.repo_path)
199 # generate some history
200 for commit in self.COMMITS:
201 self.repo.save_text(title=u"Sample", **commit)
204 for n, entry in enumerate(reversed(list(self.repo.page_history(self.TITLE_1)))):
205 assert_equal(entry["tag"], [])
208 clear_directory(self.repo_path)
210 def test_add_tag(self):
211 TAG_USER = "mike_the_tagger"
212 TAG_NAME = "production"
216 self.repo.add_page_tag(self.TITLE_1, TAG_VERSION, TAG_NAME, TAG_USER)
218 # check history again
219 history = list(self.repo.page_history(self.TITLE_1))
220 for entry in reversed(history):
221 if entry["version"] == TAG_VERSION:
222 assert_equal(entry["tag"], [TAG_NAME])
224 assert_equal(entry["tag"], [])
226 def test_add_many_tags(self):
227 TAG_USER = "mike_the_tagger"
229 (2, "production", "mike"),
230 (2, "finished", "jeremy"),
231 (0, "original", "jeremy"),
234 for rev, name, user in tags:
235 self.repo.add_page_tag(self.TITLE_1, rev, name, user)
237 # check history again
238 history = list(self.repo.page_history(self.TITLE_1))
239 for entry in reversed(history):
240 expected = [tag[1] for tag in tags if tag[0] == entry["version"]]
241 assert_equal(set(entry["tag"]), set(expected))