Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added introspection tests for most of Django model fields

  • Loading branch information...
commit eb03fe164277b3d7a2ff856415457176d6a2ee51 1 parent 31308b3
Claude Paroz claudep authored
2  django/core/management/commands/inspectdb.py
View
@@ -205,7 +205,7 @@ def get_field_type(self, connection, table_name, row):
# Add max_length for all CharFields.
if field_type == 'CharField' and row[3]:
- field_params['max_length'] = row[3]
+ field_params['max_length'] = int(row[3])
if field_type == 'DecimalField':
field_params['max_digits'] = row[4]
25 tests/regressiontests/inspectdb/models.py
View
@@ -29,3 +29,28 @@ class SpecialColumnName(models.Model):
field_field_2 = models.IntegerField(db_column='__field')
# Other chars
prc_x = models.IntegerField(db_column='prc(%) x')
+
+class ColumnTypes(models.Model):
+ id = models.AutoField(primary_key=True)
+ field1 = models.BigIntegerField()
+ field2 = models.BooleanField()
+ field3 = models.CharField(max_length=10)
+ field4 = models.CommaSeparatedIntegerField(max_length=99)
+ field5 = models.DateField()
+ field6 = models.DateTimeField()
+ field7 = models.DecimalField(max_digits=6, decimal_places=1)
+ field8 = models.EmailField()
+ field9 = models.FileField(upload_to="unused")
+ field10 = models.FilePathField()
+ field11 = models.FloatField()
+ field12 = models.IntegerField()
+ field13 = models.IPAddressField()
+ field14 = models.GenericIPAddressField(protocol="ipv4")
+ field15 = models.NullBooleanField()
+ field16 = models.PositiveIntegerField()
+ field17 = models.PositiveSmallIntegerField()
+ field18 = models.SlugField()
+ field19 = models.SmallIntegerField()
+ field20 = models.TextField()
+ field21 = models.TimeField()
+ field22 = models.URLField()
74 tests/regressiontests/inspectdb/tests.py
View
@@ -1,5 +1,7 @@
from __future__ import unicode_literals
+import re
+
from django.core.management import call_command
from django.db import connection
from django.test import TestCase, skipUnlessDBFeature
@@ -21,6 +23,78 @@ def test_stealth_table_name_filter_option(self):
# inspected
self.assertNotIn("class DjangoContentType(models.Model):", out.getvalue(), msg=error_message)
+ def test_field_types(self):
+ """Test introspection of various Django field types"""
+ out = StringIO()
+ call_command('inspectdb',
+ table_name_filter=lambda tn:tn.startswith('inspectdb_columntypes'),
+ stdout=out)
+ output = out.getvalue()
+ def assertFieldType(name, definition):
+ out_def = re.search(r'^\s*%s = (models.*)$' % name, output, re.MULTILINE).groups()[0]
+ self.assertEqual(definition, out_def)
+
+ assertFieldType('id', "models.IntegerField(primary_key=True)")
+ assertFieldType('field1', "models.BigIntegerField()")
+ if connection.vendor == 'mysql':
+ # No native boolean type on MySQL
+ assertFieldType('field2', "models.IntegerField()")
+ else:
+ assertFieldType('field2', "models.BooleanField()")
+ assertFieldType('field3', "models.CharField(max_length=10)")
+ # CommaSeparatedIntegerField
+ assertFieldType('field4', "models.CharField(max_length=99)")
+ assertFieldType('field5', "models.DateField()")
+ assertFieldType('field6', "models.DateTimeField()")
+ if connection.vendor == 'sqlite':
+ # Ticket #5014
+ assertFieldType('field7', "models.DecimalField(max_digits=None, decimal_places=None)")
+ elif connection.vendor == 'mysql':
+ pass # Ticket #5014
+ else:
+ assertFieldType('field7', "models.DecimalField(max_digits=6, decimal_places=1)")
+ # EmailField
+ assertFieldType('field8', "models.CharField(max_length=75)")
+ # FileField
+ assertFieldType('field9', "models.CharField(max_length=100)")
+ # FilePathField
+ assertFieldType('field10', "models.CharField(max_length=100)")
+ assertFieldType('field11', "models.FloatField()")
+ assertFieldType('field12', "models.IntegerField()")
+ if connection.vendor == 'postgresql':
+ # Only PostgreSQL has a specific type
+ assertFieldType('field13', "models.GenericIPAddressField()")
+ assertFieldType('field14', "models.GenericIPAddressField()")
+ else:
+ assertFieldType('field13', "models.CharField(max_length=15)")
+ assertFieldType('field14', "models.CharField(max_length=39)")
+ # Ticket #19341
+ #assertFieldType('field15', "models.NullBooleanField()")
+ if connection.vendor == 'sqlite':
+ assertFieldType('field16', "models.PositiveIntegerField()")
+ assertFieldType('field17', "models.PositiveSmallIntegerField()")
+ else:
+ # 'unsigned' property undetected on other backends
+ assertFieldType('field16', "models.IntegerField()")
+ if connection.vendor == 'postgresql':
+ assertFieldType('field17', "models.SmallIntegerField()")
+ else:
+ assertFieldType('field17', "models.IntegerField()")
+ # SlugField
+ assertFieldType('field18', "models.CharField(max_length=50)")
+ if connection.vendor in ('sqlite', 'postgresql'):
+ assertFieldType('field19', "models.SmallIntegerField()")
+ else:
+ assertFieldType('field19', "models.IntegerField()")
+ assertFieldType('field20', "models.TextField()")
+ if connection.vendor == 'mysql':
+ # Ticket #19709
+ assertFieldType('field21', "models.TextField() # This field type is a guess.")
+ else:
+ assertFieldType('field21', "models.TimeField()")
+ # URLField
+ assertFieldType('field22', "models.CharField(max_length=200)")
+
@skipUnlessDBFeature('can_introspect_foreign_keys')
def test_attribute_name_not_python_keyword(self):
out = StringIO()
Please sign in to comment.
Something went wrong with that request. Please try again.