Author: jacob
Date: 2009-04-01 23:34:42 -0500 (Wed, 01 Apr 2009)
New Revision: 10363

Added:
   django/branches/releases/1.0.X/tests/regressiontests/introspection/
   
django/branches/releases/1.0.X/tests/regressiontests/introspection/__init__.py
   django/branches/releases/1.0.X/tests/regressiontests/introspection/models.py
   django/branches/releases/1.0.X/tests/regressiontests/introspection/tests.py
Log:
[1.0.X] Added tests for database introspection. Refs #9779. Backport r10362 
from trunk.

Added: 
django/branches/releases/1.0.X/tests/regressiontests/introspection/__init__.py
===================================================================

Added: 
django/branches/releases/1.0.X/tests/regressiontests/introspection/models.py
===================================================================
--- 
django/branches/releases/1.0.X/tests/regressiontests/introspection/models.py    
                            (rev 0)
+++ 
django/branches/releases/1.0.X/tests/regressiontests/introspection/models.py    
    2009-04-02 04:34:42 UTC (rev 10363)
@@ -0,0 +1,20 @@
+from django.db import models
+
+class Reporter(models.Model):
+    first_name = models.CharField(max_length=30)
+    last_name = models.CharField(max_length=30)
+    email = models.EmailField()
+
+    def __unicode__(self):
+        return u"%s %s" % (self.first_name, self.last_name)
+
+class Article(models.Model):
+    headline = models.CharField(max_length=100)
+    pub_date = models.DateField()
+    reporter = models.ForeignKey(Reporter)
+
+    def __unicode__(self):
+        return self.headline
+
+    class Meta:
+        ordering = ('headline',)
\ No newline at end of file

Added: 
django/branches/releases/1.0.X/tests/regressiontests/introspection/tests.py
===================================================================
--- django/branches/releases/1.0.X/tests/regressiontests/introspection/tests.py 
                        (rev 0)
+++ django/branches/releases/1.0.X/tests/regressiontests/introspection/tests.py 
2009-04-02 04:34:42 UTC (rev 10363)
@@ -0,0 +1,99 @@
+from django.db import connection
+from django.test import TestCase
+from django.utils import functional
+
+from models import Reporter, Article
+
+#
+# The introspection module is optional, so methods tested here might raise
+# NotImplementedError. This is perfectly acceptable behavior for the backend
+# in question, but the tests need to handle this without failing. Ideally we'd
+# skip these tests, but until #4788 is done we'll just ignore them.
+#
+# The easiest way to accomplish this is to decorate every test case with a 
+# wrapper that ignores the exception.
+#
+# The metaclass is just for fun.
+#
+
+def ignore_not_implemented(func):
+    def _inner(*args, **kwargs):
+        try:
+            return func(*args, **kwargs)
+        except NotImplementedError:
+            return None
+    functional.update_wrapper(_inner, func)
+    return _inner
+
+class IgnoreNotimplementedError(type):
+    def __new__(cls, name, bases, attrs):
+        for k,v in attrs.items():
+            if k.startswith('test'):
+                attrs[k] = ignore_not_implemented(v)
+        return type.__new__(cls, name, bases, attrs)
+
+class IntrospectionTests(TestCase):
+    __metaclass__ = IgnoreNotimplementedError
+    
+    def test_table_names(self):
+        tl = connection.introspection.table_names()
+        self.assert_(Reporter._meta.db_table in tl, 
+                     "'%s' isn't in table_list()." % Reporter._meta.db_table)
+        self.assert_(Article._meta.db_table in tl, 
+                     "'%s' isn't in table_list()." % Article._meta.db_table)
+                     
+    def test_django_table_names(self):
+        cursor = connection.cursor()
+        cursor.execute('CREATE TABLE django_introspection_testcase_table (id 
INTEGER);');
+        tl = connection.introspection.django_table_names()
+        self.assert_('django_introspection_testcase_table' not in tl,
+                     "django_table_names() returned a non-Django table")
+        cursor.execute("DROP TABLE django_introspection_testcase_table;")
+                        
+    def test_installed_models(self):
+        tables = [Article._meta.db_table, Reporter._meta.db_table]
+        models = connection.introspection.installed_models(tables)
+        self.assertEqual(models, set([Article, Reporter]))
+        
+    def test_sequence_list(self):
+        sequences = connection.introspection.sequence_list()
+        expected = {'table': Reporter._meta.db_table, 'column': 'id'}
+        self.assert_(expected in sequences, 
+                     'Reporter sequence not found in sequence_list()')
+                     
+    def test_get_table_description_names(self):
+        cursor = connection.cursor()
+        desc = connection.introspection.get_table_description(cursor, 
Reporter._meta.db_table)
+        self.assertEqual([r[0] for r in desc],
+                         [f.column for f in Reporter._meta.fields])
+    
+    def test_get_table_description_types(self):
+        cursor = connection.cursor()
+        desc = connection.introspection.get_table_description(cursor, 
Reporter._meta.db_table)
+
+        # Convert the datatype into a string
+        def datatype(dbtype):
+            dt = connection.introspection.data_types_reverse[dbtype]
+            if type(dt) is tuple:
+                return dt[0]
+            else:
+                return dt
+            
+        self.assertEqual([datatype(r[1]) for r in desc],
+                          ['IntegerField', 'CharField', 'CharField', 
'CharField'])
+
+    def test_get_relations(self):
+        cursor = connection.cursor()
+        relations = connection.introspection.get_relations(cursor, 
Article._meta.db_table)
+        
+        # Older versions of MySQL don't have the chops to report on this stuff,
+        # so just skip it if no relations come back. If they do, though, we
+        # should test that the response is correct.
+        if relations:
+            # That's {field_index: (field_index_other_table, other_table)}
+            self.assertEqual(relations, {3: (0, Reporter._meta.db_table)})
+            
+    def test_get_indexes(self):
+        cursor = connection.cursor()
+        indexes = connection.introspection.get_indexes(cursor, 
Article._meta.db_table)
+        self.assertEqual(indexes['reporter_id'], {'unique': False, 
'primary_key': False})
\ No newline at end of file


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to