diff options
| author | Alex Gaynor <alex.gaynor@rd.io> | 2013-02-20 11:27:32 -0800 |
|---|---|---|
| committer | Alex Gaynor <alex.gaynor@rd.io> | 2013-02-20 11:27:32 -0800 |
| commit | b55cde054ee7dd22f93c3522a8ddb1d04193bcac (patch) | |
| tree | d2ac4d9a64ea6c0b8fb561492d1cccce89fa4cf7 /tests | |
| parent | cb5545ea2d747e2457221a03e985ef9b4a79ee2e (diff) | |
Added a db_constraint option to ForeignKeys.
This controls whether or not a database level cosntraint is created. This is useful in a few specialized circumstances, but in general should not be used!
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/regressiontests/backends/models.py | 15 | ||||
| -rw-r--r-- | tests/regressiontests/backends/tests.py | 24 |
2 files changed, 35 insertions, 4 deletions
diff --git a/tests/regressiontests/backends/models.py b/tests/regressiontests/backends/models.py index a92aa71e17..5876cbe52d 100644 --- a/tests/regressiontests/backends/models.py +++ b/tests/regressiontests/backends/models.py @@ -39,7 +39,7 @@ if connection.features.supports_long_model_names: verbose_name = 'model_with_long_table_name' primary_key_is_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.AutoField(primary_key=True) charfield_is_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.CharField(max_length=100) - m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ManyToManyField(Person,blank=True) + m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ManyToManyField(Person, blank=True) class Tag(models.Model): @@ -86,3 +86,16 @@ class Item(models.Model): def __str__(self): return self.name + + +@python_2_unicode_compatible +class Object(models.Model): + pass + + +@python_2_unicode_compatible +class ObjectReference(models.Model): + obj = models.ForeignKey(Object, db_constraint=False) + + def __str__(self): + return str(self.obj_id) diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py index ed6f07691a..0e1700b36e 100644 --- a/tests/regressiontests/backends/tests.py +++ b/tests/regressiontests/backends/tests.py @@ -7,13 +7,12 @@ import threading from django.conf import settings from django.core.management.color import no_style -from django.core.exceptions import ImproperlyConfigured from django.db import (backend, connection, connections, DEFAULT_DB_ALIAS, IntegrityError, transaction) from django.db.backends.signals import connection_created from django.db.backends.postgresql_psycopg2 import version as pg_version -from django.db.models import fields, Sum, Avg, Variance, StdDev -from django.db.utils import ConnectionHandler, DatabaseError, load_backend +from django.db.models import Sum, Avg, Variance, StdDev +from django.db.utils import ConnectionHandler, DatabaseError from django.test import (TestCase, skipUnlessDBFeature, skipIfDBFeature, TransactionTestCase) from django.test.utils import override_settings, str_prefix @@ -724,3 +723,22 @@ class MySQLPKZeroTests(TestCase): def test_zero_as_autoval(self): with self.assertRaises(ValueError): models.Square.objects.create(id=0, root=0, square=1) + + +class DBConstraintTestCase(TransactionTestCase): + def test_can_reference_existant(self): + obj = models.Object.objects.create() + ref = models.ObjectReference.objects.create(obj=obj) + self.assertEqual(ref.obj, obj) + + ref = models.ObjectReference.objects.get(obj=obj) + self.assertEqual(ref.obj, obj) + + def test_can_reference_non_existant(self): + self.assertFalse(models.Object.objects.filter(id=12345).exists()) + ref = models.ObjectReference.objects.create(obj_id=12345) + ref_new = models.ObjectReference.objects.get(obj_id=12345) + self.assertEqual(ref, ref_new) + + with self.assertRaises(models.Object.DoesNotExist): + ref.obj |
