From fc19f9315be312b5e5f00e0004b8696e36f852e7 Mon Sep 17 00:00:00 2001 From: Andriy Sokolovskiy Date: Fri, 5 Jun 2015 00:27:10 +0100 Subject: [PATCH] Refs #22728 - Added missing tests for defaults__exact case --- tests/get_or_create/models.py | 1 + tests/get_or_create/tests.py | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/tests/get_or_create/models.py b/tests/get_or_create/models.py index a8e9a3f3f9f..44e99675011 100644 --- a/tests/get_or_create/models.py +++ b/tests/get_or_create/models.py @@ -9,6 +9,7 @@ class Person(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) birthday = models.DateField() + defaults = models.TextField() def __str__(self): return '%s %s' % (self.first_name, self.last_name) diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py index a58e77ec0e6..762cf570251 100644 --- a/tests/get_or_create/tests.py +++ b/tests/get_or_create/tests.py @@ -126,6 +126,28 @@ class GetOrCreateTests(TestCase): # The publisher should have three books. self.assertEqual(p.books.count(), 3) + def test_defaults_exact(self): + """ + If you have a field named defaults and want to use it as an exact + lookup, you need to use 'defaults__exact'. + """ + obj, created = Person.objects.get_or_create( + first_name='George', last_name='Harrison', defaults__exact='testing', defaults={ + 'birthday': date(1943, 2, 25), + 'defaults': 'testing', + } + ) + self.assertTrue(created) + self.assertEqual(obj.defaults, 'testing') + obj2, created = Person.objects.get_or_create( + first_name='George', last_name='Harrison', defaults__exact='testing', defaults={ + 'birthday': date(1943, 2, 25), + 'defaults': 'testing', + } + ) + self.assertFalse(created) + self.assertEqual(obj, obj2) + class GetOrCreateTestsWithManualPKs(TestCase): @@ -348,3 +370,25 @@ class UpdateOrCreateTests(TestCase): self.assertFalse(created) self.assertEqual(book.name, name) self.assertEqual(author.books.count(), 1) + + def test_defaults_exact(self): + """ + If you have a field named defaults and want to use it as an exact + lookup, you need to use 'defaults__exact'. + """ + obj, created = Person.objects.update_or_create( + first_name='George', last_name='Harrison', defaults__exact='testing', defaults={ + 'birthday': date(1943, 2, 25), + 'defaults': 'testing', + } + ) + self.assertTrue(created) + self.assertEqual(obj.defaults, 'testing') + obj, created = Person.objects.update_or_create( + first_name='George', last_name='Harrison', defaults__exact='testing', defaults={ + 'birthday': date(1943, 2, 25), + 'defaults': 'another testing', + } + ) + self.assertFalse(created) + self.assertEqual(obj.defaults, 'another testing')