ensure no self-merges

This commit is contained in:
James Turk 2015-05-28 15:57:20 -04:00
parent 65f2ae7dac
commit 8e603c6329
2 changed files with 8 additions and 0 deletions

View File

@ -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

View File

@ -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')