eaea46f47890aea5bf3ac5feebae5a4015f7c665
[redakcja.git] / lib / test_vstorage.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
16 def clear_directory(top):
17     for root, dirs, files in os.walk(top, topdown=False):
18         for name in files:
19             os.remove(os.path.join(root, name))
20         for name in dirs:
21             os.rmdir(os.path.join(root, name))
22     try:
23         os.removedirs(top)
24     except OSError:
25         pass
26
27
28 class TestVersionedStorage(object):
29     def setUp(self):
30         self.repo_path = tempfile.mkdtemp()
31         self.repo = vstorage.VersionedStorage(self.repo_path)
32         
33     def tearDown(self):
34         clear_directory(self.repo_path)
35     
36     def test_save_text(self):
37         text = u"test text"
38         title = u"test title"
39         author = u"test author"
40         comment = u"test comment"
41         
42         self.repo.save_text(title = title, 
43                     text = text, author = author, 
44                     comment = comment, parent=-1)
45         
46         saved = self.repo.open_page(title).read()
47         assert_equal(saved, text)
48
49     def test_save_text_noparent(self):
50         text = u"test text"
51         title = u"test title"
52         author = u"test author"
53         comment = u"test comment"
54         
55         self.repo.save_text(title = title, 
56                     text = text, author = author, 
57                     comment = comment, parent=None)
58         
59         saved = self.repo.open_page(title).read()
60         assert_equal(saved, text)
61
62     def test_save_merge_no_conflict(self):
63         text = u"test\ntext"
64         title = u"test title"
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)
75     
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"
80         title = u"test title"
81         author = u"test author"
82         comment = u"test comment"
83         
84         self.repo.save_text(title = title, 
85                     text = text, author = author, 
86                     comment = comment, parent=-1)
87         
88         self.repo.save_text(title = title, 
89                     text = text1, author = author, 
90                     comment = comment, parent=0)
91         
92         self.repo.save_text(title = title, 
93                     text = text2, author = author, 
94                     comment = comment, parent=0)
95         
96         saved = self.repo.open_page(title).read()
97         
98         # Other conflict markers placement can also be correct
99         assert_equal(saved, u'''\
100 text
101 test
102 <<<<<<< local
103 =======
104 text
105 >>>>>>> other
106 ''')
107
108
109     def test_delete(self):
110         text = u"text test"
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)
117         
118         assert title in self.repo
119         
120         self.repo.delete_page(title, author, comment)
121         
122         assert title not in self.repo
123
124     @raises(vstorage.DocumentNotFound)
125     def test_document_not_found(self):
126         self.repo.open_page(u'unknown entity')
127
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)
133         
134         
135     def test_history(self):
136         COMMITS = [
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"},            
141         ]
142         
143         for commit in COMMITS:
144             self.repo.save_text(title = u"Sample", **commit)
145         
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"], [])     
151             
152             
153 class TestVSTags(object):
154     
155     TITLE_1 = "Sample"
156     
157     COMMITS = [
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"},            
162     ]   
163     
164     def setUp(self):
165         self.repo_path = tempfile.mkdtemp()
166         self.repo = vstorage.VersionedStorage(self.repo_path)
167         
168         # generate some history
169         for commit in self.COMMITS:
170             self.repo.save_text(title = u"Sample", **commit)
171                     
172         # verify
173         for n, entry in enumerate(reversed(list(self.repo.page_history(self.TITLE_1)))):
174             assert_equal(entry["tag"], [])
175             
176     def tearDown(self):
177         clear_directory(self.repo_path)
178             
179     def test_add_tag(self):        
180         TAG_USER = "mike_the_tagger"
181         TAG_NAME = "production"
182         TAG_VERSION = 2
183             
184         # Add tag
185         self.repo.add_page_tag(self.TITLE_1, TAG_VERSION, TAG_NAME, TAG_USER)
186         
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])
192             else:
193                 assert_equal(entry["tag"], []) 
194                 
195     def test_add_many_tags(self):        
196         TAG_USER = "mike_the_tagger"
197         tags = [
198             (2, "production", "mike"),
199             (2, "finished", "jeremy"),
200             (0, "original", "jeremy"),
201         ]
202             
203         
204         for rev, name, user in tags:
205             self.repo.add_page_tag(self.TITLE_1, rev, name, user)
206         
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))
212
213
214 if __name__ == '__main__':
215     runmodule()