Merge branch 'img-playground' of ssh://epsilon.fnp/home/rczajka/workspace/redakcja...
[redakcja.git] / apps / dvcs / tests.py
1 from django.test import TestCase
2 from dvcs.models import Change, Document
3 from django.contrib.auth.models import User
4
5 class DocumentModelTests(TestCase):
6
7     def setUp(self):
8         self.user = User.objects.create_user("tester", "tester@localhost.local")
9
10     def assertTextEqual(self, given, expected):
11         return self.assertEqual(given, expected,
12             "Expected '''%s'''\n differs from text: '''%s'''" % (expected, given)
13         )
14
15     def test_empty_file(self):
16         doc = Document.objects.create(name=u"Sample Document", creator=self.user)
17         self.assert_(doc.head is not None)
18         self.assertEqual(doc.materialize(), u"")
19
20     def test_single_commit(self):
21         doc = Document.objects.create(name=u"Sample Document", creator=self.user)
22         doc.commit(text=u"Ala ma kota", description="Commit #1", author=self.user)
23         self.assert_(doc.head is not None)
24         self.assertEqual(doc.change_set.count(), 2)
25         self.assertEqual(doc.materialize(), u"Ala ma kota")
26
27     def test_chained_commits(self):
28         doc = Document.objects.create(name=u"Sample Document", creator=self.user)
29         c1 = doc.commit(description="Commit #1", text=u"""
30             Line #1
31             Line #2 is cool
32         """, author=self.user)
33         c2 = doc.commit(description="Commit #2", text=u"""
34             Line #1
35             Line #2 is hot
36         """, author=self.user)
37         c3 = doc.commit(description="Commit #3", text=u"""
38             Line #1
39             ... is hot
40             Line #3 ate Line #2
41         """, author=self.user)
42         self.assert_(doc.head is not None)
43         self.assertEqual(doc.change_set.count(), 4)
44
45         self.assertEqual(doc.materialize(), u"""
46             Line #1
47             ... is hot
48             Line #3 ate Line #2
49         """)
50         self.assertEqual(doc.materialize(version=c3), u"""
51             Line #1
52             ... is hot
53             Line #3 ate Line #2
54         """)
55         self.assertEqual(doc.materialize(version=c2), u"""
56             Line #1
57             Line #2 is hot
58         """)
59         self.assertEqual(doc.materialize(version=c1), """
60             Line #1
61             Line #2 is cool
62         """)
63
64
65     def test_parallel_commit_noconflict(self):
66         doc = Document.objects.create(name=u"Sample Document", creator=self.user)
67         self.assert_(doc.head is not None)
68         base = doc.head
69         base = doc.commit(description="Commit #1", text=u"""
70             Line #1
71             Line #2
72 """, author=self.user)
73
74         c1 = doc.commit(description="Commit #2", text=u"""
75             Line #1 is hot
76             Line #2
77 """, parent=base, author=self.user)
78         self.assertTextEqual(c1.materialize(), u"""
79             Line #1 is hot
80             Line #2
81 """)
82         c2 = doc.commit(description="Commit #3", text=u"""
83             Line #1
84             Line #2
85             Line #3
86 """, parent=base, author=self.user)
87         self.assertEqual(doc.change_set.count(), 5)
88         self.assertTextEqual(doc.materialize(), u"""
89             Line #1 is hot
90             Line #2
91             Line #3
92 """)
93
94     def test_parallel_commit_conflict(self):
95         doc = Document.objects.create(name=u"Sample Document", creator=self.user)
96         self.assert_(doc.head is not None)
97         base = doc.head
98         base = doc.commit(description="Commit #1", text=u"""
99 Line #1
100 Line #2
101 Line #3
102 """, author=self.user)
103
104         c1 = doc.commit(description="Commit #2", text=u"""
105 Line #1
106 Line #2 is hot
107 Line #3
108 """, parent=base, author=self.user)
109         c2 = doc.commit(description="Commit #3", text=u"""
110 Line #1
111 Line #2 is cool
112 Line #3
113 """, parent=base, author=self.user)
114         self.assertEqual(doc.change_set.count(), 5)
115         self.assertTextEqual(doc.materialize(), u"""
116 Line #1
117 <<<<<<<
118 Line #2 is hot
119 =======
120 Line #2 is cool
121 >>>>>>>
122 Line #3
123 """)
124
125     def test_multiply_parallel_commits(self):
126         doc = Document.objects.create(name=u"Sample Document", creator=self.user)
127         self.assert_(doc.head is not None)
128         c1 = doc.commit(description="Commit A1", text=u"""
129 Line #1
130
131 Line #2
132
133 Line #3
134 """, author=self.user)
135         c2 = doc.commit(description="Commit A2", text=u"""
136 Line #1 *
137
138 Line #2
139
140 Line #3
141 """, author=self.user)
142         c3 = doc.commit(description="Commit B1", text=u"""
143 Line #1
144
145 Line #2 **
146
147 Line #3
148 """, parent=c1, author=self.user)
149         c4 = doc.commit(description="Commit C1", text=u"""
150 Line #1 *
151
152 Line #2
153
154 Line #3 ***
155 """, parent=c2, author=self.user)
156         self.assertEqual(doc.change_set.count(), 7)
157         self.assertTextEqual(doc.materialize(), u"""
158 Line #1 *
159
160 Line #2 **
161
162 Line #3 ***
163 """)
164