mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixes #18896. Add tests verifying that you can get IntegrityErrors using get_or_create through relations like M2M, and it also adds a note into the documentation warning about it
This commit is contained in:
@@ -6,7 +6,7 @@ import traceback
|
||||
from django.db import IntegrityError
|
||||
from django.test import TestCase, TransactionTestCase
|
||||
|
||||
from .models import Person, ManualPrimaryKeyTest, Profile
|
||||
from .models import Person, ManualPrimaryKeyTest, Profile, Tag, Thing
|
||||
|
||||
|
||||
class GetOrCreateTests(TestCase):
|
||||
@@ -77,3 +77,28 @@ class GetOrCreateTransactionTests(TransactionTestCase):
|
||||
pass
|
||||
else:
|
||||
self.skipTest("This backend does not support integrity checks.")
|
||||
|
||||
|
||||
class GetOrCreateThroughManyToMany(TestCase):
|
||||
|
||||
def test_get_get_or_create(self):
|
||||
tag = Tag.objects.create(text='foo')
|
||||
a_thing = Thing.objects.create(name='a')
|
||||
a_thing.tags.add(tag)
|
||||
obj, created = a_thing.tags.get_or_create(text='foo')
|
||||
|
||||
self.assertFalse(created)
|
||||
self.assertEqual(obj.pk, tag.pk)
|
||||
|
||||
def test_create_get_or_create(self):
|
||||
a_thing = Thing.objects.create(name='a')
|
||||
obj, created = a_thing.tags.get_or_create(text='foo')
|
||||
|
||||
self.assertTrue(created)
|
||||
self.assertEqual(obj.text, 'foo')
|
||||
self.assertIn(obj, a_thing.tags.all())
|
||||
|
||||
def test_something(self):
|
||||
Tag.objects.create(text='foo')
|
||||
a_thing = Thing.objects.create(name='a')
|
||||
self.assertRaises(IntegrityError, a_thing.tags.get_or_create, text='foo')
|
||||
|
||||
Reference in New Issue
Block a user