From 65f2ae7dacff994ea836aed63f7bb17668f7b0dd Mon Sep 17 00:00:00 2001 From: James Turk Date: Thu, 28 May 2015 15:55:35 -0400 Subject: [PATCH] ensure same class --- fkreplace/__init__.py | 3 +++ tests/tests.py | 4 ++++ tox.ini | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/fkreplace/__init__.py b/fkreplace/__init__.py index 4de821f..69e814d 100644 --- a/fkreplace/__init__.py +++ b/fkreplace/__init__.py @@ -1,5 +1,8 @@ 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(): accessor_name = related.get_accessor_name() varname = related.field.name diff --git a/tests/tests.py b/tests/tests.py index 648be1c..87c5701 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -8,6 +8,10 @@ class MergeTests(TestCase): self.b = Person.objects.create(name='bee') 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): Number.objects.create(person=self.a, number='555-1111') Number.objects.create(person=self.a, number='555-1112') diff --git a/tox.ini b/tox.ini index 74849f0..bdc8fc3 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py27-django17, py34-django17, py27-django18, py34-django18, flake8 +envlist = py27-django18, py34-django18, flake8 [testenv:flake8] deps = flake8