2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2011-07-13 17:35:51 +08:00
|
|
|
from django.db import connection
|
2014-06-05 23:56:56 +08:00
|
|
|
from django.test import TestCase, skipUnlessDBFeature
|
2009-04-02 12:32:48 +08:00
|
|
|
|
2011-10-14 05:34:56 +08:00
|
|
|
from .models import Reporter, Article
|
2009-04-02 12:32:48 +08:00
|
|
|
|
2012-11-05 02:16:06 +08:00
|
|
|
|
2013-01-13 04:46:08 +08:00
|
|
|
class IntrospectionTests(TestCase):
|
2009-04-02 12:32:48 +08:00
|
|
|
def test_table_names(self):
|
|
|
|
tl = connection.introspection.table_names()
|
2012-04-29 07:11:55 +08:00
|
|
|
self.assertEqual(tl, sorted(tl))
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertTrue(Reporter._meta.db_table in tl,
|
2009-04-02 12:32:48 +08:00
|
|
|
"'%s' isn't in table_list()." % Reporter._meta.db_table)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertTrue(Article._meta.db_table in tl,
|
2009-04-02 12:32:48 +08:00
|
|
|
"'%s' isn't in table_list()." % Article._meta.db_table)
|
2009-04-05 03:03:55 +08:00
|
|
|
|
2009-04-02 12:32:48 +08:00
|
|
|
def test_django_table_names(self):
|
2014-01-09 23:05:15 +08: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;")
|
|
|
|
self.assertTrue('django_ixn_testcase_table' not in tl,
|
|
|
|
"django_table_names() returned a non-Django table")
|
2009-04-05 03:03:55 +08:00
|
|
|
|
2012-02-12 06:12:49 +08:00
|
|
|
def test_django_table_names_retval_type(self):
|
|
|
|
# Ticket #15216
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute('CREATE TABLE django_ixn_test_table (id INTEGER);')
|
2012-02-12 06:12:49 +08: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)
|
|
|
|
|
2009-04-02 12:32:48 +08:00
|
|
|
def test_installed_models(self):
|
|
|
|
tables = [Article._meta.db_table, Reporter._meta.db_table]
|
|
|
|
models = connection.introspection.installed_models(tables)
|
|
|
|
self.assertEqual(models, set([Article, Reporter]))
|
2009-04-05 03:03:55 +08:00
|
|
|
|
2009-04-02 12:32:48 +08:00
|
|
|
def test_sequence_list(self):
|
|
|
|
sequences = connection.introspection.sequence_list()
|
|
|
|
expected = {'table': Reporter._meta.db_table, 'column': 'id'}
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertTrue(expected in sequences,
|
2009-04-02 12:32:48 +08:00
|
|
|
'Reporter sequence not found in sequence_list()')
|
2009-04-05 03:03:55 +08:00
|
|
|
|
2009-04-02 12:32:48 +08:00
|
|
|
def test_get_table_description_names(self):
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
|
2009-04-02 12:32:48 +08:00
|
|
|
self.assertEqual([r[0] for r in desc],
|
|
|
|
[f.column for f in Reporter._meta.fields])
|
2009-04-05 03:03:55 +08:00
|
|
|
|
2009-04-02 12:32:48 +08:00
|
|
|
def test_get_table_description_types(self):
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
|
2010-04-22 07:43:35 +08:00
|
|
|
self.assertEqual(
|
|
|
|
[datatype(r[1], r) for r in desc],
|
2013-09-12 22:03:29 +08:00
|
|
|
['AutoField' if connection.features.can_introspect_autofield else 'IntegerField',
|
2014-05-10 20:40:42 +08:00
|
|
|
'CharField', 'CharField', 'CharField',
|
|
|
|
'BigIntegerField' if connection.features.can_introspect_big_integer_field else 'IntegerField',
|
2014-05-08 04:14:39 +08:00
|
|
|
'BinaryField' if connection.features.can_introspect_binary_field else 'TextField']
|
2010-04-22 07:43:35 +08:00
|
|
|
)
|
2012-10-27 23:10:02 +08:00
|
|
|
|
|
|
|
# The following test fails on Oracle due to #17202 (can't correctly
|
|
|
|
# inspect the length of character columns).
|
2014-05-08 04:14:39 +08:00
|
|
|
@skipUnlessDBFeature('can_introspect_max_length')
|
2012-10-27 23:10:02 +08:00
|
|
|
def test_get_table_description_col_lengths(self):
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
|
2012-08-31 01:28:13 +08:00
|
|
|
self.assertEqual(
|
|
|
|
[r[3] for r in desc if datatype(r[1], r) == 'CharField'],
|
|
|
|
[30, 30, 75]
|
|
|
|
)
|
2009-04-02 12:32:48 +08:00
|
|
|
|
2014-06-05 23:56:56 +08:00
|
|
|
# The following test fails on Oracle. Since it forces null=True under the
|
|
|
|
# hood in some cases, its idea about null_ok is different from ours.
|
|
|
|
@skipUnlessDBFeature('can_introspect_null')
|
2012-02-12 04:05:50 +08:00
|
|
|
def test_get_table_description_nullable(self):
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
|
2012-02-12 04:05:50 +08:00
|
|
|
self.assertEqual(
|
|
|
|
[r[6] for r in desc],
|
2012-12-18 05:35:35 +08:00
|
|
|
[False, False, False, False, True, True]
|
2012-02-12 04:05:50 +08:00
|
|
|
)
|
|
|
|
|
2009-04-04 04:52:54 +08:00
|
|
|
# Regression test for #9991 - 'real' types in postgres
|
2010-10-11 20:55:17 +08:00
|
|
|
@skipUnlessDBFeature('has_real_datatype')
|
|
|
|
def test_postgresql_real_type(self):
|
2014-01-09 23:05:15 +08: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 20:55:17 +08:00
|
|
|
self.assertEqual(datatype(desc[0][1], desc[0]), 'FloatField')
|
2009-04-04 04:52:54 +08:00
|
|
|
|
2009-04-02 12:32:48 +08:00
|
|
|
def test_get_relations(self):
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
relations = connection.introspection.get_relations(cursor, Article._meta.db_table)
|
2009-04-05 03:03:55 +08:00
|
|
|
|
2009-04-02 12:32:48 +08:00
|
|
|
# Older versions of MySQL don't have the chops to report on this stuff,
|
|
|
|
# so just skip it if no relations come back. If they do, though, we
|
|
|
|
# should test that the response is correct.
|
|
|
|
if relations:
|
|
|
|
# That's {field_index: (field_index_other_table, other_table)}
|
2013-01-28 17:17:56 +08:00
|
|
|
self.assertEqual(relations, {3: (0, Reporter._meta.db_table),
|
|
|
|
4: (0, Article._meta.db_table)})
|
2009-04-05 03:03:55 +08:00
|
|
|
|
2013-01-13 04:46:08 +08:00
|
|
|
@skipUnlessDBFeature('can_introspect_foreign_keys')
|
2011-08-07 08:43:26 +08:00
|
|
|
def test_get_key_columns(self):
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
key_columns = connection.introspection.get_key_columns(cursor, Article._meta.db_table)
|
2013-01-28 17:17:56 +08:00
|
|
|
self.assertEqual(
|
|
|
|
set(key_columns),
|
|
|
|
set([('reporter_id', Reporter._meta.db_table, 'id'),
|
|
|
|
('response_to_id', Article._meta.db_table, 'id')]))
|
2011-08-07 08:43:26 +08:00
|
|
|
|
|
|
|
def test_get_primary_key_column(self):
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
primary_key_column = connection.introspection.get_primary_key_column(cursor, Article._meta.db_table)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(primary_key_column, 'id')
|
2011-08-07 08:43:26 +08:00
|
|
|
|
2009-04-02 12:32:48 +08:00
|
|
|
def test_get_indexes(self):
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
indexes = connection.introspection.get_indexes(cursor, Article._meta.db_table)
|
2009-04-04 04:52:54 +08:00
|
|
|
self.assertEqual(indexes['reporter_id'], {'unique': False, 'primary_key': False})
|
2009-04-05 03:03:55 +08:00
|
|
|
|
2012-04-30 19:05:30 +08:00
|
|
|
def test_get_indexes_multicol(self):
|
|
|
|
"""
|
|
|
|
Test that multicolumn indexes are not included in the introspection
|
|
|
|
results.
|
|
|
|
"""
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
indexes = connection.introspection.get_indexes(cursor, Reporter._meta.db_table)
|
2012-04-30 19:05:30 +08:00
|
|
|
self.assertNotIn('first_name', indexes)
|
|
|
|
self.assertIn('id', indexes)
|
2009-08-22 05:42:39 +08:00
|
|
|
|
2012-11-05 02:16:06 +08:00
|
|
|
|
2009-08-22 05:42:39 +08:00
|
|
|
def datatype(dbtype, description):
|
2009-04-04 04:52:54 +08:00
|
|
|
"""Helper to convert a data type into a string."""
|
2009-08-22 05:42:39 +08:00
|
|
|
dt = connection.introspection.get_field_type(dbtype, description)
|
2009-04-04 04:52:54 +08:00
|
|
|
if type(dt) is tuple:
|
|
|
|
return dt[0]
|
|
|
|
else:
|
|
|
|
return dt
|