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 *
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, text=text, author=author, comment=comment, parent=None)
61 saved_text, rev = self.repo.page_text(title)
62 assert_equal(saved_text, text)
65 def test_save_merge_no_conflict(self):
68 author = u"test author"
69 comment = u"test comment"
70 self.repo.save_text(title=title, text=text, author=author, comment=comment, parent=None)
71 self.repo.save_text(title=title, text=text, author=author, comment=comment, parent=None)
73 saved_text, rev = self.repo.page_text(title)
74 assert_equal(saved_text, text)
77 def test_save_merge_line_conflict(self):
78 text = u"test\ntest\n"
79 text1 = u"test\ntext\n"
80 text2 = u"text\ntest\n"
82 author = u"test author"
83 comment = u"test comment"
85 self.repo.save_text(title=title, text=text, author=author, comment=comment, parent=None)
87 saved_text, rev = self.repo.page_text(title)
88 assert_equal(saved_text, text)
91 self.repo.save_text(title=title, text=text1, author=author, comment=comment, parent=0)
93 saved_text, rev = self.repo.page_text(title)
94 assert_equal(saved_text, text1)
97 self.repo.save_text(title=title, text=text2, author=author, comment=comment, parent=0)
99 saved_text, rev = self.repo.page_text(title)
100 # Other conflict markers placement can also be correct
101 assert_equal(saved_text, u'''\
110 def test_delete(self):
112 title = u"test title"
113 author = u"test author"
114 comment = u"test comment"
115 self.repo.save_text(title=title, text=text, author=author, comment=comment, parent=None)
117 ok_(title in self.repo, "Document not in repository.")
119 self.repo.delete_page(title, author, comment)
121 ok_(title not in self.repo, "Document in repository after delete")
123 @raises(vstorage.DocumentNotFound)
124 def test_document_not_found(self):
125 self.repo.page_text(u'unknown entity')
127 def test_open_existing_repository(self):
128 self.repo.save_text(title=u'Python!', text=u'ham and spam')
129 current_repo_revision = self.repo.repo_revision()
130 same_repo = vstorage.VersionedStorage(self.repo_path)
131 assert_equal(same_repo.repo_revision(), current_repo_revision)
133 def test_history(self):
135 {"author": "bunny", "text": "1", "comment": "Oh yeah!"},
136 {"author": "frank", "text": "2", "comment": "Second is the best!"},
137 {"text": "3", "comment": "Third"},
138 {"author": "welma", "text": "4", "comment": "Fourth"},
141 for commit in COMMITS:
142 self.repo.save_text(title=u"Sample", **commit)
144 for n, entry in enumerate(reversed(list(self.repo.page_history(u"Sample")))):
145 assert_equal(entry["version"], n)
146 assert_equal(entry["author"], COMMITS[n].get("author", "anonymous"))
147 assert_equal(entry["description"], COMMITS[n]["comment"])
148 assert_equal(entry["tag"], [])
150 def test_data_revert(self):
152 {u"title": u"one", "author": "bunny", "text": "1.1", "comment": "1"},
153 {u"title": u"one", "author": "frank", "text": "1.2", "comment": "2"},
154 {u"title": u"two", "author": "bunny", "text": "2.1", "comment": "3"},
155 {u"title": u"one", "author": "frank", "text": "1.3", "comment": "4"},
158 for commit in COMMITS:
159 self.repo.save_text(**commit)
161 # now revert last change on one
162 self.repo.revert(u"one", 0)
163 assert_equal(self.repo.page_text(u"one"), (u"1.1", 3))
164 assert_equal(self.repo.page_text(u"two"), (u"2.1", 0))
166 self.repo.revert(u"one", 2)
167 assert_equal(self.repo.page_text(u"one"), (u"1.3", 4))
170 class TestVSTags(object):
175 {"author": "bunny", "text": "1", "comment": "Oh yeah!"},
176 {"author": "frank", "text": "2", "comment": "Second is the best!"},
177 {"text": "3", "comment": "Third"},
178 {"author": "welma", "text": "4", "comment": "Fourth"},
182 self.repo_path = tempfile.mkdtemp()
183 self.repo = vstorage.VersionedStorage(self.repo_path)
185 # generate some history
186 for commit in self.COMMITS:
187 self.repo.save_text(title=u"Sample", **commit)
190 for n, entry in enumerate(reversed(list(self.repo.page_history(self.TITLE_1)))):
191 assert_equal(entry["tag"], [])
194 clear_directory(self.repo_path)
196 def test_add_tag(self):
197 TAG_USER = "mike_the_tagger"
198 TAG_NAME = "production"
202 self.repo.add_page_tag(self.TITLE_1, TAG_VERSION, TAG_NAME, TAG_USER)
204 # check history again
205 history = list(self.repo.page_history(self.TITLE_1))
206 for entry in reversed(history):
207 if entry["version"] == TAG_VERSION:
208 assert_equal(entry["tag"], [TAG_NAME])
210 assert_equal(entry["tag"], [])
212 def test_add_many_tags(self):
214 (2, "production", "mike"),
215 (2, "finished", "jeremy"),
216 (0, "original", "jeremy"),
219 for rev, name, user in tags:
220 self.repo.add_page_tag(self.TITLE_1, rev, name, user)
222 # check history again
223 history = list(self.repo.page_history(self.TITLE_1))
224 for entry in reversed(history):
225 expected = [tag[1] for tag in tags if tag[0] == entry["version"]]
226 assert_equal(set(entry["tag"]), set(expected))