00df2f696518454cfbd487738debaee2aed34649
[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 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 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 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 same_repo.repo_revision() == current_repo_revision
133
134
135 if __name__ == '__main__':
136     runmodule()