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