Fixes #382.
[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
8 import vstorage
9
10
11 def clear_directory(top):
12     for root, dirs, files in os.walk(top, topdown=False):
13         for name in files:
14             os.remove(os.path.join(root, name))
15         for name in dirs:
16             os.rmdir(os.path.join(root, name))
17     try:
18         os.removedirs(top)
19     except OSError:
20         pass
21
22
23 class TestVersionedStorage(object):
24     def setUp(self):
25         self.repo_path = tempfile.mkdtemp()
26         self.repo = vstorage.VersionedStorage(self.repo_path)
27         
28     def tearDown(self):
29         clear_directory(self.repo_path)
30     
31     def test_save_text(self):
32         text = u"test text"
33         title = u"test title"
34         author = u"test author"
35         comment = u"test comment"
36         self.repo.save_text(title, text, author, comment, parent=-1)
37         saved = self.repo.open_page(title).read()
38         assert saved == text
39
40     def test_save_text_noparent(self):
41         text = u"test text"
42         title = u"test title"
43         author = u"test author"
44         comment = u"test comment"
45         self.repo.save_text(title, text, author, comment, parent=None)
46         saved = self.repo.open_page(title).read()
47         assert saved == text
48
49     def test_save_merge_no_conflict(self):
50         text = u"test\ntext"
51         title = u"test title"
52         author = u"test author"
53         comment = u"test comment"
54         self.repo.save_text(title, text, author, comment, parent=-1)
55         self.repo.save_text(title, text, author, comment, parent=-1)
56         saved = self.repo.open_page(title).read()
57         assert saved == text
58     
59     def test_save_merge_line_conflict(self):
60         text = u"test\ntest\n"
61         text1 = u"test\ntext\n"
62         text2 = u"text\ntest\n"
63         title = u"test title"
64         author = u"test author"
65         comment = u"test comment"
66         self.repo.save_text(title, text, author, comment, parent=-1)
67         self.repo.save_text(title, text1, author, comment, parent=0)
68         self.repo.save_text(title, text2, author, comment, parent=0)
69         saved = self.repo.open_page(title).read()
70         # Other conflict markers placement can also be correct
71         assert_equal(saved, u'''\
72 text
73 test
74 <<<<<<< local
75 =======
76 text
77 >>>>>>> other
78 ''')
79
80     def test_delete(self):
81         text = u"text test"
82         title = u"test title"
83         author = u"test author"
84         comment = u"test comment"
85         self.repo.save_text(title, text, author, comment, parent=-1)
86         assert title in self.repo
87         self.repo.delete_page(title, author, comment)
88         assert title not in self.repo
89
90     @raises(vstorage.DocumentNotFound)
91     def test_document_not_found(self):
92         self.repo.open_page(u'unknown entity')
93
94     def test_open_existing_repository(self):
95         self.repo.save_text(u'Python!', u'ham and spam')
96         current_repo_revision = self.repo.repo_revision()
97         same_repo = vstorage.VersionedStorage(self.repo_path)
98         assert same_repo.repo_revision() == current_repo_revision
99