~ubuntu-branches/ubuntu/saucy/python-django/saucy-updates

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Luke Faraone, Jakub Wilk, Luke Faraone
  • Date: 2013-05-09 15:10:47 UTC
  • mfrom: (1.1.21) (4.4.27 sid)
  • Revision ID: package-import@ubuntu.com-20130509151047-aqv8d71oj9wvcv8c
Tags: 1.5.1-2
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Luke Faraone ]
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from __future__ import absolute_import
 
1
from __future__ import absolute_import, unicode_literals
2
2
 
3
3
from functools import update_wrapper
4
4
 
5
5
from django.db import connection
6
6
from django.test import TestCase, skipUnlessDBFeature, skipIfDBFeature
 
7
from django.utils import six, unittest
7
8
 
8
9
from .models import Reporter, Article
9
10
 
10
 
#
 
11
if connection.vendor == 'oracle':
 
12
    expectedFailureOnOracle = unittest.expectedFailure
 
13
else:
 
14
    expectedFailureOnOracle = lambda f: f
 
15
 
 
16
 
11
17
# The introspection module is optional, so methods tested here might raise
12
18
# NotImplementedError. This is perfectly acceptable behavior for the backend
13
19
# in question, but the tests need to handle this without failing. Ideally we'd
17
23
# wrapper that ignores the exception.
18
24
#
19
25
# The metaclass is just for fun.
20
 
#
 
26
 
21
27
 
22
28
def ignore_not_implemented(func):
23
29
    def _inner(*args, **kwargs):
28
34
    update_wrapper(_inner, func)
29
35
    return _inner
30
36
 
 
37
 
31
38
class IgnoreNotimplementedError(type):
32
39
    def __new__(cls, name, bases, attrs):
33
 
        for k,v in attrs.items():
 
40
        for k, v in attrs.items():
34
41
            if k.startswith('test'):
35
42
                attrs[k] = ignore_not_implemented(v)
36
43
        return type.__new__(cls, name, bases, attrs)
37
44
 
38
 
class IntrospectionTests(TestCase):
39
 
    __metaclass__ = IgnoreNotimplementedError
40
45
 
 
46
class IntrospectionTests(six.with_metaclass(IgnoreNotimplementedError, TestCase)):
41
47
    def test_table_names(self):
42
48
        tl = connection.introspection.table_names()
 
49
        self.assertEqual(tl, sorted(tl))
43
50
        self.assertTrue(Reporter._meta.db_table in tl,
44
51
                     "'%s' isn't in table_list()." % Reporter._meta.db_table)
45
52
        self.assertTrue(Article._meta.db_table in tl,
89
96
            ['IntegerField', 'CharField', 'CharField', 'CharField', 'BigIntegerField']
90
97
        )
91
98
 
 
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]
 
108
        )
 
109
 
92
110
    # Oracle forces null=True under the hood in some cases (see
93
111
    # https://docs.djangoproject.com/en/dev/ref/databases/#null-and-empty-strings)
94
112
    # so its idea about null_ok in cursor.description is different from ours.
124
142
    def test_get_key_columns(self):
125
143
        cursor = connection.cursor()
126
144
        key_columns = connection.introspection.get_key_columns(cursor, Article._meta.db_table)
127
 
        self.assertEqual(key_columns, [(u'reporter_id', Reporter._meta.db_table, u'id')])
 
145
        self.assertEqual(key_columns, [('reporter_id', Reporter._meta.db_table, 'id')])
128
146
 
129
147
    def test_get_primary_key_column(self):
130
148
        cursor = connection.cursor()
131
149
        primary_key_column = connection.introspection.get_primary_key_column(cursor, Article._meta.db_table)
132
 
        self.assertEqual(primary_key_column, u'id')
 
150
        self.assertEqual(primary_key_column, 'id')
133
151
 
134
152
    def test_get_indexes(self):
135
153
        cursor = connection.cursor()
136
154
        indexes = connection.introspection.get_indexes(cursor, Article._meta.db_table)
137
155
        self.assertEqual(indexes['reporter_id'], {'unique': False, 'primary_key': False})
138
156
 
 
157
    def test_get_indexes_multicol(self):
 
158
        """
 
159
        Test that multicolumn indexes are not included in the introspection
 
160
        results.
 
161
        """
 
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)
 
166
 
139
167
 
140
168
def datatype(dbtype, description):
141
169
    """Helper to convert a data type into a string."""