fix when there is no gallery dir
[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
12 import vstorage
13
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, text=text, author=author, comment=comment, parent=None)
60
61         saved_text, rev = self.repo.page_text(title)
62         assert_equal(saved_text, text)
63         assert_equal(rev, 0)
64
65     def test_save_merge_no_conflict(self):
66         text = u"test\ntext"
67         title = u"test title"
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)
72
73         saved_text, rev = self.repo.page_text(title)
74         assert_equal(saved_text, text)
75         assert_equal(rev, 0)
76
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"
81         title = u"test title"
82         author = u"test author"
83         comment = u"test comment"
84
85         self.repo.save_text(title=title, text=text, author=author, comment=comment, parent=None)
86
87         saved_text, rev = self.repo.page_text(title)
88         assert_equal(saved_text, text)
89         assert_equal(rev, 0)
90
91         self.repo.save_text(title=title, text=text1, author=author, comment=comment, parent=0)
92
93         saved_text, rev = self.repo.page_text(title)
94         assert_equal(saved_text, text1)
95         assert_equal(rev, 1)
96
97         self.repo.save_text(title=title, text=text2, author=author, comment=comment, parent=0)
98
99         saved_text, rev = self.repo.page_text(title)
100         # Other conflict markers placement can also be correct
101         assert_equal(saved_text, u'''\
102 text
103 test
104 <<<<<<< local
105 =======
106 text
107 >>>>>>> other
108 ''')
109
110     def test_delete(self):
111         text = u"text test"
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)
116
117         ok_(title in self.repo, "Document not in repository.")
118
119         self.repo.delete_page(title, author, comment)
120
121         ok_(title not in self.repo, "Document in repository after delete")
122
123     @raises(vstorage.DocumentNotFound)
124     def test_document_not_found(self):
125         self.repo.page_text(u'unknown entity')
126
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)
132
133     def test_history(self):
134         COMMITS = [
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"},
139         ]
140
141         for commit in COMMITS:
142             self.repo.save_text(title=u"Sample", **commit)
143
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"], [])
149
150     def test_data_revert(self):
151         COMMITS = [
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"},
156         ]
157
158         for commit in COMMITS:
159             self.repo.save_text(**commit)
160
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))
165
166         self.repo.revert(u"one", 2)
167         assert_equal(self.repo.page_text(u"one"), (u"1.3", 4))
168
169
170 class TestVSTags(object):
171
172     TITLE_1 = "Sample"
173
174     COMMITS = [
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"},
179     ]
180
181     def setUp(self):
182         self.repo_path = tempfile.mkdtemp()
183         self.repo = vstorage.VersionedStorage(self.repo_path)
184
185         # generate some history
186         for commit in self.COMMITS:
187             self.repo.save_text(title=u"Sample", **commit)
188
189         # verify
190         for n, entry in enumerate(reversed(list(self.repo.page_history(self.TITLE_1)))):
191             assert_equal(entry["tag"], [])
192
193     def tearDown(self):
194         clear_directory(self.repo_path)
195
196     def test_add_tag(self):
197         TAG_USER = "mike_the_tagger"
198         TAG_NAME = "production"
199         TAG_VERSION = 2
200
201         # Add tag
202         self.repo.add_page_tag(self.TITLE_1, TAG_VERSION, TAG_NAME, TAG_USER)
203
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])
209             else:
210                 assert_equal(entry["tag"], [])
211
212     def test_add_many_tags(self):
213         tags = [
214             (2, "production", "mike"),
215             (2, "finished", "jeremy"),
216             (0, "original", "jeremy"),
217         ]
218
219         for rev, name, user in tags:
220             self.repo.add_page_tag(self.TITLE_1, rev, name, user)
221
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))