It's safe to edit accesskey on the list.
[redakcja.git] / lib / vstorage / tests.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
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.
6 #
7
8 import os
9 import tempfile
10 from nose.tools import *
11 from nose.core import runmodule
12
13 import vstorage
14
15 def clear_directory(top):
16     for root, dirs, files in os.walk(top, topdown=False):
17         for name in files:
18             os.remove(os.path.join(root, name))
19         for name in dirs:
20             os.rmdir(os.path.join(root, name))
21     try:
22         os.removedirs(top)
23     except OSError:
24         pass
25
26
27 class TestVersionedStorage(object):
28     def setUp(self):
29         self.repo_path = tempfile.mkdtemp()
30         self.repo = vstorage.VersionedStorage(self.repo_path)
31
32     def tearDown(self):
33         clear_directory(self.repo_path)
34
35     def test_save_text(self):
36         text = u"test text"
37         title = u"test title"
38         author = u"test author"
39         comment = u"test comment"
40
41         self.repo.save_text(
42             title=title,
43             text=text,
44             author=author,
45             comment=comment,
46             parent=None,
47         )
48
49         saved_text, rev = self.repo.page_text(title)
50         assert_equal(saved_text, text)
51         assert_equal(rev, 0)
52
53     def test_save_text_noparent(self):
54         text = u"test text"
55         title = u"test title"
56         author = u"test author"
57         comment = u"test comment"
58
59         self.repo.save_text(title=title,
60                     text=text, author=author,
61                     comment=comment, parent=None)
62
63         saved_text, rev = self.repo.page_text(title)
64         assert_equal(saved_text, text)
65         assert_equal(rev, 0)
66
67     def test_save_merge_no_conflict(self):
68         text = u"test\ntext"
69         title = u"test title"
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)
78
79         saved_text, rev = self.repo.page_text(title)
80         assert_equal(saved_text, text)
81         assert_equal(rev, 0)
82
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"
87         title = u"test title"
88         author = u"test author"
89         comment = u"test comment"
90
91         self.repo.save_text(title=title,
92                     text=text, author=author,
93                     comment=comment, parent=None)
94
95         saved_text, rev = self.repo.page_text(title)
96         assert_equal(saved_text, text)
97         assert_equal(rev, 0)
98
99         self.repo.save_text(title=title,
100                     text=text1, author=author,
101                     comment=comment, parent=0)
102
103         saved_text, rev = self.repo.page_text(title)
104         assert_equal(saved_text, text1)
105         assert_equal(rev, 1)
106
107         self.repo.save_text(title=title,
108                     text=text2, author=author,
109                     comment=comment, parent=0)
110
111         saved_text, rev = self.repo.page_text(title)
112         # Other conflict markers placement can also be correct
113         assert_equal(saved_text, u'''\
114 text
115 test
116 <<<<<<< local
117 =======
118 text
119 >>>>>>> other
120 ''')
121
122     def test_delete(self):
123         text = u"text test"
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)
130
131         assert title in self.repo
132
133         self.repo.delete_page(title, author, comment)
134
135         assert title not in self.repo
136
137     @raises(vstorage.DocumentNotFound)
138     def test_document_not_found(self):
139         self.repo.page_text(u'unknown entity')
140
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)
146
147     def test_history(self):
148         COMMITS = [
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"},
153         ]
154
155         for commit in COMMITS:
156             self.repo.save_text(title=u"Sample", **commit)
157
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"], [])
163
164
165 class TestVSTags(object):
166
167     TITLE_1 = "Sample"
168
169     COMMITS = [
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"},
174     ]
175
176     def setUp(self):
177         self.repo_path = tempfile.mkdtemp()
178         self.repo = vstorage.VersionedStorage(self.repo_path)
179
180         # generate some history
181         for commit in self.COMMITS:
182             self.repo.save_text(title=u"Sample", **commit)
183
184         # verify
185         for n, entry in enumerate(reversed(list(self.repo.page_history(self.TITLE_1)))):
186             assert_equal(entry["tag"], [])
187
188     def tearDown(self):
189         clear_directory(self.repo_path)
190
191     def test_add_tag(self):
192         TAG_USER = "mike_the_tagger"
193         TAG_NAME = "production"
194         TAG_VERSION = 2
195
196         # Add tag
197         self.repo.add_page_tag(self.TITLE_1, TAG_VERSION, TAG_NAME, TAG_USER)
198
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])
204             else:
205                 assert_equal(entry["tag"], [])
206
207     def test_add_many_tags(self):
208         TAG_USER = "mike_the_tagger"
209         tags = [
210             (2, "production", "mike"),
211             (2, "finished", "jeremy"),
212             (0, "original", "jeremy"),
213         ]
214
215         for rev, name, user in tags:
216             self.repo.add_page_tag(self.TITLE_1, rev, name, user)
217
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))