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