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