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