django-mergeobject-experime.../tests/tests.py

95 lines
3.4 KiB
Python
Raw Normal View History

2015-05-28 04:21:04 +00:00
from django.test import TestCase
2015-05-28 18:17:22 +00:00
from .models import Person, Number, SSN, Group
2015-05-28 20:45:07 +00:00
from fkreplace import merge, OneToOneConflict, KEEP, DELETE
2015-05-28 04:21:04 +00:00
2015-05-28 12:41:38 +00:00
class MergeTests(TestCase):
def setUp(self):
self.a = Person.objects.create(name='alf')
self.b = Person.objects.create(name='bee')
self.g = Group.objects.create(name='Team Awesome')
2015-05-28 19:55:35 +00:00
def test_same_class(self):
with self.assertRaises(ValueError):
merge(self.a, self.g)
2015-05-28 19:57:20 +00:00
def test_same_object(self):
with self.assertRaises(ValueError):
# make sure this doesn't rely on is
merge(self.a, Person.objects.get(name=self.a.name))
2015-05-28 12:41:38 +00:00
def test_fk_simple(self):
2015-05-28 19:14:06 +00:00
Number.objects.create(person=self.a, number='555-1111')
Number.objects.create(person=self.a, number='555-1112')
merge(self.a, self.b)
2015-05-28 12:41:38 +00:00
# move FKs pointing at A to C
assert self.a.numbers.count() == 0
2015-05-28 19:14:06 +00:00
assert self.b.numbers.count() == 2
assert Number.objects.count() == 2
2015-05-28 12:41:38 +00:00
def test_fk_existing(self):
2015-05-28 19:14:06 +00:00
Number.objects.create(person=self.a, number='555-1112')
Number.objects.create(person=self.b, number='555-1113')
2015-05-28 12:41:38 +00:00
merge(self.a, self.b)
2015-05-28 19:14:06 +00:00
# everything now on B
2015-05-28 12:41:38 +00:00
assert self.a.numbers.count() == 0
2015-05-28 19:14:06 +00:00
assert self.b.numbers.count() == 2
assert Number.objects.count() == 2
2015-05-28 12:41:38 +00:00
def test_one2one_simple(self):
2015-05-28 19:14:06 +00:00
SSN.objects.create(person=self.a, number='1')
merge(self.a, self.b)
# move FKs pointing at A to B
#TODO: assert self.a.ssn is None
assert Person.objects.get(pk=self.b.pk).ssn.number == '1'
assert SSN.objects.count() == 1
2015-05-28 18:53:05 +00:00
2015-05-28 20:28:21 +00:00
def test_one2one_conflict(self):
SSN.objects.create(person=self.a, number='1')
2015-05-28 20:45:07 +00:00
SSN.objects.create(person=self.b, number='2')
2015-05-28 20:28:21 +00:00
with self.assertRaises(OneToOneConflict):
merge(self.a, self.b)
2015-05-28 18:53:05 +00:00
2015-05-28 20:45:07 +00:00
def test_one2one_conflict_keep(self):
SSN.objects.create(person=self.a, number='1')
SSN.objects.create(person=self.b, number='2')
merge(self.a, self.b, KEEP)
assert Person.objects.get(pk=self.a.pk).ssn.number == '1'
assert Person.objects.get(pk=self.b.pk).ssn.number == '2'
def test_one2one_conflict_delete(self):
SSN.objects.create(person=self.a, number='1')
SSN.objects.create(person=self.b, number='2')
merge(self.a, self.b, DELETE)
assert Person.objects.get(pk=self.b.pk).ssn.number == '1'
assert SSN.objects.count() == 1
2015-05-28 18:53:05 +00:00
def test_many2many_simple(self):
2015-05-28 19:14:06 +00:00
self.g.people.add(self.a)
merge(self.a, self.b)
# A's membership in G has been moved to B
assert self.g.people.get().name == self.b.name
2015-05-28 18:53:05 +00:00
assert self.a.groups.all().count() == 0
2015-05-28 19:14:06 +00:00
assert self.b.groups.all().count() == 1
2015-05-28 18:53:05 +00:00
def test_many2many_redundant(self):
2015-05-28 19:14:06 +00:00
self.g.people.add(self.a)
self.g.people.add(self.b)
2015-05-28 18:53:05 +00:00
merge(self.a, self.b)
# A's membership in G is redundant with B's
assert self.g.people.all().count() == 1
assert self.a.groups.all().count() == 0
assert self.b.groups.all().count() == 1
2015-05-28 19:14:06 +00:00
def test_many2many_same_class(self):
f = Person.objects.create(name='friend')
self.a.friends.add(f)
merge(self.a, self.b)
2015-05-28 18:53:05 +00:00
assert self.a.friends.all().count() == 0
2015-05-28 19:14:06 +00:00
assert self.b.friends.get().name == f.name
2015-05-28 20:06:48 +00:00
assert f.friends.get().name == self.b.name