1
from django.conf import settings
2
from django.db import connection
3
from django.test import TestCase
4
from django.utils import functional
6
from models import Reporter, Article
11
from sets import Set as set # Python 2.3 fallback
14
# The introspection module is optional, so methods tested here might raise
15
# NotImplementedError. This is perfectly acceptable behavior for the backend
16
# in question, but the tests need to handle this without failing. Ideally we'd
17
# skip these tests, but until #4788 is done we'll just ignore them.
19
# The easiest way to accomplish this is to decorate every test case with a
20
# wrapper that ignores the exception.
22
# The metaclass is just for fun.
25
def ignore_not_implemented(func):
26
def _inner(*args, **kwargs):
28
return func(*args, **kwargs)
29
except NotImplementedError:
31
functional.update_wrapper(_inner, func)
34
class IgnoreNotimplementedError(type):
35
def __new__(cls, name, bases, attrs):
36
for k,v in attrs.items():
37
if k.startswith('test'):
38
attrs[k] = ignore_not_implemented(v)
39
return type.__new__(cls, name, bases, attrs)
41
class IntrospectionTests(TestCase):
42
__metaclass__ = IgnoreNotimplementedError
44
def test_table_names(self):
45
tl = connection.introspection.table_names()
46
self.assert_(Reporter._meta.db_table in tl,
47
"'%s' isn't in table_list()." % Reporter._meta.db_table)
48
self.assert_(Article._meta.db_table in tl,
49
"'%s' isn't in table_list()." % Article._meta.db_table)
51
def test_django_table_names(self):
52
cursor = connection.cursor()
53
cursor.execute('CREATE TABLE django_ixn_test_table (id INTEGER);');
54
tl = connection.introspection.django_table_names()
55
cursor.execute("DROP TABLE django_ixn_test_table;")
56
self.assert_('django_ixn_testcase_table' not in tl,
57
"django_table_names() returned a non-Django table")
59
def test_installed_models(self):
60
tables = [Article._meta.db_table, Reporter._meta.db_table]
61
models = connection.introspection.installed_models(tables)
62
self.assertEqual(models, set([Article, Reporter]))
64
def test_sequence_list(self):
65
sequences = connection.introspection.sequence_list()
66
expected = {'table': Reporter._meta.db_table, 'column': 'id'}
67
self.assert_(expected in sequences,
68
'Reporter sequence not found in sequence_list()')
70
def test_get_table_description_names(self):
71
cursor = connection.cursor()
72
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
73
self.assertEqual([r[0] for r in desc],
74
[f.column for f in Reporter._meta.fields])
76
def test_get_table_description_types(self):
77
cursor = connection.cursor()
78
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
79
self.assertEqual([datatype(r[1]) for r in desc],
80
['IntegerField', 'CharField', 'CharField', 'CharField'])
82
# Regression test for #9991 - 'real' types in postgres
83
if settings.DATABASE_ENGINE.startswith('postgresql'):
84
def test_postgresql_real_type(self):
85
cursor = connection.cursor()
86
cursor.execute("CREATE TABLE django_ixn_real_test_table (number REAL);")
87
desc = connection.introspection.get_table_description(cursor, 'django_ixn_real_test_table')
88
cursor.execute('DROP TABLE django_ixn_real_test_table;')
89
self.assertEqual(datatype(desc[0][1]), 'FloatField')
91
def test_get_relations(self):
92
cursor = connection.cursor()
93
relations = connection.introspection.get_relations(cursor, Article._meta.db_table)
95
# Older versions of MySQL don't have the chops to report on this stuff,
96
# so just skip it if no relations come back. If they do, though, we
97
# should test that the response is correct.
99
# That's {field_index: (field_index_other_table, other_table)}
100
self.assertEqual(relations, {3: (0, Reporter._meta.db_table)})
102
def test_get_indexes(self):
103
cursor = connection.cursor()
104
indexes = connection.introspection.get_indexes(cursor, Article._meta.db_table)
105
self.assertEqual(indexes['reporter_id'], {'unique': False, 'primary_key': False})
107
def datatype(dbtype):
108
"""Helper to convert a data type into a string."""
109
dt = connection.introspection.data_types_reverse[dbtype]
110
if type(dt) is tuple: