1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3 from nose.tools import *
4 from django.test import TestCase
5 from dvcs.models import Document
8 class ADocument(Document):
13 class DocumentModelTests(TestCase):
15 def assertTextEqual(self, given, expected):
16 return self.assertEqual(
18 "Expected '''%s'''\n differs from text: '''%s'''" % (expected, given))
20 def test_empty_file(self):
21 doc = ADocument.objects.create()
22 self.assertTextEqual(doc.materialize(), "")
24 def test_single_commit(self):
25 doc = ADocument.objects.create()
26 doc.commit(text="Ala ma kota", description="Commit #1")
27 self.assertTextEqual(doc.materialize(), "Ala ma kota")
29 def test_chained_commits(self):
30 doc = ADocument.objects.create()
45 c1 = doc.commit(description="Commit #1", text=text1)
46 c2 = doc.commit(description="Commit #2", text=text2)
47 c3 = doc.commit(description="Commit #3", text=text3)
49 self.assertTextEqual(doc.materialize(), text3)
50 self.assertTextEqual(doc.materialize(change=c3), text3)
51 self.assertTextEqual(doc.materialize(change=c2), text2)
52 self.assertTextEqual(doc.materialize(change=c1), text1)
54 def test_parallel_commit_noconflict(self):
55 doc = ADocument.objects.create()
75 base = doc.commit(description="Commit #1", text=text1)
76 c1 = doc.commit(description="Commit #2", text=text2)
77 commits = doc.change_set.count()
78 c2 = doc.commit(description="Commit #3", text=text3, parent=base)
80 doc.change_set.count(), commits + 2,
81 "Parallel commits should create an additional merge commit")
82 self.assertTextEqual(doc.materialize(), text_merged)
84 def test_parallel_commit_conflict(self):
85 doc = ADocument.objects.create()
110 base = doc.commit(description="Commit #1", text=text1)
111 c1 = doc.commit(description="Commit #2", text=text2)
112 commits = doc.change_set.count()
113 c2 = doc.commit(description="Commit #3", text=text3, parent=base)
115 doc.change_set.count(), commits + 2,
116 "Parallel commits should create an additional merge commit")
117 self.assertTextEqual(doc.materialize(), text_merged)
119 def test_multiple_parallel_commits(self):
156 doc = ADocument.objects.create()
157 c1 = doc.commit(description="Commit A1", text=text_a1)
158 c2 = doc.commit(description="Commit A2", text=text_a2, parent=c1)
159 c3 = doc.commit(description="Commit B1", text=text_b1, parent=c1)
160 c4 = doc.commit(description="Commit C1", text=text_c1, parent=c1)
161 self.assertTextEqual(doc.materialize(), text_merged)
163 def test_prepend_history(self):
164 doc1 = ADocument.objects.create()
165 doc2 = ADocument.objects.create()
166 doc1.commit(text='Commit 1')
167 doc2.commit(text='Commit 2')
168 doc2.prepend_history(doc1)
169 self.assertEqual(ADocument.objects.all().count(), 1)
170 self.assertTextEqual(doc2.at_revision(1).materialize(), 'Commit 1')
171 self.assertTextEqual(doc2.materialize(), 'Commit 2')
173 def test_prepend_to_self(self):
174 doc = ADocument.objects.create()
175 doc.commit(text='Commit 1')
176 with self.assertRaises(AssertionError):
177 doc.prepend_history(doc)
178 self.assertTextEqual(doc.materialize(), 'Commit 1')