Refs #22728 - Added missing tests for defaults__exact case
This commit is contained in:
parent
3e9b5bfd9c
commit
fc19f9315b
|
@ -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)
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue