Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Added tests for database introspection. Refs #9779. Backport …

…r10362 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10363 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 38aeee4839f273b0575dbbb605303e27c3f87da8 1 parent fd7280e
Jacob Kaplan-Moss authored April 02, 2009
0  tests/regressiontests/introspection/__init__.py
No changes.
20  tests/regressiontests/introspection/models.py
... ...
@@ -0,0 +1,20 @@
  1
+from django.db import models
  2
+
  3
+class Reporter(models.Model):
  4
+    first_name = models.CharField(max_length=30)
  5
+    last_name = models.CharField(max_length=30)
  6
+    email = models.EmailField()
  7
+
  8
+    def __unicode__(self):
  9
+        return u"%s %s" % (self.first_name, self.last_name)
  10
+
  11
+class Article(models.Model):
  12
+    headline = models.CharField(max_length=100)
  13
+    pub_date = models.DateField()
  14
+    reporter = models.ForeignKey(Reporter)
  15
+
  16
+    def __unicode__(self):
  17
+        return self.headline
  18
+
  19
+    class Meta:
  20
+        ordering = ('headline',)
99  tests/regressiontests/introspection/tests.py
... ...
@@ -0,0 +1,99 @@
  1
+from django.db import connection
  2
+from django.test import TestCase
  3
+from django.utils import functional
  4
+
  5
+from models import Reporter, Article
  6
+
  7
+#
  8
+# The introspection module is optional, so methods tested here might raise
  9
+# NotImplementedError. This is perfectly acceptable behavior for the backend
  10
+# in question, but the tests need to handle this without failing. Ideally we'd
  11
+# skip these tests, but until #4788 is done we'll just ignore them.
  12
+#
  13
+# The easiest way to accomplish this is to decorate every test case with a 
  14
+# wrapper that ignores the exception.
  15
+#
  16
+# The metaclass is just for fun.
  17
+#
  18
+
  19
+def ignore_not_implemented(func):
  20
+    def _inner(*args, **kwargs):
  21
+        try:
  22
+            return func(*args, **kwargs)
  23
+        except NotImplementedError:
  24
+            return None
  25
+    functional.update_wrapper(_inner, func)
  26
+    return _inner
  27
+
  28
+class IgnoreNotimplementedError(type):
  29
+    def __new__(cls, name, bases, attrs):
  30
+        for k,v in attrs.items():
  31
+            if k.startswith('test'):
  32
+                attrs[k] = ignore_not_implemented(v)
  33
+        return type.__new__(cls, name, bases, attrs)
  34
+
  35
+class IntrospectionTests(TestCase):
  36
+    __metaclass__ = IgnoreNotimplementedError
  37
+    
  38
+    def test_table_names(self):
  39
+        tl = connection.introspection.table_names()
  40
+        self.assert_(Reporter._meta.db_table in tl, 
  41
+                     "'%s' isn't in table_list()." % Reporter._meta.db_table)
  42
+        self.assert_(Article._meta.db_table in tl, 
  43
+                     "'%s' isn't in table_list()." % Article._meta.db_table)
  44
+                     
  45
+    def test_django_table_names(self):
  46
+        cursor = connection.cursor()
  47
+        cursor.execute('CREATE TABLE django_introspection_testcase_table (id INTEGER);');
  48
+        tl = connection.introspection.django_table_names()
  49
+        self.assert_('django_introspection_testcase_table' not in tl,
  50
+                     "django_table_names() returned a non-Django table")
  51
+        cursor.execute("DROP TABLE django_introspection_testcase_table;")
  52
+                        
  53
+    def test_installed_models(self):
  54
+        tables = [Article._meta.db_table, Reporter._meta.db_table]
  55
+        models = connection.introspection.installed_models(tables)
  56
+        self.assertEqual(models, set([Article, Reporter]))
  57
+        
  58
+    def test_sequence_list(self):
  59
+        sequences = connection.introspection.sequence_list()
  60
+        expected = {'table': Reporter._meta.db_table, 'column': 'id'}
  61
+        self.assert_(expected in sequences, 
  62
+                     'Reporter sequence not found in sequence_list()')
  63
+                     
  64
+    def test_get_table_description_names(self):
  65
+        cursor = connection.cursor()
  66
+        desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
  67
+        self.assertEqual([r[0] for r in desc],
  68
+                         [f.column for f in Reporter._meta.fields])
  69
+    
  70
+    def test_get_table_description_types(self):
  71
+        cursor = connection.cursor()
  72
+        desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
  73
+
  74
+        # Convert the datatype into a string
  75
+        def datatype(dbtype):
  76
+            dt = connection.introspection.data_types_reverse[dbtype]
  77
+            if type(dt) is tuple:
  78
+                return dt[0]
  79
+            else:
  80
+                return dt
  81
+            
  82
+        self.assertEqual([datatype(r[1]) for r in desc],
  83
+                          ['IntegerField', 'CharField', 'CharField', 'CharField'])
  84
+
  85
+    def test_get_relations(self):
  86
+        cursor = connection.cursor()
  87
+        relations = connection.introspection.get_relations(cursor, Article._meta.db_table)
  88
+        
  89
+        # Older versions of MySQL don't have the chops to report on this stuff,
  90
+        # so just skip it if no relations come back. If they do, though, we
  91
+        # should test that the response is correct.
  92
+        if relations:
  93
+            # That's {field_index: (field_index_other_table, other_table)}
  94
+            self.assertEqual(relations, {3: (0, Reporter._meta.db_table)})
  95
+            
  96
+    def test_get_indexes(self):
  97
+        cursor = connection.cursor()
  98
+        indexes = connection.introspection.get_indexes(cursor, Article._meta.db_table)
  99
+        self.assertEqual(indexes['reporter_id'], {'unique': False, 'primary_key': False})

0 notes on commit 38aeee4

Please sign in to comment.
Something went wrong with that request. Please try again.