summaryrefslogtreecommitdiff
path: root/tests/get_or_create
diff options
context:
space:
mode:
authorPablo Recio <pablo@potatolondon.com>2013-05-19 14:15:36 +0200
committerPablo Recio <pablo@potatolondon.com>2013-05-19 14:16:12 +0200
commit65f9e0affd8ca04e2c597c43c1547ef7c888ec2a (patch)
treea5d498bf2a020c84902698b8175f4eb03ee6ead8 /tests/get_or_create
parentd34b1c29e294b19e51a47918125314a1540c01d4 (diff)
downloaddjango-65f9e0affd8ca04e2c597c43c1547ef7c888ec2a.tar.gz
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
Diffstat (limited to 'tests/get_or_create')
-rw-r--r--tests/get_or_create/models.py9
-rw-r--r--tests/get_or_create/tests.py27
2 files changed, 35 insertions, 1 deletions
diff --git a/tests/get_or_create/models.py b/tests/get_or_create/models.py
index 82905de4f8..2f21344f59 100644
--- a/tests/get_or_create/models.py
+++ b/tests/get_or_create/models.py
@@ -28,3 +28,12 @@ class ManualPrimaryKeyTest(models.Model):
class Profile(models.Model):
person = models.ForeignKey(Person, primary_key=True)
+
+
+class Tag(models.Model):
+ text = models.CharField(max_length=256, unique=True)
+
+
+class Thing(models.Model):
+ name = models.CharField(max_length=256)
+ tags = models.ManyToManyField(Tag)
diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py
index e9cce9bbde..5117a2f915 100644
--- a/tests/get_or_create/tests.py
+++ b/tests/get_or_create/tests.py
@@ -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')