~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to tests/regressiontests/introspection/tests.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 22.
  • Revision ID: james.westby@ubuntu.com-20090729112628-9qrzwnl9x32jxhbg
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from django.conf import settings
 
2
from django.db import connection
 
3
from django.test import TestCase
 
4
from django.utils import functional
 
5
 
 
6
from models import Reporter, Article
 
7
 
 
8
try:
 
9
    set
 
10
except NameError:
 
11
    from sets import Set as set     # Python 2.3 fallback
 
12
 
 
13
#
 
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.
 
18
#
 
19
# The easiest way to accomplish this is to decorate every test case with a
 
20
# wrapper that ignores the exception.
 
21
#
 
22
# The metaclass is just for fun.
 
23
#
 
24
 
 
25
def ignore_not_implemented(func):
 
26
    def _inner(*args, **kwargs):
 
27
        try:
 
28
            return func(*args, **kwargs)
 
29
        except NotImplementedError:
 
30
            return None
 
31
    functional.update_wrapper(_inner, func)
 
32
    return _inner
 
33
 
 
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)
 
40
 
 
41
class IntrospectionTests(TestCase):
 
42
    __metaclass__ = IgnoreNotimplementedError
 
43
 
 
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)
 
50
 
 
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")
 
58
 
 
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]))
 
63
 
 
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()')
 
69
 
 
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])
 
75
 
 
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'])
 
81
 
 
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')
 
90
 
 
91
    def test_get_relations(self):
 
92
        cursor = connection.cursor()
 
93
        relations = connection.introspection.get_relations(cursor, Article._meta.db_table)
 
94
 
 
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.
 
98
        if relations:
 
99
            # That's {field_index: (field_index_other_table, other_table)}
 
100
            self.assertEqual(relations, {3: (0, Reporter._meta.db_table)})
 
101
 
 
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})
 
106
 
 
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:
 
111
        return dt[0]
 
112
    else:
 
113
        return dt