1 from nose.tools import *
2 from django.test import TestCase
3 from dvcs.models import Document
6 class ADocument(Document):
11 class DocumentModelTests(TestCase):
13 def assertTextEqual(self, given, expected):
14 return self.assertEqual(given, expected,
15 "Expected '''%s'''\n differs from text: '''%s'''" % (expected, given)
18 def test_empty_file(self):
19 doc = ADocument.objects.create()
20 self.assertTextEqual(doc.materialize(), u"")
22 def test_single_commit(self):
23 doc = ADocument.objects.create()
24 doc.commit(text=u"Ala ma kota", description="Commit #1")
25 self.assertTextEqual(doc.materialize(), u"Ala ma kota")
27 def test_chained_commits(self):
28 doc = ADocument.objects.create()
43 c1 = doc.commit(description="Commit #1", text=text1)
44 c2 = doc.commit(description="Commit #2", text=text2)
45 c3 = doc.commit(description="Commit #3", text=text3)
47 self.assertTextEqual(doc.materialize(), text3)
48 self.assertTextEqual(doc.materialize(change=c3), text3)
49 self.assertTextEqual(doc.materialize(change=c2), text2)
50 self.assertTextEqual(doc.materialize(change=c1), text1)
52 def test_parallel_commit_noconflict(self):
53 doc = ADocument.objects.create()
73 base = doc.commit(description="Commit #1", text=text1)
74 c1 = doc.commit(description="Commit #2", text=text2)
75 commits = doc.change_set.count()
76 c2 = doc.commit(description="Commit #3", text=text3, parent=base)
77 self.assertEqual(doc.change_set.count(), commits + 2,
78 u"Parallel commits should create an additional merge commit")
79 self.assertTextEqual(doc.materialize(), text_merged)
81 def test_parallel_commit_conflict(self):
82 doc = ADocument.objects.create()
107 base = doc.commit(description="Commit #1", text=text1)
108 c1 = doc.commit(description="Commit #2", text=text2)
109 commits = doc.change_set.count()
110 c2 = doc.commit(description="Commit #3", text=text3, parent=base)
111 self.assertEqual(doc.change_set.count(), commits + 2,
112 u"Parallel commits should create an additional merge commit")
113 self.assertTextEqual(doc.materialize(), text_merged)
116 def test_multiple_parallel_commits(self):
154 doc = ADocument.objects.create()
155 c1 = doc.commit(description="Commit A1", text=text_a1)
156 c2 = doc.commit(description="Commit A2", text=text_a2, parent=c1)
157 c3 = doc.commit(description="Commit B1", text=text_b1, parent=c1)
158 c4 = doc.commit(description="Commit C1", text=text_c1, parent=c1)
159 self.assertTextEqual(doc.materialize(), text_merged)
162 def test_prepend_history(self):
163 doc1 = ADocument.objects.create()
164 doc2 = ADocument.objects.create()
165 doc1.commit(text='Commit 1')
166 doc2.commit(text='Commit 2')
167 doc2.prepend_history(doc1)
168 self.assertEqual(ADocument.objects.all().count(), 1)
169 self.assertTextEqual(doc2.at_revision(1).materialize(), 'Commit 1')
170 self.assertTextEqual(doc2.materialize(), 'Commit 2')
172 def test_prepend_to_self(self):
173 doc = ADocument.objects.create()
174 doc.commit(text='Commit 1')
175 with self.assertRaises(AssertionError):
176 doc.prepend_history(doc)
177 self.assertTextEqual(doc.materialize(), 'Commit 1')