69978260fa0e2445f656cbf3f282bfb04cb789c5
[redakcja.git] / lib / wlrepo / tests / test_mercurial.py
1 # -*- encoding: utf-8 -*-
2
3 __author__= "Łukasz Rekucki"
4 __date__ = "$2009-09-18 14:43:27$"
5 __doc__ = "Tests for RAL mercurial backend."
6
7 from nose.tools import *
8
9 import wlrepo
10 from wlrepo import MercurialLibrary
11 from wlrepo.backend_mercurial import *
12
13 import os, os.path, tempfile
14 import shutil
15
16 REPO_TEMPLATES = os.path.join( os.path.dirname(__file__), 'data/repos')
17 ROOT_PATH = None
18
19 class testBasicLibrary(object):
20
21     def setUp(self):
22         self.path = tempfile.mkdtemp("", "testdir_" )
23         print self.path
24         for subdir in os.listdir(REPO_TEMPLATES):
25             shutil.copytree(REPO_TEMPLATES + '/' + subdir, self.path + '/' + subdir, False)
26
27     def tearDown(self):        
28         if self.path is not None:
29             shutil.rmtree(self.path, True)
30         pass
31    
32     def test_opening(self):
33         library = MercurialLibrary(self.path + '/cleanrepo')
34
35     def test_main_cabinett(self):
36         library = MercurialLibrary(self.path + '/cleanrepo')
37
38         mcab = library.main_cabinet
39         assert_equal(mcab.maindoc_name, '')
40
41         # @type mcab MercurialCabinet
42         doclist = mcab.documents()
43         assert_equal( list(doclist), ['valid_file'])
44
45
46     def test_read_document(self):
47         library = MercurialLibrary(self.path + '/testrepoI')
48         doc = library.main_cabinet.retrieve('valid_file')
49         
50         assert_equal(doc.read().strip(), 'Ala ma kota')
51
52     def test_read_UTF8_document(self):
53         library = MercurialLibrary(self.path + '/testrepoI')
54         doc = library.main_cabinet.retrieve('polish_file')
55
56         assert_equal(doc.read().strip(), u'Gąska!'.encode('utf-8'))
57
58     def test_write_document(self):
59         library = MercurialLibrary(self.path + '/testrepoI')
60         doc = library.main_cabinet.retrieve('valid_file')
61
62         assert_equal(doc.read().strip(), 'Ala ma kota')
63         
64         STRING = u'Gąski lubią pływać!\n'.encode('utf-8')
65         doc.write(STRING)       
66         
67         assert_equal(doc.read(), STRING)
68
69     def test_create_document(self):
70         repopath = os.path.join(self.path, 'testrepoI')
71
72         library = MercurialLibrary(repopath)
73         doc = library.main_cabinet.create("another_file", "Some text")
74         assert_equal( doc.read(), "Some text")
75         assert_true( os.path.isfile( os.path.join(repopath, "pub_another_file.xml")) )
76         
77     def test_switch_branch(self):
78         library = MercurialLibrary(self.path + '/testrepoII')
79
80         tester_cab = library.cabinet("valid_file", "tester", create=False)
81         assert_equal( list(tester_cab.documents()), ['valid_file'])
82
83     @raises(wlrepo.CabinetNotFound)
84     def test_branch_not_found(self):
85         library = MercurialLibrary(self.path + '/testrepoII')
86         tester_cab = library.cabinet("ugh", "tester", create=False)
87
88     def test_no_branches(self):
89         library = MercurialLibrary(self.path + '/testrepoII')
90         n4 = library.shelf(4)
91         n3 = library.shelf(3)
92         n2 = library.shelf(2)
93         n1 = library.shelf(1)
94         n0 = library.shelf(0)
95
96         assert_true( n3.parentof(n4) )
97         assert_false( n4.parentof(n3) )
98         assert_true( n0.parentof(n1) )
99         assert_false( n1.parentof(n0) )
100         assert_false( n0.parentof(n4) )
101
102     # def test_ancestor_of_simple(self):
103         assert_true( n3.ancestorof(n4) )
104         assert_true( n2.ancestorof(n4) )
105         assert_true( n1.ancestorof(n4) )
106         assert_true( n0.ancestorof(n4) )
107
108         assert_true( n2.ancestorof(n3) )
109         assert_true( n1.ancestorof(n3) )
110         assert_true( n0.ancestorof(n3) )
111
112         assert_false( n4.ancestorof(n4) )
113         assert_false( n4.ancestorof(n3) )
114         assert_false( n3.ancestorof(n2) )
115         assert_false( n3.ancestorof(n1) )
116         assert_false( n3.ancestorof(n0) )
117
118     # def test_common_ancestor_simple(self):
119         assert_true( n3.has_common_ancestor(n4) )
120         assert_true( n3.has_common_ancestor(n3) )
121         assert_true( n3.has_common_ancestor(n3) )
122
123
124     def test_once_branched(self):
125         library = MercurialLibrary(self.path + '/test3')
126
127         n7 = library.shelf(7)
128         n6 = library.shelf(6)
129         n5 = library.shelf(5)
130         n4 = library.shelf(4)
131         n3 = library.shelf(3)
132         n2 = library.shelf(2)
133
134         assert_true( n2.parentof(n3) )
135         assert_false( n3.parentof(n2) )
136
137         assert_true( n2.parentof(n5) )
138         assert_false( n5.parentof(n2) )
139
140         assert_false( n2.parentof(n4) )
141         assert_false( n2.parentof(n6) )
142         assert_false( n3.parentof(n5) )
143         assert_false( n5.parentof(n3) )
144
145     # def test_ancestorof_branched(self):
146         assert_true( n2.ancestorof(n7) )
147         assert_false( n7.ancestorof(n2) )
148         assert_true( n2.ancestorof(n6) )
149         assert_false( n6.ancestorof(n2) )
150         assert_true( n2.ancestorof(n5) )
151         assert_false( n5.ancestorof(n2) )
152
153         assert_false( n3.ancestorof(n5) )
154         assert_false( n5.ancestorof(n3) )
155         assert_false( n4.ancestorof(n5) )
156         assert_false( n5.ancestorof(n4) )
157         assert_false( n3.ancestorof(n7) )
158         assert_false( n7.ancestorof(n3) )
159         assert_false( n4.ancestorof(n6) )
160         assert_false( n6.ancestorof(n4) )
161
162     # def test_common_ancestor_branched(self):
163         assert_true( n2.has_common_ancestor(n4) )
164         assert_true( n2.has_common_ancestor(n7) )
165         assert_true( n2.has_common_ancestor(n6) )
166
167         # cause it's not in the right branch
168         assert_false( n5.has_common_ancestor(n3) )
169         assert_false( n7.has_common_ancestor(n4) )
170
171     def test_after_merge(self):
172         library = MercurialLibrary(self.path + '/test4')
173         n8 = library.shelf(8)
174         n7 = library.shelf(7)
175         n6 = library.shelf(6)
176
177         assert_true( n7.parentof(n8) )
178         assert_false( n8.parentof(n7) )
179         
180         assert_true( n7.ancestorof(n8) )
181         assert_true( n6.ancestorof(n8) )
182         
183
184         assert_true( n7.has_common_ancestor(n8) )
185         # cause it's not in the right branch
186         assert_false( n8.has_common_ancestor(n7) )
187
188
189     def test_after_merge_and_local_commit(self):
190         library = MercurialLibrary(self.path + '/test5b')
191         n9 = library.shelf(9)
192         n8 = library.shelf(8)
193         n7 = library.shelf(7)
194         n6 = library.shelf(6)
195
196         assert_true( n7.parentof(n8) )
197         assert_false( n8.parentof(n7) )
198
199         assert_true( n9.has_common_ancestor(n8) )
200         # cause it's not in the right branch
201         assert_false( n8.has_common_ancestor(n9) )
202
203
204     def test_merge_personal_to_default(self):
205         library = MercurialLibrary(self.path + '/test3')
206
207         main = library.shelf(2)
208         local = library.shelf(7)
209
210         document = library.document("ala", "admin")
211         shared = document.shared()
212         print document, shared
213
214         document.share("Here is my copy!")
215
216         assert_equal( document.shelf(), local) # local didn't change
217
218         
219         new_main = shared.shelf()
220         assert_not_equal( new_main, main) # main has new revision
221
222         # check for parents
223         assert_true( main.parentof(new_main) )
224         assert_true( local.parentof(new_main) )
225         
226         
227
228     def testCreateBranch(self):
229         repopath = os.path.join(self.path, 'testrepoII')
230         library = MercurialLibrary(repopath)
231
232         tester_cab = library.cabinet("anotherone", "tester", create=True)       
233         assert_equal( list(tester_cab.documents()), ['anotherone'])
234
235         
236
237