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