summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Holtermann <info@markusholtermann.eu>2015-01-20 11:39:23 +0100
committerMarkus Holtermann <info@markusholtermann.eu>2015-01-20 17:46:37 +0100
commit70845c6809f53d9d4029d760aa0118a47b92cb9d (patch)
tree657b9cb52c617e6d913e99920144e22e517cc9dc
parent7a1ccc069971277d74fde143966de3f3d24c9a91 (diff)
[1.7.x] Refs #24163 -- Fixed failing Oracle test when migrating from ForeignKey to OneToOneField
Thanks Tim Graham for review Backport of 64ecb3f07db4be5eef4d9eb7687f783ee446c82f from master
-rw-r--r--tests/schema/tests.py50
1 files changed, 26 insertions, 24 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 9e2d5bfd4e..f86916b5a4 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -538,7 +538,7 @@ class SchemaTests(TransactionTestCase):
@unittest.skipUnless(connection.features.supports_foreign_keys, "No FK support")
def test_alter_o2o_to_fk(self):
"""
- #24163 - Tests altering of OneToOne to FK
+ #24163 - Tests altering of OneToOneField to ForeignKey
"""
# Create the table
with connection.schema_editor() as editor:
@@ -547,19 +547,21 @@ class SchemaTests(TransactionTestCase):
# Ensure the field is right to begin with
columns = self.column_classes(BookWithO2O)
self.assertEqual(columns['author_id'][0], "IntegerField")
- # Make sure the FK and unique constraints are present
+ # Ensure the field is unique
+ author = Author.objects.create(name="Joe")
+ BookWithO2O.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
+ with self.assertRaises(IntegrityError):
+ BookWithO2O.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
+ BookWithO2O.objects.all().delete()
+ # Make sure the FK constraint is present
constraints = self.get_constraints(BookWithO2O._meta.db_table)
author_is_fk = False
- author_is_unique = False
for name, details in constraints.items():
if details['columns'] == ['author_id']:
if details['foreign_key'] and details['foreign_key'] == ('schema_author', 'id'):
author_is_fk = True
- if details['unique']:
- author_is_unique = True
self.assertTrue(author_is_fk, "No FK constraint for author_id found")
- self.assertTrue(author_is_unique, "No unique constraint for author_id found")
- # Alter the O2O to FK
+ # Alter the OneToOneField to ForeignKey
new_field = ForeignKey(Author)
new_field.set_attributes_from_name("author")
with connection.schema_editor() as editor:
@@ -572,23 +574,22 @@ class SchemaTests(TransactionTestCase):
# Ensure the field is right afterwards
columns = self.column_classes(Book)
self.assertEqual(columns['author_id'][0], "IntegerField")
- # Make sure the FK constraint is present and unique constraint is absent
+ # Ensure the field is not unique anymore
+ Book.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
+ Book.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
+ # Make sure the FK constraint is still present
constraints = self.get_constraints(Book._meta.db_table)
author_is_fk = False
- author_is_unique = True
for name, details in constraints.items():
if details['columns'] == ['author_id']:
if details['foreign_key'] and details['foreign_key'] == ('schema_author', 'id'):
author_is_fk = True
- if not details['unique']:
- author_is_unique = False
self.assertTrue(author_is_fk, "No FK constraint for author_id found")
- self.assertFalse(author_is_unique, "Unique constraint for author_id found")
@unittest.skipUnless(connection.features.supports_foreign_keys, "No FK support")
def test_alter_fk_to_o2o(self):
"""
- #24163 - Tests altering of FK to OneToOne
+ #24163 - Tests altering of ForeignKey to OneToOneField
"""
# Create the table
with connection.schema_editor() as editor:
@@ -597,19 +598,20 @@ class SchemaTests(TransactionTestCase):
# Ensure the field is right to begin with
columns = self.column_classes(Book)
self.assertEqual(columns['author_id'][0], "IntegerField")
- # Make sure the FK constraint is present and unique constraint is absent
+ # Ensure the field is not unique
+ author = Author.objects.create(name="Joe")
+ Book.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
+ Book.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
+ Book.objects.all().delete()
+ # Make sure the FK constraint is present
constraints = self.get_constraints(Book._meta.db_table)
author_is_fk = False
- author_is_unique = True
for name, details in constraints.items():
if details['columns'] == ['author_id']:
if details['foreign_key'] and details['foreign_key'] == ('schema_author', 'id'):
author_is_fk = True
- if not details['unique']:
- author_is_unique = False
self.assertTrue(author_is_fk, "No FK constraint for author_id found")
- self.assertFalse(author_is_unique, "Unique constraint for author_id found")
- # Alter the O2O to FK
+ # Alter the ForeignKey to OneToOneField
new_field = OneToOneField(Author)
new_field.set_attributes_from_name("author")
with connection.schema_editor() as editor:
@@ -622,18 +624,18 @@ class SchemaTests(TransactionTestCase):
# Ensure the field is right afterwards
columns = self.column_classes(BookWithO2O)
self.assertEqual(columns['author_id'][0], "IntegerField")
- # Make sure the FK and unique constraints are present
+ # Ensure the field is unique now
+ BookWithO2O.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
+ with self.assertRaises(IntegrityError):
+ BookWithO2O.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
+ # Make sure the FK constraint is present
constraints = self.get_constraints(BookWithO2O._meta.db_table)
author_is_fk = False
- author_is_unique = False
for name, details in constraints.items():
if details['columns'] == ['author_id']:
if details['foreign_key'] and details['foreign_key'] == ('schema_author', 'id'):
author_is_fk = True
- if details['unique']:
- author_is_unique = True
self.assertTrue(author_is_fk, "No FK constraint for author_id found")
- self.assertTrue(author_is_unique, "No unique constraint for author_id found")
def test_alter_implicit_id_to_explicit(self):
"""