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
18 def clear_directory(top):
19 for root, dirs, files in os.walk(top, topdown=False):
21 os.remove(os.path.join(root, name))
23 os.rmdir(os.path.join(root, name))
30 class TestVersionedStorage(object):
32 self.repo_path = tempfile.mkdtemp()
33 self.repo = vstorage.VersionedStorage(self.repo_path)
36 clear_directory(self.repo_path)
38 def test_save_text(self):
41 author = u"test author"
42 comment = u"test comment"
52 saved = self.repo.open_page(title).read()
53 assert_equal(saved, text)
55 def test_save_text_noparent(self):
58 author = u"test author"
59 comment = u"test comment"
61 self.repo.save_text(title=title,
62 text=text, author=author,
63 comment=comment, parent=None)
65 saved = self.repo.open_page(title).read()
66 assert_equal(saved, 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=NULL_PARENT)
76 self.repo.save_text(title=title,
77 text=text, author=author,
78 comment=comment, parent=NULL_PARENT)
79 saved = self.repo.open_page(title).read()
80 assert_equal(saved, text)
82 def test_save_merge_line_conflict(self):
83 text = u"test\ntest\n"
84 text1 = u"test\ntext\n"
85 text2 = u"text\ntest\n"
87 author = u"test author"
88 comment = u"test comment"
90 self.repo.save_text(title=title,
91 text=text, author=author,
92 comment=comment, parent=NULL_PARENT)
94 self.repo.save_text(title=title,
95 text=text1, author=author,
96 comment=comment, parent=0)
98 self.repo.save_text(title=title,
99 text=text2, author=author,
100 comment=comment, parent=0)
102 saved = self.repo.open_page(title).read()
104 # Other conflict markers placement can also be correct
105 assert_equal(saved, u'''\
114 def test_delete(self):
116 title = u"test title"
117 author = u"test author"
118 comment = u"test comment"
119 self.repo.save_text(title=title,
120 text=text, author=author,
121 comment=comment, parent=NULL_PARENT)
123 assert title in self.repo
125 self.repo.delete_page(title, author, comment)
127 assert title not in self.repo
129 @raises(vstorage.DocumentNotFound)
130 def test_document_not_found(self):
131 self.repo.open_page(u'unknown entity')
133 def test_open_existing_repository(self):
134 self.repo.save_text(title=u'Python!', text=u'ham and spam')
135 current_repo_revision = self.repo.repo_revision()
136 same_repo = vstorage.VersionedStorage(self.repo_path)
137 assert_equal(same_repo.repo_revision(), current_repo_revision)
139 def test_history(self):
141 {"author": "bunny", "text":"1", "comment": "Oh yeah!"},
142 {"author": "frank", "text":"2", "comment": "Second is the best!"},
143 {"text":"3", "comment": "Third"},
144 {"author": "welma", "text":"4", "comment": "Fourth"},
147 for commit in COMMITS:
148 self.repo.save_text(title=u"Sample", **commit)
150 for n, entry in enumerate(reversed(list(self.repo.page_history(u"Sample")))):
151 assert_equal(entry["version"], n)
152 assert_equal(entry["author"], COMMITS[n].get("author", "anonymous"))
153 assert_equal(entry["description"], COMMITS[n]["comment"])
154 assert_equal(entry["tag"], [])
157 class TestVSTags(object):
162 {"author": "bunny", "text":"1", "comment": "Oh yeah!"},
163 {"author": "frank", "text":"2", "comment": "Second is the best!"},
164 {"text":"3", "comment": "Third"},
165 {"author": "welma", "text":"4", "comment": "Fourth"},
169 self.repo_path = tempfile.mkdtemp()
170 self.repo = vstorage.VersionedStorage(self.repo_path)
172 # generate some history
173 for commit in self.COMMITS:
174 self.repo.save_text(title=u"Sample", **commit)
177 for n, entry in enumerate(reversed(list(self.repo.page_history(self.TITLE_1)))):
178 assert_equal(entry["tag"], [])
181 clear_directory(self.repo_path)
183 def test_add_tag(self):
184 TAG_USER = "mike_the_tagger"
185 TAG_NAME = "production"
189 self.repo.add_page_tag(self.TITLE_1, TAG_VERSION, TAG_NAME, TAG_USER)
191 # check history again
192 history = list(self.repo.page_history(self.TITLE_1))
193 for entry in reversed(history):
194 if entry["version"] == TAG_VERSION:
195 assert_equal(entry["tag"], [TAG_NAME])
197 assert_equal(entry["tag"], [])
199 def test_add_many_tags(self):
200 TAG_USER = "mike_the_tagger"
202 (2, "production", "mike"),
203 (2, "finished", "jeremy"),
204 (0, "original", "jeremy"),
207 for rev, name, user in tags:
208 self.repo.add_page_tag(self.TITLE_1, rev, name, user)
210 # check history again
211 history = list(self.repo.page_history(self.TITLE_1))
212 for entry in reversed(history):
213 expected = [tag[1] for tag in tags if tag[0] == entry["version"]]
214 assert_equal(set(entry["tag"]), set(expected))
217 if __name__ == '__main__':