87b9a949cf2f7f7594df0aa1a1fa8993130e872d
[redakcja.git] / lib / test_vstorage.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 import os
5 import tempfile
6 from nose.tools import *
7 from nose.core import runmodule
8
9 import vstorage
10
11
12 def clear_directory(top):
13     for root, dirs, files in os.walk(top, topdown=False):
14         for name in files:
15             os.remove(os.path.join(root, name))
16         for name in dirs:
17             os.rmdir(os.path.join(root, name))
18     try:
19         os.removedirs(top)
20     except OSError:
21         pass
22
23
24 class TestVersionedStorage(object):
25     def setUp(self):
26         self.repo_path = tempfile.mkdtemp()
27         self.repo = vstorage.VersionedStorage(self.repo_path)
28         
29     def tearDown(self):
30         clear_directory(self.repo_path)
31     
32     def test_save_text(self):
33         text = u"test text"
34         title = u"test title"
35         author = u"test author"
36         comment = u"test comment"
37         
38         self.repo.save_text(title = title, 
39                     text = text, author = author, 
40                     comment = comment, parent=-1)
41         
42         saved = self.repo.open_page(title).read()
43         assert saved == text
44
45     def test_save_text_noparent(self):
46         text = u"test text"
47         title = u"test title"
48         author = u"test author"
49         comment = u"test comment"
50         
51         self.repo.save_text(title = title, 
52                     text = text, author = author, 
53                     comment = comment, parent=None)
54         
55         saved = self.repo.open_page(title).read()
56         assert saved == text
57
58     def test_save_merge_no_conflict(self):
59         text = u"test\ntext"
60         title = u"test title"
61         author = u"test author"
62         comment = u"test comment"
63         self.repo.save_text(title = title, 
64                     text = text, author = author, 
65                     comment = comment, parent=-1)
66         self.repo.save_text(title = title, 
67                     text = text, author = author, 
68                     comment = comment, parent=-1)
69         saved = self.repo.open_page(title).read()
70         assert saved == text
71     
72     def test_save_merge_line_conflict(self):
73         text = u"test\ntest\n"
74         text1 = u"test\ntext\n"
75         text2 = u"text\ntest\n"
76         title = u"test title"
77         author = u"test author"
78         comment = u"test comment"
79         
80         self.repo.save_text(title = title, 
81                     text = text, author = author, 
82                     comment = comment, parent=-1)
83         
84         self.repo.save_text(title = title, 
85                     text = text1, author = author, 
86                     comment = comment, parent=0)
87         
88         self.repo.save_text(title = title, 
89                     text = text2, author = author, 
90                     comment = comment, parent=0)
91         
92         saved = self.repo.open_page(title).read()
93         
94         # Other conflict markers placement can also be correct
95         assert_equal(saved, u'''\
96 text
97 test
98 <<<<<<< local
99 =======
100 text
101 >>>>>>> other
102 ''')
103
104
105     def test_delete(self):
106         text = u"text test"
107         title = u"test title"
108         author = u"test author"
109         comment = u"test comment"
110         self.repo.save_text(title = title, 
111                     text = text, author = author, 
112                     comment = comment, parent=-1)
113         
114         assert title in self.repo
115         
116         self.repo.delete_page(title, author, comment)
117         
118         assert title not in self.repo
119
120     @raises(vstorage.DocumentNotFound)
121     def test_document_not_found(self):
122         self.repo.open_page(u'unknown entity')
123
124     def test_open_existing_repository(self):
125         self.repo.save_text(title = u'Python!', text = u'ham and spam')
126         current_repo_revision = self.repo.repo_revision()
127         same_repo = vstorage.VersionedStorage(self.repo_path)
128         assert same_repo.repo_revision() == current_repo_revision
129
130
131 if __name__ == '__main__':
132     runmodule()