1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.test import TestCase
5 from .models import ADocument
7 class DocumentModelTests(TestCase):
9 def assertTextEqual(self, given, expected):
10 return self.assertEqual(given, expected,
11 "Expected '''%s'''\n differs from text: '''%s'''" % (expected, given)
14 def test_empty_file(self):
15 doc = ADocument.objects.create()
16 self.assertTextEqual(doc.materialize(), u"")
18 def test_single_commit(self):
19 doc = ADocument.objects.create()
20 doc.commit(text=u"Ala ma kota", description="Commit #1")
21 self.assertTextEqual(doc.materialize(), u"Ala ma kota")
23 def test_chained_commits(self):
24 doc = ADocument.objects.create()
39 c1 = doc.commit(description="Commit #1", text=text1)
40 c2 = doc.commit(description="Commit #2", text=text2)
41 c3 = doc.commit(description="Commit #3", text=text3)
43 self.assertTextEqual(doc.materialize(), text3)
44 self.assertTextEqual(doc.materialize(change=c3), text3)
45 self.assertTextEqual(doc.materialize(change=c2), text2)
46 self.assertTextEqual(doc.materialize(change=c1), text1)
48 def test_parallel_commit_noconflict(self):
49 doc = ADocument.objects.create()
69 base = doc.commit(description="Commit #1", text=text1)
70 c1 = doc.commit(description="Commit #2", text=text2)
71 commits = doc.change_set.count()
72 c2 = doc.commit(description="Commit #3", text=text3, parent=base)
73 self.assertEqual(doc.change_set.count(), commits + 2,
74 u"Parallel commits should create an additional merge commit")
75 self.assertTextEqual(doc.materialize(), text_merged)
77 def test_parallel_commit_conflict(self):
78 doc = ADocument.objects.create()
103 base = doc.commit(description="Commit #1", text=text1)
104 c1 = doc.commit(description="Commit #2", text=text2)
105 commits = doc.change_set.count()
106 c2 = doc.commit(description="Commit #3", text=text3, parent=base)
107 self.assertEqual(doc.change_set.count(), commits + 2,
108 u"Parallel commits should create an additional merge commit")
109 self.assertTextEqual(doc.materialize(), text_merged)
112 def test_multiple_parallel_commits(self):
150 doc = ADocument.objects.create()
151 c1 = doc.commit(description="Commit A1", text=text_a1)
152 c2 = doc.commit(description="Commit A2", text=text_a2, parent=c1)
153 c3 = doc.commit(description="Commit B1", text=text_b1, parent=c1)
154 c4 = doc.commit(description="Commit C1", text=text_c1, parent=c1)
155 self.assertTextEqual(doc.materialize(), text_merged)
158 def test_prepend_history(self):
159 doc1 = ADocument.objects.create()
160 doc2 = ADocument.objects.create()
161 doc1.commit(text='Commit 1')
162 doc2.commit(text='Commit 2')
163 doc2.prepend_history(doc1)
164 self.assertEqual(ADocument.objects.all().count(), 1)
165 self.assertTextEqual(doc2.at_revision(1).materialize(), 'Commit 1')
166 self.assertTextEqual(doc2.materialize(), 'Commit 2')
168 def test_prepend_to_self(self):
169 doc = ADocument.objects.create()
170 doc.commit(text='Commit 1')
171 with self.assertRaises(AssertionError):
172 doc.prepend_history(doc)
173 self.assertTextEqual(doc.materialize(), 'Commit 1')