App Engine Python SDK version 1.8.0
[gae.git] / python / lib / django-1.5 / tests / regressiontests / introspection / tests.py
blob2df946d874c3d2d20a73c5c8dd3be61911a10ec1
1 from __future__ import absolute_import, unicode_literals
3 from functools import update_wrapper
5 from django.db import connection
6 from django.test import TestCase, skipUnlessDBFeature, skipIfDBFeature
7 from django.utils import six, unittest
9 from .models import Reporter, Article
11 if connection.vendor == 'oracle':
12 expectedFailureOnOracle = unittest.expectedFailure
13 else:
14 expectedFailureOnOracle = lambda f: f
17 # The introspection module is optional, so methods tested here might raise
18 # NotImplementedError. This is perfectly acceptable behavior for the backend
19 # in question, but the tests need to handle this without failing. Ideally we'd
20 # skip these tests, but until #4788 is done we'll just ignore them.
22 # The easiest way to accomplish this is to decorate every test case with a
23 # wrapper that ignores the exception.
25 # The metaclass is just for fun.
28 def ignore_not_implemented(func):
29 def _inner(*args, **kwargs):
30 try:
31 return func(*args, **kwargs)
32 except NotImplementedError:
33 return None
34 update_wrapper(_inner, func)
35 return _inner
38 class IgnoreNotimplementedError(type):
39 def __new__(cls, name, bases, attrs):
40 for k, v in attrs.items():
41 if k.startswith('test'):
42 attrs[k] = ignore_not_implemented(v)
43 return type.__new__(cls, name, bases, attrs)
46 class IntrospectionTests(six.with_metaclass(IgnoreNotimplementedError, TestCase)):
47 def test_table_names(self):
48 tl = connection.introspection.table_names()
49 self.assertEqual(tl, sorted(tl))
50 self.assertTrue(Reporter._meta.db_table in tl,
51 "'%s' isn't in table_list()." % Reporter._meta.db_table)
52 self.assertTrue(Article._meta.db_table in tl,
53 "'%s' isn't in table_list()." % Article._meta.db_table)
55 def test_django_table_names(self):
56 cursor = connection.cursor()
57 cursor.execute('CREATE TABLE django_ixn_test_table (id INTEGER);')
58 tl = connection.introspection.django_table_names()
59 cursor.execute("DROP TABLE django_ixn_test_table;")
60 self.assertTrue('django_ixn_testcase_table' not in tl,
61 "django_table_names() returned a non-Django table")
63 def test_django_table_names_retval_type(self):
64 # Ticket #15216
65 cursor = connection.cursor()
66 cursor.execute('CREATE TABLE django_ixn_test_table (id INTEGER);')
68 tl = connection.introspection.django_table_names(only_existing=True)
69 self.assertIs(type(tl), list)
71 tl = connection.introspection.django_table_names(only_existing=False)
72 self.assertIs(type(tl), list)
74 def test_installed_models(self):
75 tables = [Article._meta.db_table, Reporter._meta.db_table]
76 models = connection.introspection.installed_models(tables)
77 self.assertEqual(models, set([Article, Reporter]))
79 def test_sequence_list(self):
80 sequences = connection.introspection.sequence_list()
81 expected = {'table': Reporter._meta.db_table, 'column': 'id'}
82 self.assertTrue(expected in sequences,
83 'Reporter sequence not found in sequence_list()')
85 def test_get_table_description_names(self):
86 cursor = connection.cursor()
87 desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
88 self.assertEqual([r[0] for r in desc],
89 [f.column for f in Reporter._meta.fields])
91 def test_get_table_description_types(self):
92 cursor = connection.cursor()
93 desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
94 self.assertEqual(
95 [datatype(r[1], r) for r in desc],
96 ['IntegerField', 'CharField', 'CharField', 'CharField', 'BigIntegerField']
99 # The following test fails on Oracle due to #17202 (can't correctly
100 # inspect the length of character columns).
101 @expectedFailureOnOracle
102 def test_get_table_description_col_lengths(self):
103 cursor = connection.cursor()
104 desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
105 self.assertEqual(
106 [r[3] for r in desc if datatype(r[1], r) == 'CharField'],
107 [30, 30, 75]
110 # Oracle forces null=True under the hood in some cases (see
111 # https://docs.djangoproject.com/en/dev/ref/databases/#null-and-empty-strings)
112 # so its idea about null_ok in cursor.description is different from ours.
113 @skipIfDBFeature('interprets_empty_strings_as_nulls')
114 def test_get_table_description_nullable(self):
115 cursor = connection.cursor()
116 desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
117 self.assertEqual(
118 [r[6] for r in desc],
119 [False, False, False, False, True]
122 # Regression test for #9991 - 'real' types in postgres
123 @skipUnlessDBFeature('has_real_datatype')
124 def test_postgresql_real_type(self):
125 cursor = connection.cursor()
126 cursor.execute("CREATE TABLE django_ixn_real_test_table (number REAL);")
127 desc = connection.introspection.get_table_description(cursor, 'django_ixn_real_test_table')
128 cursor.execute('DROP TABLE django_ixn_real_test_table;')
129 self.assertEqual(datatype(desc[0][1], desc[0]), 'FloatField')
131 def test_get_relations(self):
132 cursor = connection.cursor()
133 relations = connection.introspection.get_relations(cursor, Article._meta.db_table)
135 # Older versions of MySQL don't have the chops to report on this stuff,
136 # so just skip it if no relations come back. If they do, though, we
137 # should test that the response is correct.
138 if relations:
139 # That's {field_index: (field_index_other_table, other_table)}
140 self.assertEqual(relations, {3: (0, Reporter._meta.db_table)})
142 def test_get_key_columns(self):
143 cursor = connection.cursor()
144 key_columns = connection.introspection.get_key_columns(cursor, Article._meta.db_table)
145 self.assertEqual(key_columns, [('reporter_id', Reporter._meta.db_table, 'id')])
147 def test_get_primary_key_column(self):
148 cursor = connection.cursor()
149 primary_key_column = connection.introspection.get_primary_key_column(cursor, Article._meta.db_table)
150 self.assertEqual(primary_key_column, 'id')
152 def test_get_indexes(self):
153 cursor = connection.cursor()
154 indexes = connection.introspection.get_indexes(cursor, Article._meta.db_table)
155 self.assertEqual(indexes['reporter_id'], {'unique': False, 'primary_key': False})
157 def test_get_indexes_multicol(self):
159 Test that multicolumn indexes are not included in the introspection
160 results.
162 cursor = connection.cursor()
163 indexes = connection.introspection.get_indexes(cursor, Reporter._meta.db_table)
164 self.assertNotIn('first_name', indexes)
165 self.assertIn('id', indexes)
168 def datatype(dbtype, description):
169 """Helper to convert a data type into a string."""
170 dt = connection.introspection.get_field_type(dbtype, description)
171 if type(dt) is tuple:
172 return dt[0]
173 else:
174 return dt