2012-08-23 19:07:56 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-01-31 19:34:36 +00:00
|
|
|
import re
|
|
|
|
|
2011-01-24 14:58:05 +00:00
|
|
|
from django.core.management import call_command
|
2012-10-27 01:51:14 +00:00
|
|
|
from django.db import connection
|
2011-02-01 14:42:52 +00:00
|
|
|
from django.test import TestCase, skipUnlessDBFeature
|
2012-08-07 13:41:54 +00:00
|
|
|
from django.utils.six import StringIO
|
2011-01-24 14:58:05 +00:00
|
|
|
|
2011-01-24 15:18:56 +00:00
|
|
|
|
2011-01-24 14:58:05 +00:00
|
|
|
class InspectDBTestCase(TestCase):
|
2011-02-01 14:42:52 +00:00
|
|
|
|
2012-06-01 23:58:53 +00:00
|
|
|
def test_stealth_table_name_filter_option(self):
|
|
|
|
out = StringIO()
|
|
|
|
# Lets limit the introspection to tables created for models of this
|
|
|
|
# application
|
|
|
|
call_command('inspectdb',
|
|
|
|
table_name_filter=lambda tn:tn.startswith('inspectdb_'),
|
|
|
|
stdout=out)
|
|
|
|
error_message = "inspectdb has examined a table that should have been filtered out."
|
|
|
|
# contrib.contenttypes is one of the apps always installed when running
|
|
|
|
# the Django test suite, check that one of its tables hasn't been
|
|
|
|
# inspected
|
|
|
|
self.assertNotIn("class DjangoContentType(models.Model):", out.getvalue(), msg=error_message)
|
|
|
|
|
2013-01-31 19:34:36 +00:00
|
|
|
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()")
|
2013-01-31 19:55:00 +00:00
|
|
|
assertFieldType('field2b', "models.IntegerField(null=True, blank=True)")
|
2013-01-31 19:34:36 +00:00
|
|
|
else:
|
|
|
|
assertFieldType('field2', "models.BooleanField()")
|
2013-01-31 19:55:00 +00:00
|
|
|
assertFieldType('field2b', "models.NullBooleanField()")
|
2013-01-31 19:34:36 +00:00
|
|
|
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)")
|
|
|
|
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()")
|
2013-01-31 19:40:53 +00:00
|
|
|
assertFieldType('field21', "models.TimeField()")
|
2013-01-31 19:34:36 +00:00
|
|
|
# URLField
|
|
|
|
assertFieldType('field22', "models.CharField(max_length=200)")
|
|
|
|
|
2011-02-01 14:42:52 +00:00
|
|
|
@skipUnlessDBFeature('can_introspect_foreign_keys')
|
2011-01-24 14:58:05 +00:00
|
|
|
def test_attribute_name_not_python_keyword(self):
|
|
|
|
out = StringIO()
|
2012-06-01 23:58:53 +00:00
|
|
|
# Lets limit the introspection to tables created for models of this
|
|
|
|
# application
|
|
|
|
call_command('inspectdb',
|
|
|
|
table_name_filter=lambda tn:tn.startswith('inspectdb_'),
|
|
|
|
stdout=out)
|
2012-08-23 19:07:56 +00:00
|
|
|
output = out.getvalue()
|
2011-01-24 14:58:05 +00:00
|
|
|
error_message = "inspectdb generated an attribute name which is a python keyword"
|
2013-01-28 09:21:07 +00:00
|
|
|
# Recursive foreign keys should be set to 'self'
|
|
|
|
self.assertIn("parent = models.ForeignKey('self')", output)
|
2012-08-23 19:07:56 +00:00
|
|
|
self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", output, msg=error_message)
|
2012-04-27 06:56:31 +00:00
|
|
|
# As InspectdbPeople model is defined after InspectdbMessage, it should be quoted
|
2012-08-23 19:07:56 +00:00
|
|
|
self.assertIn("from_field = models.ForeignKey('InspectdbPeople', db_column='from_id')",
|
|
|
|
output)
|
2012-02-05 07:51:37 +00:00
|
|
|
self.assertIn("people_pk = models.ForeignKey(InspectdbPeople, primary_key=True)",
|
2012-08-23 19:07:56 +00:00
|
|
|
output)
|
2012-02-05 07:51:37 +00:00
|
|
|
self.assertIn("people_unique = models.ForeignKey(InspectdbPeople, unique=True)",
|
2012-08-23 19:07:56 +00:00
|
|
|
output)
|
2012-02-11 20:53:48 +00:00
|
|
|
|
|
|
|
def test_digits_column_name_introspection(self):
|
|
|
|
"""Introspection of column names consist/start with digits (#16536/#17676)"""
|
|
|
|
out = StringIO()
|
2012-06-01 23:58:53 +00:00
|
|
|
# Lets limit the introspection to tables created for models of this
|
|
|
|
# application
|
|
|
|
call_command('inspectdb',
|
|
|
|
table_name_filter=lambda tn:tn.startswith('inspectdb_'),
|
|
|
|
stdout=out)
|
2012-08-23 19:07:56 +00:00
|
|
|
output = out.getvalue()
|
2012-02-11 20:53:48 +00:00
|
|
|
error_message = "inspectdb generated a model field name which is a number"
|
2012-08-23 19:07:56 +00:00
|
|
|
self.assertNotIn(" 123 = models.CharField", output, msg=error_message)
|
|
|
|
self.assertIn("number_123 = models.CharField", output)
|
2012-02-11 20:53:48 +00:00
|
|
|
|
|
|
|
error_message = "inspectdb generated a model field name which starts with a digit"
|
2012-08-23 19:07:56 +00:00
|
|
|
self.assertNotIn(" 4extra = models.CharField", output, msg=error_message)
|
|
|
|
self.assertIn("number_4extra = models.CharField", output)
|
|
|
|
|
|
|
|
self.assertNotIn(" 45extra = models.CharField", output, msg=error_message)
|
|
|
|
self.assertIn("number_45extra = models.CharField", output)
|
2012-02-11 20:53:48 +00:00
|
|
|
|
2012-08-23 20:50:25 +00:00
|
|
|
def test_special_column_name_introspection(self):
|
2012-10-27 01:51:14 +00:00
|
|
|
"""
|
|
|
|
Introspection of column names containing special characters,
|
|
|
|
unsuitable for Python identifiers
|
2012-08-23 20:50:25 +00:00
|
|
|
"""
|
2012-08-23 19:07:56 +00:00
|
|
|
out = StringIO()
|
|
|
|
call_command('inspectdb', stdout=out)
|
|
|
|
output = out.getvalue()
|
2012-10-27 01:51:14 +00:00
|
|
|
base_name = 'Field' if connection.vendor != 'oracle' else 'field'
|
2012-08-23 19:07:56 +00:00
|
|
|
self.assertIn("field = models.IntegerField()", output)
|
2012-10-27 01:51:14 +00:00
|
|
|
self.assertIn("field_field = models.IntegerField(db_column='%s_')" % base_name, output)
|
|
|
|
self.assertIn("field_field_0 = models.IntegerField(db_column='%s__')" % base_name, output)
|
2012-08-23 19:07:56 +00:00
|
|
|
self.assertIn("field_field_1 = models.IntegerField(db_column='__field')", output)
|
2012-08-23 20:50:25 +00:00
|
|
|
self.assertIn("prc_x = models.IntegerField(db_column='prc(%) x')", output)
|