From 9899347641b2d3b4457cc99203a2b06504b32a16 Mon Sep 17 00:00:00 2001 From: Will Koster Date: Wed, 25 May 2016 15:33:35 -0500 Subject: Fixed #26638 -- Allowed callable arguments for QuerySet.get_or_create()/update_or_create() defaults. --- tests/get_or_create/tests.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'tests') diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py index 0f790e67b2..84f8261a92 100644 --- a/tests/get_or_create/tests.py +++ b/tests/get_or_create/tests.py @@ -146,6 +146,25 @@ class GetOrCreateTests(TestCase): self.assertFalse(created) self.assertEqual(obj, obj2) + def test_callable_defaults(self): + """ + Callables in `defaults` are evaluated if the instance is created. + """ + obj, created = Person.objects.get_or_create( + first_name="George", + defaults={"last_name": "Harrison", "birthday": lambda: date(1943, 2, 25)}, + ) + self.assertTrue(created) + self.assertEqual(date(1943, 2, 25), obj.birthday) + + def test_callable_defaults_not_called(self): + def raise_exception(): + raise AssertionError + obj, created = Person.objects.get_or_create( + first_name="John", last_name="Lennon", + defaults={"birthday": lambda: raise_exception()}, + ) + class GetOrCreateTestsWithManualPKs(TestCase): @@ -387,3 +406,10 @@ class UpdateOrCreateTests(TestCase): ) self.assertFalse(created) self.assertEqual(obj.defaults, 'another testing') + + def test_update_callable_default(self): + obj, created = Person.objects.update_or_create( + first_name='George', last_name='Harrison', + defaults={'birthday': lambda: date(1943, 2, 25)}, + ) + self.assertEqual(obj.birthday, date(1943, 2, 25)) -- cgit v1.3