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
16 def clear_directory(top):
17 for root, dirs, files in os.walk(top, topdown=False):
19 os.remove(os.path.join(root, name))
21 os.rmdir(os.path.join(root, name))
28 class TestVersionedStorage(object):
30 self.repo_path = tempfile.mkdtemp()
31 self.repo = vstorage.VersionedStorage(self.repo_path)
34 clear_directory(self.repo_path)
36 def test_save_text(self):
39 author = u"test author"
40 comment = u"test comment"
42 self.repo.save_text(title = title,
43 text = text, author = author,
44 comment = comment, parent=-1)
46 saved = self.repo.open_page(title).read()
47 assert_equal(saved, text)
49 def test_save_text_noparent(self):
52 author = u"test author"
53 comment = u"test comment"
55 self.repo.save_text(title = title,
56 text = text, author = author,
57 comment = comment, parent=None)
59 saved = self.repo.open_page(title).read()
60 assert_equal(saved, text)
62 def test_save_merge_no_conflict(self):
65 author = u"test author"
66 comment = u"test comment"
67 self.repo.save_text(title = title,
68 text = text, author = author,
69 comment = comment, parent=-1)
70 self.repo.save_text(title = title,
71 text = text, author = author,
72 comment = comment, parent=-1)
73 saved = self.repo.open_page(title).read()
74 assert_equal(saved, text)
76 def test_save_merge_line_conflict(self):
77 text = u"test\ntest\n"
78 text1 = u"test\ntext\n"
79 text2 = u"text\ntest\n"
81 author = u"test author"
82 comment = u"test comment"
84 self.repo.save_text(title = title,
85 text = text, author = author,
86 comment = comment, parent=-1)
88 self.repo.save_text(title = title,
89 text = text1, author = author,
90 comment = comment, parent=0)
92 self.repo.save_text(title = title,
93 text = text2, author = author,
94 comment = comment, parent=0)
96 saved = self.repo.open_page(title).read()
98 # Other conflict markers placement can also be correct
99 assert_equal(saved, u'''\
109 def test_delete(self):
111 title = u"test title"
112 author = u"test author"
113 comment = u"test comment"
114 self.repo.save_text(title = title,
115 text = text, author = author,
116 comment = comment, parent=-1)
118 assert title in self.repo
120 self.repo.delete_page(title, author, comment)
122 assert title not in self.repo
124 @raises(vstorage.DocumentNotFound)
125 def test_document_not_found(self):
126 self.repo.open_page(u'unknown entity')
128 def test_open_existing_repository(self):
129 self.repo.save_text(title = u'Python!', text = u'ham and spam')
130 current_repo_revision = self.repo.repo_revision()
131 same_repo = vstorage.VersionedStorage(self.repo_path)
132 assert_equal(same_repo.repo_revision(), current_repo_revision)
135 def test_history(self):
137 {"author": "bunny", "text":"1", "comment": "Oh yeah!"},
138 {"author": "frank", "text":"2", "comment": "Second is the best!"},
139 {"text":"3", "comment": "Third"}, # anonymous
140 {"author": "welma", "text":"4", "comment": "Fourth"},
143 for commit in COMMITS:
144 self.repo.save_text(title = u"Sample", **commit)
146 for n, entry in enumerate(reversed(list(self.repo.page_history(u"Sample")))):
147 assert_equal(entry["version"], n)
148 assert_equal(entry["author"], COMMITS[n].get("author", "anonymous") )
149 assert_equal(entry["description"], COMMITS[n]["comment"])
150 assert_equal(entry["tag"], [])
153 class TestVSTags(object):
158 {"author": "bunny", "text":"1", "comment": "Oh yeah!"},
159 {"author": "frank", "text":"2", "comment": "Second is the best!"},
160 {"text":"3", "comment": "Third"}, # anonymous
161 {"author": "welma", "text":"4", "comment": "Fourth"},
165 self.repo_path = tempfile.mkdtemp()
166 self.repo = vstorage.VersionedStorage(self.repo_path)
168 # generate some history
169 for commit in self.COMMITS:
170 self.repo.save_text(title = u"Sample", **commit)
173 for n, entry in enumerate(reversed(list(self.repo.page_history(self.TITLE_1)))):
174 assert_equal(entry["tag"], [])
177 clear_directory(self.repo_path)
179 def test_add_tag(self):
180 TAG_USER = "mike_the_tagger"
181 TAG_NAME = "production"
185 self.repo.add_page_tag(self.TITLE_1, TAG_VERSION, TAG_NAME, TAG_USER)
187 # check history again
188 history = list(self.repo.page_history(self.TITLE_1))
189 for entry in reversed(history):
190 if entry["version"] == TAG_VERSION:
191 assert_equal(entry["tag"], [TAG_NAME])
193 assert_equal(entry["tag"], [])
195 def test_add_many_tags(self):
196 TAG_USER = "mike_the_tagger"
198 (2, "production", "mike"),
199 (2, "finished", "jeremy"),
200 (0, "original", "jeremy"),
204 for rev, name, user in tags:
205 self.repo.add_page_tag(self.TITLE_1, rev, name, user)
207 # check history again
208 history = list(self.repo.page_history(self.TITLE_1))
209 for entry in reversed(history):
210 expected = [ tag[1] for tag in tags if tag[0] == entry["version"] ]
211 assert_equal(set(entry["tag"]), set(expected))
214 if __name__ == '__main__':