2009-04-03 20:52:54 +00:00
|
|
|
from django.conf import settings
|
2009-12-22 15:18:51 +00:00
|
|
|
from django.db import connection, DEFAULT_DB_ALIAS
|
2010-10-11 12:55:17 +00:00
|
|
|
from django.test import TestCase, skipUnlessDBFeature
|
2009-04-02 04:32:48 +00:00
|
|
|
from django.utils import functional
|
|
|
|
|
|
|
|
from models import Reporter, Article
|
|
|
|
|
|
|
|
#
|
|
|
|
# The introspection module is optional, so methods tested here might raise
|
|
|
|
# NotImplementedError. This is perfectly acceptable behavior for the backend
|
|
|
|
# in question, but the tests need to handle this without failing. Ideally we'd
|
|
|
|
# skip these tests, but until #4788 is done we'll just ignore them.
|
|
|
|
#
|
2009-04-04 19:03:55 +00:00
|
|
|
# The easiest way to accomplish this is to decorate every test case with a
|
2009-04-02 04:32:48 +00:00
|
|
|
# wrapper that ignores the exception.
|
|
|
|
#
|
|
|
|
# The metaclass is just for fun.
|
|
|
|
#
|
|
|
|
|
|
|
|
def ignore_not_implemented(func):
|
|
|
|
def _inner(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
except NotImplementedError:
|
|
|
|
return None
|
|
|
|
functional.update_wrapper(_inner, func)
|
|
|
|
return _inner
|
|
|
|
|
|
|
|
class IgnoreNotimplementedError(type):
|
|
|
|
def __new__(cls, name, bases, attrs):
|
|
|
|
for k,v in attrs.items():
|
|
|
|
if k.startswith('test'):
|
|
|
|
attrs[k] = ignore_not_implemented(v)
|
|
|
|
return type.__new__(cls, name, bases, attrs)
|
|
|
|
|
|
|
|
class IntrospectionTests(TestCase):
|
|
|
|
__metaclass__ = IgnoreNotimplementedError
|
2009-04-04 19:03:55 +00:00
|
|
|
|
2009-04-02 04:32:48 +00:00
|
|
|
def test_table_names(self):
|
|
|
|
tl = connection.introspection.table_names()
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertTrue(Reporter._meta.db_table in tl,
|
2009-04-02 04:32:48 +00:00
|
|
|
"'%s' isn't in table_list()." % Reporter._meta.db_table)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertTrue(Article._meta.db_table in 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):
|
|
|
|
cursor = connection.cursor()
|
2009-04-16 22:32:48 +00:00
|
|
|
cursor.execute('CREATE TABLE django_ixn_test_table (id INTEGER);');
|
2009-04-02 04:32:48 +00:00
|
|
|
tl = connection.introspection.django_table_names()
|
2009-04-16 22:32:48 +00:00
|
|
|
cursor.execute("DROP TABLE django_ixn_test_table;")
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertTrue('django_ixn_testcase_table' not in tl,
|
2009-04-02 04:32:48 +00:00
|
|
|
"django_table_names() returned a non-Django table")
|
2009-04-04 19:03:55 +00:00
|
|
|
|
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)
|
|
|
|
self.assertEqual(models, set([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'}
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertTrue(expected in 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):
|
|
|
|
cursor = connection.cursor()
|
|
|
|
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
|
|
|
|
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):
|
2009-04-04 19:03:55 +00:00
|
|
|
cursor = connection.cursor()
|
2009-04-02 04:32:48 +00:00
|
|
|
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],
|
|
|
|
['IntegerField', 'CharField', 'CharField', 'CharField', 'BigIntegerField']
|
|
|
|
)
|
2009-04-02 04:32:48 +00:00
|
|
|
|
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):
|
|
|
|
cursor = connection.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;')
|
|
|
|
self.assertEqual(datatype(desc[0][1], desc[0]), 'FloatField')
|
2009-04-03 20:52:54 +00:00
|
|
|
|
2009-04-02 04:32:48 +00:00
|
|
|
def test_get_relations(self):
|
|
|
|
cursor = connection.cursor()
|
|
|
|
relations = connection.introspection.get_relations(cursor, Article._meta.db_table)
|
2009-04-04 19:03:55 +00:00
|
|
|
|
2009-04-02 04:32:48 +00: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)}
|
|
|
|
self.assertEqual(relations, {3: (0, Reporter._meta.db_table)})
|
2009-04-04 19:03:55 +00:00
|
|
|
|
2009-04-02 04:32:48 +00:00
|
|
|
def test_get_indexes(self):
|
|
|
|
cursor = connection.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
|
|
|
|
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
|