Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.6.x] Made Oracle introspect FloatFields correctly

Broke InspectDBTestCase.test_field_types in two:
- a test_number_field_types, which now passes on Oracle too
- a test_field_types, for all non-numeric fields, which is still expected to fail

Also made some pep8 fixes in the tests file. Refs #19884

Thanks Tim Graham for review.

Backport of e9d12ba from master
  • Loading branch information...
commit 5f42c0219550b62e62db6e0f3834671a994e3cd8 1 parent 0573120
@shaib shaib authored
Showing with 48 additions and 33 deletions.
  1. +13 −10 django/db/backends/oracle/introspection.py
  2. +35 −23 tests/inspectdb/tests.py
View
23 django/db/backends/oracle/introspection.py
@@ -30,16 +30,19 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
def get_field_type(self, data_type, description):
# If it's a NUMBER with scale == 0, consider it an IntegerField
- if data_type == cx_Oracle.NUMBER and description[5] == 0:
- if description[4] > 11:
- return 'BigIntegerField'
- elif description[4]==1:
- return 'BooleanField'
- else:
- return 'IntegerField'
- else:
- return super(DatabaseIntrospection, self).get_field_type(
- data_type, description)
+ if data_type == cx_Oracle.NUMBER:
+ precision, scale = description[4:6]
+ if scale == 0:
+ if precision > 11:
+ return 'BigIntegerField'
+ elif precision == 1:
+ return 'BooleanField'
+ else:
+ return 'IntegerField'
+ elif scale == -127:
+ return 'FloatField'
+
+ return super(DatabaseIntrospection, self).get_field_type(data_type, description)
def get_table_list(self, cursor):
"Returns a list of table names in the current database."
View
58 tests/inspectdb/tests.py
@@ -29,19 +29,49 @@ def test_stealth_table_name_filter_option(self):
# inspected
self.assertNotIn("class DjangoContentType(models.Model):", out.getvalue(), msg=error_message)
- # Inspecting oracle DB doesn't produce correct results, see #19884
- @expectedFailureOnOracle
- def test_field_types(self):
- """Test introspection of various Django field types"""
+ def make_field_type_asserter(self):
+ """Call inspectdb and return a function to validate a field type in its output"""
out = StringIO()
call_command('inspectdb',
- table_name_filter=lambda tn:tn.startswith('inspectdb_columntypes'),
+ 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)
+ return assertFieldType
+
+ # Inspecting oracle DB doesn't produce correct results, see #19884
+ @expectedFailureOnOracle
+ def test_field_types(self):
+ """Test introspection of various Django field types"""
+ assertFieldType = self.make_field_type_asserter()
+
+ assertFieldType('char_field', "models.CharField(max_length=10)")
+ assertFieldType('comma_separated_int_field', "models.CharField(max_length=99)")
+ assertFieldType('date_field', "models.DateField()")
+ assertFieldType('date_time_field', "models.DateTimeField()")
+ assertFieldType('email_field', "models.CharField(max_length=75)")
+ assertFieldType('file_field', "models.CharField(max_length=100)")
+ assertFieldType('file_path_field', "models.CharField(max_length=100)")
+ if connection.vendor == 'postgresql':
+ # Only PostgreSQL has a specific type
+ assertFieldType('ip_address_field', "models.GenericIPAddressField()")
+ assertFieldType('gen_ip_adress_field', "models.GenericIPAddressField()")
+ else:
+ assertFieldType('ip_address_field', "models.CharField(max_length=15)")
+ assertFieldType('gen_ip_adress_field', "models.CharField(max_length=39)")
+ assertFieldType('slug_field', "models.CharField(max_length=50)")
+ assertFieldType('text_field', "models.TextField()")
+ assertFieldType('time_field', "models.TimeField()")
+ assertFieldType('url_field', "models.CharField(max_length=200)")
+
+ def test_number_field_types(self):
+ """Test introspection of various Django field types"""
+ assertFieldType = self.make_field_type_asserter()
+
assertFieldType('id', "models.IntegerField(primary_key=True)")
assertFieldType('big_int_field', "models.BigIntegerField()")
if connection.vendor == 'mysql':
@@ -51,28 +81,14 @@ def assertFieldType(name, definition):
else:
assertFieldType('bool_field', "models.BooleanField()")
assertFieldType('null_bool_field', "models.NullBooleanField()")
- assertFieldType('char_field', "models.CharField(max_length=10)")
- assertFieldType('comma_separated_int_field', "models.CharField(max_length=99)")
- assertFieldType('date_field', "models.DateField()")
- assertFieldType('date_time_field', "models.DateTimeField()")
if connection.vendor == 'sqlite':
# Guessed arguments, see #5014
assertFieldType('decimal_field', "models.DecimalField(max_digits=10, decimal_places=5) "
"# max_digits and decimal_places have been guessed, as this database handles decimal fields as float")
else:
assertFieldType('decimal_field', "models.DecimalField(max_digits=6, decimal_places=1)")
- assertFieldType('email_field', "models.CharField(max_length=75)")
- assertFieldType('file_field', "models.CharField(max_length=100)")
- assertFieldType('file_path_field', "models.CharField(max_length=100)")
assertFieldType('float_field', "models.FloatField()")
assertFieldType('int_field', "models.IntegerField()")
- if connection.vendor == 'postgresql':
- # Only PostgreSQL has a specific type
- assertFieldType('ip_address_field', "models.GenericIPAddressField()")
- assertFieldType('gen_ip_adress_field', "models.GenericIPAddressField()")
- else:
- assertFieldType('ip_address_field', "models.CharField(max_length=15)")
- assertFieldType('gen_ip_adress_field', "models.CharField(max_length=39)")
if connection.vendor == 'sqlite':
assertFieldType('pos_int_field', "models.PositiveIntegerField()")
assertFieldType('pos_small_int_field', "models.PositiveSmallIntegerField()")
@@ -83,14 +99,10 @@ def assertFieldType(name, definition):
assertFieldType('pos_small_int_field', "models.SmallIntegerField()")
else:
assertFieldType('pos_small_int_field', "models.IntegerField()")
- assertFieldType('slug_field', "models.CharField(max_length=50)")
if connection.vendor in ('sqlite', 'postgresql'):
assertFieldType('small_int_field', "models.SmallIntegerField()")
else:
assertFieldType('small_int_field', "models.IntegerField()")
- assertFieldType('text_field', "models.TextField()")
- assertFieldType('time_field', "models.TimeField()")
- assertFieldType('url_field', "models.CharField(max_length=200)")
@skipUnlessDBFeature('can_introspect_foreign_keys')
def test_attribute_name_not_python_keyword(self):
Please sign in to comment.
Something went wrong with that request. Please try again.