From eb03fe164277b3d7a2ff856415457176d6a2ee51 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 31 Jan 2013 20:34:36 +0100 Subject: [PATCH] Added introspection tests for most of Django model fields --- django/core/management/commands/inspectdb.py | 2 +- tests/regressiontests/inspectdb/models.py | 25 +++++++ tests/regressiontests/inspectdb/tests.py | 74 ++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py index 443312c66d..08870af56b 100644 --- a/django/core/management/commands/inspectdb.py +++ b/django/core/management/commands/inspectdb.py @@ -205,7 +205,7 @@ class Command(NoArgsCommand): # 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] diff --git a/tests/regressiontests/inspectdb/models.py b/tests/regressiontests/inspectdb/models.py index a8adf862b1..ae14c78eb7 100644 --- a/tests/regressiontests/inspectdb/models.py +++ b/tests/regressiontests/inspectdb/models.py @@ -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() diff --git a/tests/regressiontests/inspectdb/tests.py b/tests/regressiontests/inspectdb/tests.py index f9cbbce5c4..5fa1eb1095 100644 --- a/tests/regressiontests/inspectdb/tests.py +++ b/tests/regressiontests/inspectdb/tests.py @@ -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 @@ class InspectDBTestCase(TestCase): # 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()