de77d9913e4bf0cac0f79ffc4ccdc4fc47d54751
[redakcja.git] / apps / dvcs / tests / __init__.py
1 from nose.tools import *
2 from django.test import TestCase
3 from dvcs.models import Document
4
5
6 class ADocument(Document):
7     pass
8
9
10 class DocumentModelTests(TestCase):
11
12     def assertTextEqual(self, given, expected):
13         return self.assertEqual(given, expected,
14             "Expected '''%s'''\n differs from text: '''%s'''" % (expected, given)
15         )
16
17     def test_empty_file(self):
18         doc = ADocument.objects.create()
19         self.assertTextEqual(doc.materialize(), u"")
20
21     def test_single_commit(self):
22         doc = ADocument.objects.create()
23         doc.commit(text=u"Ala ma kota", description="Commit #1")
24         self.assertTextEqual(doc.materialize(), u"Ala ma kota")
25
26     def test_chained_commits(self):
27         doc = ADocument.objects.create()
28         text1 = u"""
29             Line #1
30             Line #2 is cool
31         """
32         text2 = u"""
33             Line #1
34             Line #2 is hot
35         """
36         text3 = u"""
37             Line #1
38             ... is hot
39             Line #3 ate Line #2
40         """
41
42         c1 = doc.commit(description="Commit #1", text=text1)
43         c2 = doc.commit(description="Commit #2", text=text2)
44         c3 = doc.commit(description="Commit #3", text=text3)
45
46         self.assertTextEqual(doc.materialize(), text3)
47         self.assertTextEqual(doc.materialize(change=c3), text3)
48         self.assertTextEqual(doc.materialize(change=c2), text2)
49         self.assertTextEqual(doc.materialize(change=c1), text1)
50
51     def test_parallel_commit_noconflict(self):
52         doc = ADocument.objects.create()
53         text1 = u"""
54             Line #1
55             Line #2
56         """
57         text2 = u"""
58             Line #1 is hot
59             Line #2
60         """
61         text3 = u"""
62             Line #1
63             Line #2
64             Line #3
65         """
66         text_merged = u"""
67             Line #1 is hot
68             Line #2
69             Line #3
70         """
71
72         base = doc.commit(description="Commit #1", text=text1)
73         c1 = doc.commit(description="Commit #2", text=text2)
74         commits = doc.change_set.count()
75         c2 = doc.commit(description="Commit #3", text=text3, parent=base)
76         self.assertEqual(doc.change_set.count(), commits + 2,
77             u"Parallel commits should create an additional merge commit")
78         self.assertTextEqual(doc.materialize(), text_merged)
79
80     def test_parallel_commit_conflict(self):
81         doc = ADocument.objects.create()
82         text1 = u"""
83             Line #1
84             Line #2
85             Line #3
86         """
87         text2 = u"""
88             Line #1
89             Line #2 is hot
90             Line #3
91         """
92         text3 = u"""
93             Line #1
94             Line #2 is cool
95             Line #3
96         """
97         text_merged = u"""
98             Line #1
99 <<<<<<<
100             Line #2 is hot
101 =======
102             Line #2 is cool
103 >>>>>>>
104             Line #3
105         """
106         base = doc.commit(description="Commit #1", text=text1)
107         c1 = doc.commit(description="Commit #2", text=text2)
108         commits = doc.change_set.count()
109         c2 = doc.commit(description="Commit #3", text=text3, parent=base)
110         self.assertEqual(doc.change_set.count(), commits + 2,
111             u"Parallel commits should create an additional merge commit")
112         self.assertTextEqual(doc.materialize(), text_merged)
113
114
115     def test_multiple_parallel_commits(self):
116         text_a1 = u"""
117             Line #1
118
119             Line #2
120
121             Line #3
122             """
123         text_a2 = u"""
124             Line #1 *
125
126             Line #2
127
128             Line #3
129             """
130         text_b1 = u"""
131             Line #1
132
133             Line #2 **
134
135             Line #3
136             """
137         text_c1 = u"""
138             Line #1
139
140             Line #2
141
142             Line #3 ***
143             """
144         text_merged = u"""
145             Line #1 *
146
147             Line #2 **
148
149             Line #3 ***
150             """
151
152
153         doc = ADocument.objects.create()
154         c1 = doc.commit(description="Commit A1", text=text_a1)
155         c2 = doc.commit(description="Commit A2", text=text_a2, parent=c1)
156         c3 = doc.commit(description="Commit B1", text=text_b1, parent=c1)
157         c4 = doc.commit(description="Commit C1", text=text_c1, parent=c1)
158         self.assertTextEqual(doc.materialize(), text_merged)