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