2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2015-01-10 15:48:07 +00:00
|
|
|
from unittest import skipUnless
|
|
|
|
|
2011-07-13 09:35:51 +00:00
|
|
|
from django.db import connection
|
2014-09-20 22:00:52 +00:00
|
|
|
from django.db.utils import DatabaseError
|
2015-01-10 15:48:07 +00:00
|
|
|
from django.test import TransactionTestCase, mock, skipUnlessDBFeature
|
2009-04-02 04:32:48 +00:00
|
|
|
|
2015-07-02 08:43:15 +00:00
|
|
|
from .models import Article, City, Reporter
|
2009-04-02 04:32:48 +00:00
|
|
|
|
2012-11-04 18:16:06 +00:00
|
|
|
|
2014-10-18 21:01:13 +00:00
|
|
|
class IntrospectionTests(TransactionTestCase):
|
|
|
|
|
|
|
|
available_apps = ['introspection']
|
|
|
|
|
2009-04-02 04:32:48 +00:00
|
|
|
def test_table_names(self):
|
|
|
|
tl = connection.introspection.table_names()
|
2012-04-28 23:11:55 +00:00
|
|
|
self.assertEqual(tl, sorted(tl))
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn(Reporter._meta.db_table, tl,
|
2009-04-02 04:32:48 +00:00
|
|
|
"'%s' isn't in table_list()." % Reporter._meta.db_table)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn(Article._meta.db_table, tl,
|
2009-04-02 04:32:48 +00:00
|
|
|
"'%s' isn't in table_list()." % Article._meta.db_table)
|
2009-04-04 19:03:55 +00:00
|
|
|
|
2009-04-02 04:32:48 +00:00
|
|
|
def test_django_table_names(self):
|
2014-01-09 15:05:15 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute('CREATE TABLE django_ixn_test_table (id INTEGER);')
|
|
|
|
tl = connection.introspection.django_table_names()
|
|
|
|
cursor.execute("DROP TABLE django_ixn_test_table;")
|
2014-07-10 02:24:19 +00:00
|
|
|
self.assertNotIn('django_ixn_test_table', tl,
|
|
|
|
"django_table_names() returned a non-Django table")
|
2009-04-04 19:03:55 +00:00
|
|
|
|
2012-02-11 22:12:49 +00:00
|
|
|
def test_django_table_names_retval_type(self):
|
2015-02-05 18:25:34 +00:00
|
|
|
# Table name is a list #15216
|
2012-02-11 22:12:49 +00:00
|
|
|
tl = connection.introspection.django_table_names(only_existing=True)
|
|
|
|
self.assertIs(type(tl), list)
|
|
|
|
tl = connection.introspection.django_table_names(only_existing=False)
|
|
|
|
self.assertIs(type(tl), list)
|
|
|
|
|
2014-09-20 22:00:52 +00:00
|
|
|
def test_table_names_with_views(self):
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
try:
|
|
|
|
cursor.execute(
|
|
|
|
'CREATE VIEW introspection_article_view AS SELECT headline '
|
|
|
|
'from introspection_article;')
|
|
|
|
except DatabaseError as e:
|
|
|
|
if 'insufficient privileges' in str(e):
|
2014-09-21 17:15:48 +00:00
|
|
|
self.fail("The test user has no CREATE VIEW privileges")
|
2014-09-20 22:00:52 +00:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
self.assertIn('introspection_article_view',
|
|
|
|
connection.introspection.table_names(include_views=True))
|
|
|
|
self.assertNotIn('introspection_article_view',
|
|
|
|
connection.introspection.table_names())
|
|
|
|
|
2009-04-02 04:32:48 +00:00
|
|
|
def test_installed_models(self):
|
|
|
|
tables = [Article._meta.db_table, Reporter._meta.db_table]
|
|
|
|
models = connection.introspection.installed_models(tables)
|
2014-09-26 12:31:50 +00:00
|
|
|
self.assertEqual(models, {Article, Reporter})
|
2009-04-04 19:03:55 +00:00
|
|
|
|
2009-04-02 04:32:48 +00:00
|
|
|
def test_sequence_list(self):
|
|
|
|
sequences = connection.introspection.sequence_list()
|
|
|
|
expected = {'table': Reporter._meta.db_table, 'column': 'id'}
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn(expected, sequences,
|
2009-04-02 04:32:48 +00:00
|
|
|
'Reporter sequence not found in sequence_list()')
|
2009-04-04 19:03:55 +00:00
|
|
|
|
2009-04-02 04:32:48 +00:00
|
|
|
def test_get_table_description_names(self):
|
2014-01-09 15:05:15 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
|
2009-04-02 04:32:48 +00:00
|
|
|
self.assertEqual([r[0] for r in desc],
|
|
|
|
[f.column for f in Reporter._meta.fields])
|
2009-04-04 19:03:55 +00:00
|
|
|
|
2009-04-02 04:32:48 +00:00
|
|
|
def test_get_table_description_types(self):
|
2014-01-09 15:05:15 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
|
2010-04-21 23:43:35 +00:00
|
|
|
self.assertEqual(
|
|
|
|
[datatype(r[1], r) for r in desc],
|
2013-09-12 14:03:29 +00:00
|
|
|
['AutoField' if connection.features.can_introspect_autofield else 'IntegerField',
|
2014-05-10 12:40:42 +00:00
|
|
|
'CharField', 'CharField', 'CharField',
|
|
|
|
'BigIntegerField' if connection.features.can_introspect_big_integer_field else 'IntegerField',
|
2014-08-26 05:22:55 +00:00
|
|
|
'BinaryField' if connection.features.can_introspect_binary_field else 'TextField',
|
|
|
|
'SmallIntegerField' if connection.features.can_introspect_small_integer_field else 'IntegerField']
|
2010-04-21 23:43:35 +00:00
|
|
|
)
|
2012-10-27 15:10:02 +00:00
|
|
|
|
|
|
|
# The following test fails on Oracle due to #17202 (can't correctly
|
|
|
|
# inspect the length of character columns).
|
2014-05-07 20:14:39 +00:00
|
|
|
@skipUnlessDBFeature('can_introspect_max_length')
|
2012-10-27 15:10:02 +00:00
|
|
|
def test_get_table_description_col_lengths(self):
|
2014-01-09 15:05:15 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
|
2012-08-30 17:28:13 +00:00
|
|
|
self.assertEqual(
|
|
|
|
[r[3] for r in desc if datatype(r[1], r) == 'CharField'],
|
2014-06-30 18:34:45 +00:00
|
|
|
[30, 30, 254]
|
2012-08-30 17:28:13 +00:00
|
|
|
)
|
2009-04-02 04:32:48 +00:00
|
|
|
|
2014-06-05 15:56:56 +00:00
|
|
|
@skipUnlessDBFeature('can_introspect_null')
|
2012-02-11 20:05:50 +00:00
|
|
|
def test_get_table_description_nullable(self):
|
2014-01-09 15:05:15 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
|
2014-06-13 21:43:49 +00:00
|
|
|
nullable_by_backend = connection.features.interprets_empty_strings_as_nulls
|
2012-02-11 20:05:50 +00:00
|
|
|
self.assertEqual(
|
|
|
|
[r[6] for r in desc],
|
2014-08-26 05:22:55 +00:00
|
|
|
[False, nullable_by_backend, nullable_by_backend, nullable_by_backend, True, True, False]
|
2012-02-11 20:05:50 +00:00
|
|
|
)
|
|
|
|
|
2015-07-02 08:43:15 +00:00
|
|
|
@skipUnlessDBFeature('can_introspect_autofield')
|
|
|
|
def test_bigautofield(self):
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
desc = connection.introspection.get_table_description(cursor, City._meta.db_table)
|
|
|
|
self.assertIn('BigAutoField', [datatype(r[1], r) for r in desc])
|
|
|
|
|
2009-04-03 20:52:54 +00:00
|
|
|
# Regression test for #9991 - 'real' types in postgres
|
2010-10-11 12:55:17 +00:00
|
|
|
@skipUnlessDBFeature('has_real_datatype')
|
|
|
|
def test_postgresql_real_type(self):
|
2014-01-09 15:05:15 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute("CREATE TABLE django_ixn_real_test_table (number REAL);")
|
|
|
|
desc = connection.introspection.get_table_description(cursor, 'django_ixn_real_test_table')
|
|
|
|
cursor.execute('DROP TABLE django_ixn_real_test_table;')
|
2010-10-11 12:55:17 +00:00
|
|
|
self.assertEqual(datatype(desc[0][1], desc[0]), 'FloatField')
|
2009-04-03 20:52:54 +00:00
|
|
|
|
2015-01-10 14:42:31 +00:00
|
|
|
@skipUnlessDBFeature('can_introspect_foreign_keys')
|
2009-04-02 04:32:48 +00:00
|
|
|
def test_get_relations(self):
|
2014-01-09 15:05:15 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
relations = connection.introspection.get_relations(cursor, Article._meta.db_table)
|
2009-04-04 19:03:55 +00:00
|
|
|
|
2015-01-10 19:27:30 +00:00
|
|
|
# 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)
|
2009-04-04 19:03:55 +00:00
|
|
|
|
2015-01-10 15:48:07 +00:00
|
|
|
@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:
|
2015-09-11 23:33:12 +00:00
|
|
|
cursor.fetchone = mock.Mock(
|
|
|
|
return_value=[
|
|
|
|
"CREATE TABLE track(id, art_id INTEGER, FOREIGN KEY(art_id) REFERENCES {}(id));".format(
|
|
|
|
Article._meta.db_table
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2015-01-10 15:48:07 +00:00
|
|
|
relations = connection.introspection.get_relations(cursor, 'mocked_table')
|
2015-01-10 19:27:30 +00:00
|
|
|
self.assertEqual(relations, {'art_id': ('id', Article._meta.db_table)})
|
2015-01-10 15:48:07 +00:00
|
|
|
|
2013-01-12 20:46:08 +00:00
|
|
|
@skipUnlessDBFeature('can_introspect_foreign_keys')
|
2011-08-07 00:43:26 +00:00
|
|
|
def test_get_key_columns(self):
|
2014-01-09 15:05:15 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
key_columns = connection.introspection.get_key_columns(cursor, Article._meta.db_table)
|
2013-01-28 09:17:56 +00:00
|
|
|
self.assertEqual(
|
|
|
|
set(key_columns),
|
2014-09-26 12:31:50 +00:00
|
|
|
{('reporter_id', Reporter._meta.db_table, 'id'),
|
2014-09-29 12:13:40 +00:00
|
|
|
('response_to_id', Article._meta.db_table, 'id')})
|
2011-08-07 00:43:26 +00:00
|
|
|
|
|
|
|
def test_get_primary_key_column(self):
|
2014-01-09 15:05:15 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
primary_key_column = connection.introspection.get_primary_key_column(cursor, Article._meta.db_table)
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(primary_key_column, 'id')
|
2011-08-07 00:43:26 +00:00
|
|
|
|
2009-04-02 04:32:48 +00:00
|
|
|
def test_get_indexes(self):
|
2014-01-09 15:05:15 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
indexes = connection.introspection.get_indexes(cursor, Article._meta.db_table)
|
2009-04-03 20:52:54 +00:00
|
|
|
self.assertEqual(indexes['reporter_id'], {'unique': False, 'primary_key': False})
|
2009-04-04 19:03:55 +00:00
|
|
|
|
2012-04-30 11:05:30 +00:00
|
|
|
def test_get_indexes_multicol(self):
|
|
|
|
"""
|
|
|
|
Test that multicolumn indexes are not included in the introspection
|
|
|
|
results.
|
|
|
|
"""
|
2014-01-09 15:05:15 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
indexes = connection.introspection.get_indexes(cursor, Reporter._meta.db_table)
|
2012-04-30 11:05:30 +00:00
|
|
|
self.assertNotIn('first_name', indexes)
|
|
|
|
self.assertIn('id', indexes)
|
2009-08-21 21:42:39 +00:00
|
|
|
|
2012-11-04 18:16:06 +00:00
|
|
|
|
2009-08-21 21:42:39 +00:00
|
|
|
def datatype(dbtype, description):
|
2009-04-03 20:52:54 +00:00
|
|
|
"""Helper to convert a data type into a string."""
|
2009-08-21 21:42:39 +00:00
|
|
|
dt = connection.introspection.get_field_type(dbtype, description)
|
2009-04-03 20:52:54 +00:00
|
|
|
if type(dt) is tuple:
|
|
|
|
return dt[0]
|
|
|
|
else:
|
|
|
|
return dt
|