summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2015-01-10 20:27:30 +0100
committerClaude Paroz <claude@2xlibre.net>2015-01-12 19:58:47 +0100
commit4c413e231cfe788de6e371567f395c8ccbd26103 (patch)
tree8b2100a8d9d650546615c8c1cd3b8f52251690a3 /tests
parentb75c707943e159b80c179c538721406bbfb8b120 (diff)
Fixed #17785 -- Preferred column names in get_relations introspection
Thanks Thomas Güttler for the report and the initial patch, and Tim Graham for the review.
Diffstat (limited to 'tests')
-rw-r--r--tests/introspection/models.py1
-rw-r--r--tests/introspection/tests.py23
2 files changed, 19 insertions, 5 deletions
diff --git a/tests/introspection/models.py b/tests/introspection/models.py
index 12264cfbd5..3929509934 100644
--- a/tests/introspection/models.py
+++ b/tests/introspection/models.py
@@ -24,6 +24,7 @@ class Reporter(models.Model):
class Article(models.Model):
headline = models.CharField(max_length=100)
pub_date = models.DateField()
+ body = models.TextField(default='')
reporter = models.ForeignKey(Reporter)
response_to = models.ForeignKey('self', null=True)
diff --git a/tests/introspection/tests.py b/tests/introspection/tests.py
index 6961bb7c31..cc6b33e572 100644
--- a/tests/introspection/tests.py
+++ b/tests/introspection/tests.py
@@ -117,19 +117,32 @@ class IntrospectionTests(TransactionTestCase):
with connection.cursor() as cursor:
relations = connection.introspection.get_relations(cursor, Article._meta.db_table)
- # That's {field_index: (field_index_other_table, other_table)}
- self.assertEqual(relations, {3: (0, Reporter._meta.db_table),
- 4: (0, Article._meta.db_table)})
+ # That's {field_name: (field_name_other_table, other_table)}
+ expected_relations = {
+ 'reporter_id': ('id', Reporter._meta.db_table),
+ 'response_to_id': ('id', Article._meta.db_table),
+ }
+ self.assertEqual(relations, expected_relations)
+
+ # Removing a field shouldn't disturb get_relations (#17785)
+ body = Article._meta.get_field('body')
+ with connection.schema_editor() as editor:
+ editor.remove_field(Article, body)
+ with connection.cursor() as cursor:
+ relations = connection.introspection.get_relations(cursor, Article._meta.db_table)
+ with connection.schema_editor() as editor:
+ editor.add_field(Article, body)
+ self.assertEqual(relations, expected_relations)
@skipUnless(connection.vendor == 'sqlite', "This is an sqlite-specific issue")
def test_get_relations_alt_format(self):
"""With SQLite, foreign keys can be added with different syntaxes."""
with connection.cursor() as cursor:
cursor.fetchone = mock.Mock(return_value=[
- "CREATE TABLE track(id, art INTEGER, FOREIGN KEY(art) REFERENCES %s(id));" % Article._meta.db_table
+ "CREATE TABLE track(id, art_id INTEGER, FOREIGN KEY(art_id) REFERENCES %s(id));" % Article._meta.db_table
])
relations = connection.introspection.get_relations(cursor, 'mocked_table')
- self.assertEqual(relations, {1: (0, Article._meta.db_table)})
+ self.assertEqual(relations, {'art_id': ('id', Article._meta.db_table)})
@skipUnlessDBFeature('can_introspect_foreign_keys')
def test_get_key_columns(self):