Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added introspection tests for most of Django model fields

  • Loading branch information...
commit eb03fe164277b3d7a2ff856415457176d6a2ee51 1 parent 31308b3
Claude Paroz authored January 31, 2013
2  django/core/management/commands/inspectdb.py
@@ -205,7 +205,7 @@ def get_field_type(self, connection, table_name, row):
205 205
 
206 206
         # Add max_length for all CharFields.
207 207
         if field_type == 'CharField' and row[3]:
208  
-            field_params['max_length'] = row[3]
  208
+            field_params['max_length'] = int(row[3])
209 209
 
210 210
         if field_type == 'DecimalField':
211 211
             field_params['max_digits'] = row[4]
25  tests/regressiontests/inspectdb/models.py
@@ -29,3 +29,28 @@ class SpecialColumnName(models.Model):
29 29
     field_field_2 = models.IntegerField(db_column='__field')
30 30
     # Other chars
31 31
     prc_x = models.IntegerField(db_column='prc(%) x')
  32
+
  33
+class ColumnTypes(models.Model):
  34
+    id = models.AutoField(primary_key=True)
  35
+    field1 = models.BigIntegerField()
  36
+    field2 = models.BooleanField()
  37
+    field3 = models.CharField(max_length=10)
  38
+    field4 = models.CommaSeparatedIntegerField(max_length=99)
  39
+    field5 = models.DateField()
  40
+    field6 = models.DateTimeField()
  41
+    field7 = models.DecimalField(max_digits=6, decimal_places=1)
  42
+    field8 = models.EmailField()
  43
+    field9 = models.FileField(upload_to="unused")
  44
+    field10 = models.FilePathField()
  45
+    field11 = models.FloatField()
  46
+    field12 = models.IntegerField()
  47
+    field13 = models.IPAddressField()
  48
+    field14 = models.GenericIPAddressField(protocol="ipv4")
  49
+    field15 = models.NullBooleanField()
  50
+    field16 = models.PositiveIntegerField()
  51
+    field17 = models.PositiveSmallIntegerField()
  52
+    field18 = models.SlugField()
  53
+    field19 = models.SmallIntegerField()
  54
+    field20 = models.TextField()
  55
+    field21 = models.TimeField()
  56
+    field22 = models.URLField()
74  tests/regressiontests/inspectdb/tests.py
... ...
@@ -1,5 +1,7 @@
1 1
 from __future__ import unicode_literals
2 2
 
  3
+import re
  4
+
3 5
 from django.core.management import call_command
4 6
 from django.db import connection
5 7
 from django.test import TestCase, skipUnlessDBFeature
@@ -21,6 +23,78 @@ def test_stealth_table_name_filter_option(self):
21 23
         # inspected
22 24
         self.assertNotIn("class DjangoContentType(models.Model):", out.getvalue(), msg=error_message)
23 25
 
  26
+    def test_field_types(self):
  27
+        """Test introspection of various Django field types"""
  28
+        out = StringIO()
  29
+        call_command('inspectdb',
  30
+                     table_name_filter=lambda tn:tn.startswith('inspectdb_columntypes'),
  31
+                     stdout=out)
  32
+        output = out.getvalue()
  33
+        def assertFieldType(name, definition):
  34
+            out_def = re.search(r'^\s*%s = (models.*)$' % name, output, re.MULTILINE).groups()[0]
  35
+            self.assertEqual(definition, out_def)
  36
+
  37
+        assertFieldType('id', "models.IntegerField(primary_key=True)")
  38
+        assertFieldType('field1', "models.BigIntegerField()")
  39
+        if connection.vendor == 'mysql':
  40
+            # No native boolean type on MySQL
  41
+            assertFieldType('field2', "models.IntegerField()")
  42
+        else:
  43
+            assertFieldType('field2', "models.BooleanField()")
  44
+        assertFieldType('field3', "models.CharField(max_length=10)")
  45
+        # CommaSeparatedIntegerField
  46
+        assertFieldType('field4', "models.CharField(max_length=99)")
  47
+        assertFieldType('field5', "models.DateField()")
  48
+        assertFieldType('field6', "models.DateTimeField()")
  49
+        if connection.vendor == 'sqlite':
  50
+            # Ticket #5014
  51
+            assertFieldType('field7', "models.DecimalField(max_digits=None, decimal_places=None)")
  52
+        elif connection.vendor == 'mysql':
  53
+            pass # Ticket #5014
  54
+        else:
  55
+            assertFieldType('field7', "models.DecimalField(max_digits=6, decimal_places=1)")
  56
+        # EmailField
  57
+        assertFieldType('field8', "models.CharField(max_length=75)")
  58
+        # FileField
  59
+        assertFieldType('field9', "models.CharField(max_length=100)")
  60
+        # FilePathField
  61
+        assertFieldType('field10', "models.CharField(max_length=100)")
  62
+        assertFieldType('field11', "models.FloatField()")
  63
+        assertFieldType('field12', "models.IntegerField()")
  64
+        if connection.vendor == 'postgresql':
  65
+            # Only PostgreSQL has a specific type
  66
+            assertFieldType('field13', "models.GenericIPAddressField()")
  67
+            assertFieldType('field14', "models.GenericIPAddressField()")
  68
+        else:
  69
+            assertFieldType('field13', "models.CharField(max_length=15)")
  70
+            assertFieldType('field14', "models.CharField(max_length=39)")
  71
+        # Ticket #19341
  72
+        #assertFieldType('field15', "models.NullBooleanField()")
  73
+        if connection.vendor == 'sqlite':
  74
+            assertFieldType('field16', "models.PositiveIntegerField()")
  75
+            assertFieldType('field17', "models.PositiveSmallIntegerField()")
  76
+        else:
  77
+            # 'unsigned' property undetected on other backends
  78
+            assertFieldType('field16', "models.IntegerField()")
  79
+            if connection.vendor == 'postgresql':
  80
+                assertFieldType('field17', "models.SmallIntegerField()")
  81
+            else:
  82
+                assertFieldType('field17', "models.IntegerField()")
  83
+        # SlugField
  84
+        assertFieldType('field18', "models.CharField(max_length=50)")
  85
+        if connection.vendor in ('sqlite', 'postgresql'):
  86
+            assertFieldType('field19', "models.SmallIntegerField()")
  87
+        else:
  88
+            assertFieldType('field19', "models.IntegerField()")
  89
+        assertFieldType('field20', "models.TextField()")
  90
+        if connection.vendor == 'mysql':
  91
+            # Ticket #19709
  92
+            assertFieldType('field21', "models.TextField() # This field type is a guess.")
  93
+        else:
  94
+            assertFieldType('field21', "models.TimeField()")
  95
+        # URLField
  96
+        assertFieldType('field22', "models.CharField(max_length=200)")
  97
+
24 98
     @skipUnlessDBFeature('can_introspect_foreign_keys')
25 99
     def test_attribute_name_not_python_keyword(self):
26 100
         out = StringIO()

0 notes on commit eb03fe1

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