ensure same class

This commit is contained in:
James Turk 2015-05-28 15:55:35 -04:00
parent 14e94a0c7d
commit 65f2ae7dac
3 changed files with 8 additions and 1 deletions

View File

@ -1,5 +1,8 @@
def merge(from_obj, to_obj): def merge(from_obj, to_obj):
if not isinstance(from_obj, type(to_obj)):
raise ValueError("both objects must be of the same type")
for related in from_obj._meta.get_all_related_objects(): for related in from_obj._meta.get_all_related_objects():
accessor_name = related.get_accessor_name() accessor_name = related.get_accessor_name()
varname = related.field.name varname = related.field.name

View File

@ -8,6 +8,10 @@ class MergeTests(TestCase):
self.b = Person.objects.create(name='bee') self.b = Person.objects.create(name='bee')
self.g = Group.objects.create(name='Team Awesome') self.g = Group.objects.create(name='Team Awesome')
def test_same_class(self):
with self.assertRaises(ValueError):
merge(self.a, self.g)
def test_fk_simple(self): def test_fk_simple(self):
Number.objects.create(person=self.a, number='555-1111') Number.objects.create(person=self.a, number='555-1111')
Number.objects.create(person=self.a, number='555-1112') Number.objects.create(person=self.a, number='555-1112')

View File

@ -1,5 +1,5 @@
[tox] [tox]
envlist = py27-django17, py34-django17, py27-django18, py34-django18, flake8 envlist = py27-django18, py34-django18, flake8
[testenv:flake8] [testenv:flake8]
deps = flake8 deps = flake8