1 from nose.tools import *
2 from django.test import TestCase
3 from .models import ADocument
5 class DocumentModelTests(TestCase):
7 def assertTextEqual(self, given, expected):
8 return self.assertEqual(given, expected,
9 "Expected '''%s'''\n differs from text: '''%s'''" % (expected, given)
12 def test_empty_file(self):
14 doc = ADocument.objects.create()
15 self.assertTextEqual(doc.materialize(), u"")
17 def test_single_commit(self):
18 doc = ADocument.objects.create()
19 doc.commit(text=u"Ala ma kota", description="Commit #1")
20 self.assertTextEqual(doc.materialize(), u"Ala ma kota")
22 def test_chained_commits(self):
23 doc = ADocument.objects.create()
38 c1 = doc.commit(description="Commit #1", text=text1)
39 c2 = doc.commit(description="Commit #2", text=text2)
40 c3 = doc.commit(description="Commit #3", text=text3)
42 self.assertTextEqual(doc.materialize(), text3)
43 self.assertTextEqual(doc.materialize(change=c3), text3)
44 self.assertTextEqual(doc.materialize(change=c2), text2)
45 self.assertTextEqual(doc.materialize(change=c1), text1)
47 def test_parallel_commit_noconflict(self):
48 doc = ADocument.objects.create()
68 base = doc.commit(description="Commit #1", text=text1)
69 c1 = doc.commit(description="Commit #2", text=text2)
70 commits = doc.change_set.count()
71 c2 = doc.commit(description="Commit #3", text=text3, parent=base)
72 self.assertEqual(doc.change_set.count(), commits + 2,
73 u"Parallel commits should create an additional merge commit")
74 self.assertTextEqual(doc.materialize(), text_merged)
76 def test_parallel_commit_conflict(self):
77 doc = ADocument.objects.create()
102 base = doc.commit(description="Commit #1", text=text1)
103 c1 = doc.commit(description="Commit #2", text=text2)
104 commits = doc.change_set.count()
105 c2 = doc.commit(description="Commit #3", text=text3, parent=base)
106 self.assertEqual(doc.change_set.count(), commits + 2,
107 u"Parallel commits should create an additional merge commit")
108 self.assertTextEqual(doc.materialize(), text_merged)
111 def test_multiple_parallel_commits(self):
149 doc = ADocument.objects.create()
150 c1 = doc.commit(description="Commit A1", text=text_a1)
151 c2 = doc.commit(description="Commit A2", text=text_a2, parent=c1)
152 c3 = doc.commit(description="Commit B1", text=text_b1, parent=c1)
153 c4 = doc.commit(description="Commit C1", text=text_c1, parent=c1)
154 self.assertTextEqual(doc.materialize(), text_merged)
157 def test_prepend_history(self):
158 doc1 = ADocument.objects.create()
159 doc2 = ADocument.objects.create()
160 doc1.commit(text='Commit 1')
161 doc2.commit(text='Commit 2')
162 doc2.prepend_history(doc1)
163 self.assertEqual(ADocument.objects.all().count(), 1)
164 self.assertTextEqual(doc2.at_revision(1).materialize(), 'Commit 1')
165 self.assertTextEqual(doc2.materialize(), 'Commit 2')
167 def test_prepend_to_self(self):
168 doc = ADocument.objects.create()
169 doc.commit(text='Commit 1')
170 with self.assertRaises(AssertionError):
171 doc.prepend_history(doc)
172 self.assertTextEqual(doc.materialize(), 'Commit 1')