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