diff options
author | Viktor Danyliuk <v.v.danyliuk@gmail.com> | 2018-07-17 05:08:43 +0300 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2018-07-16 22:08:43 -0400 |
commit | 6ae7aaa7d6eb880043c4c80009b36e2287bfcb92 (patch) | |
tree | ad7628988f9ccf5f56dd181aa710cd15f82485b6 /tests/get_or_create | |
parent | 4d48ddd8f93800a80330ec1dee7b7d4afe6ea95a (diff) | |
download | django-6ae7aaa7d6eb880043c4c80009b36e2287bfcb92.tar.gz |
Fixed #29413 -- Prevented evaluation of QuerySet.get_or_create()/update_or_create() defaults unless needed.
Removed the logic added in 81e05a418dc6f347d9627373288e773c477a9fe0 which
was obsolete since dbffffa7dc95fc62cbecfd00284bde62ee796f48.
Diffstat (limited to 'tests/get_or_create')
-rw-r--r-- | tests/get_or_create/tests.py | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py index 1aed318608..e4647d2ab5 100644 --- a/tests/get_or_create/tests.py +++ b/tests/get_or_create/tests.py @@ -5,9 +5,8 @@ from threading import Thread from django.core.exceptions import FieldError from django.db import DatabaseError, IntegrityError, connection -from django.test import ( - SimpleTestCase, TestCase, TransactionTestCase, skipUnlessDBFeature, -) +from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature +from django.utils.functional import lazy from .models import ( Author, Book, DefaultPerson, ManualPrimaryKeyTest, Person, Profile, @@ -178,6 +177,15 @@ class GetOrCreateTests(TestCase): defaults={"birthday": lambda: raise_exception()}, ) + def test_defaults_not_evaluated_unless_needed(self): + """`defaults` aren't evaluated if the instance isn't created.""" + def raise_exception(): + raise AssertionError + obj, created = Person.objects.get_or_create( + first_name='John', defaults=lazy(raise_exception, object)(), + ) + self.assertFalse(created) + class GetOrCreateTestsWithManualPKs(TestCase): @@ -443,6 +451,19 @@ class UpdateOrCreateTests(TestCase): self.assertIs(created, False) self.assertEqual(obj.last_name, 'NotHarrison') + def test_defaults_not_evaluated_unless_needed(self): + """`defaults` aren't evaluated if the instance isn't created.""" + Person.objects.create( + first_name='John', last_name='Lennon', birthday=date(1940, 10, 9) + ) + + def raise_exception(): + raise AssertionError + obj, created = Person.objects.get_or_create( + first_name='John', defaults=lazy(raise_exception, object)(), + ) + self.assertFalse(created) + class UpdateOrCreateTestsWithManualPKs(TestCase): @@ -515,15 +536,17 @@ class UpdateOrCreateTransactionTests(TransactionTestCase): self.assertEqual(updated_person.last_name, 'NotLennon') -class InvalidCreateArgumentsTests(SimpleTestCase): +class InvalidCreateArgumentsTests(TransactionTestCase): + available_apps = ['get_or_create'] msg = "Invalid field name(s) for model Thing: 'nonexistent'." + bad_field_msg = "Cannot resolve keyword 'nonexistent' into field. Choices are: id, name, tags" def test_get_or_create_with_invalid_defaults(self): with self.assertRaisesMessage(FieldError, self.msg): Thing.objects.get_or_create(name='a', defaults={'nonexistent': 'b'}) def test_get_or_create_with_invalid_kwargs(self): - with self.assertRaisesMessage(FieldError, self.msg): + with self.assertRaisesMessage(FieldError, self.bad_field_msg): Thing.objects.get_or_create(name='a', nonexistent='b') def test_update_or_create_with_invalid_defaults(self): @@ -531,11 +554,11 @@ class InvalidCreateArgumentsTests(SimpleTestCase): Thing.objects.update_or_create(name='a', defaults={'nonexistent': 'b'}) def test_update_or_create_with_invalid_kwargs(self): - with self.assertRaisesMessage(FieldError, self.msg): + with self.assertRaisesMessage(FieldError, self.bad_field_msg): Thing.objects.update_or_create(name='a', nonexistent='b') def test_multiple_invalid_fields(self): - with self.assertRaisesMessage(FieldError, "Invalid field name(s) for model Thing: 'invalid', 'nonexistent'"): + with self.assertRaisesMessage(FieldError, self.bad_field_msg): Thing.objects.update_or_create(name='a', nonexistent='b', defaults={'invalid': 'c'}) def test_property_attribute_without_setter_defaults(self): @@ -543,5 +566,6 @@ class InvalidCreateArgumentsTests(SimpleTestCase): Thing.objects.update_or_create(name='a', defaults={'name_in_all_caps': 'FRANK'}) def test_property_attribute_without_setter_kwargs(self): - with self.assertRaisesMessage(FieldError, "Invalid field name(s) for model Thing: 'name_in_all_caps'"): + msg = "Cannot resolve keyword 'name_in_all_caps' into field. Choices are: id, name, tags" + with self.assertRaisesMessage(FieldError, msg): Thing.objects.update_or_create(name_in_all_caps='FRANK', defaults={'name': 'Frank'}) |