1 from django.test import TestCase
2 from .models import ADocument
4 class DocumentModelTests(TestCase):
6 def assertTextEqual(self, given, expected):
7 return self.assertEqual(given, expected,
8 "Expected '''%s'''\n differs from text: '''%s'''" % (expected, given)
11 def test_empty_file(self):
12 doc = ADocument.objects.create()
13 self.assertTextEqual(doc.materialize(), u"")
15 def test_single_commit(self):
16 doc = ADocument.objects.create()
17 doc.commit(text=u"Ala ma kota", description="Commit #1")
18 self.assertTextEqual(doc.materialize(), u"Ala ma kota")
20 def test_chained_commits(self):
21 doc = ADocument.objects.create()
36 c1 = doc.commit(description="Commit #1", text=text1)
37 c2 = doc.commit(description="Commit #2", text=text2)
38 c3 = doc.commit(description="Commit #3", text=text3)
40 self.assertTextEqual(doc.materialize(), text3)
41 self.assertTextEqual(doc.materialize(change=c3), text3)
42 self.assertTextEqual(doc.materialize(change=c2), text2)
43 self.assertTextEqual(doc.materialize(change=c1), text1)
45 def test_parallel_commit_noconflict(self):
46 doc = ADocument.objects.create()
66 base = doc.commit(description="Commit #1", text=text1)
67 c1 = doc.commit(description="Commit #2", text=text2)
68 commits = doc.change_set.count()
69 c2 = doc.commit(description="Commit #3", text=text3, parent=base)
70 self.assertEqual(doc.change_set.count(), commits + 2,
71 u"Parallel commits should create an additional merge commit")
72 self.assertTextEqual(doc.materialize(), text_merged)
74 def test_parallel_commit_conflict(self):
75 doc = ADocument.objects.create()
100 base = doc.commit(description="Commit #1", text=text1)
101 c1 = doc.commit(description="Commit #2", text=text2)
102 commits = doc.change_set.count()
103 c2 = doc.commit(description="Commit #3", text=text3, parent=base)
104 self.assertEqual(doc.change_set.count(), commits + 2,
105 u"Parallel commits should create an additional merge commit")
106 self.assertTextEqual(doc.materialize(), text_merged)
109 def test_multiple_parallel_commits(self):
147 doc = ADocument.objects.create()
148 c1 = doc.commit(description="Commit A1", text=text_a1)
149 c2 = doc.commit(description="Commit A2", text=text_a2, parent=c1)
150 c3 = doc.commit(description="Commit B1", text=text_b1, parent=c1)
151 c4 = doc.commit(description="Commit C1", text=text_c1, parent=c1)
152 self.assertTextEqual(doc.materialize(), text_merged)
155 def test_prepend_history(self):
156 doc1 = ADocument.objects.create()
157 doc2 = ADocument.objects.create()
158 doc1.commit(text='Commit 1')
159 doc2.commit(text='Commit 2')
160 doc2.prepend_history(doc1)
161 self.assertEqual(ADocument.objects.all().count(), 1)
162 self.assertTextEqual(doc2.at_revision(1).materialize(), 'Commit 1')
163 self.assertTextEqual(doc2.materialize(), 'Commit 2')
165 def test_prepend_to_self(self):
166 doc = ADocument.objects.create()
167 doc.commit(text='Commit 1')
168 with self.assertRaises(AssertionError):
169 doc.prepend_history(doc)
170 self.assertTextEqual(doc.materialize(), 'Commit 1')