From 8e603c6329795d43a7c52783126942e2f2b91aca Mon Sep 17 00:00:00 2001 From: James Turk Date: Thu, 28 May 2015 15:57:20 -0400 Subject: [PATCH] ensure no self-merges --- fkreplace/__init__.py | 3 +++ tests/tests.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/fkreplace/__init__.py b/fkreplace/__init__.py index 69e814d..5b9f8a4 100644 --- a/fkreplace/__init__.py +++ b/fkreplace/__init__.py @@ -3,6 +3,9 @@ def merge(from_obj, to_obj): if not isinstance(from_obj, type(to_obj)): raise ValueError("both objects must be of the same type") + if from_obj.pk == to_obj.pk: + raise ValueError("cannot merge object with itself") + 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 87c5701..f0fc2f9 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -12,6 +12,11 @@ class MergeTests(TestCase): with self.assertRaises(ValueError): merge(self.a, self.g) + 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)) + def test_fk_simple(self): Number.objects.create(person=self.a, number='555-1111') Number.objects.create(person=self.a, number='555-1112')