2013-04-02 01:51:53 +08:00
|
|
|
# -*- encoding: utf-8 -*-
|
2012-08-24 03:07:56 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-02-01 03:34:36 +08:00
|
|
|
import re
|
2014-05-08 04:14:39 +08:00
|
|
|
from unittest import skipUnless
|
2013-02-01 03:34:36 +08:00
|
|
|
|
2011-01-24 22:58:05 +08:00
|
|
|
from django.core.management import call_command
|
2012-10-27 09:51:14 +08:00
|
|
|
from django.db import connection
|
2011-02-01 22:42:52 +08:00
|
|
|
from django.test import TestCase, skipUnlessDBFeature
|
2013-04-02 01:51:53 +08:00
|
|
|
from django.utils.six import PY3, StringIO
|
2011-01-24 22:58:05 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2011-01-24 22:58:05 +08:00
|
|
|
class InspectDBTestCase(TestCase):
|
2011-02-01 22:42:52 +08:00
|
|
|
|
2012-06-02 07:58:53 +08: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',
|
2013-10-27 03:15:03 +08:00
|
|
|
table_name_filter=lambda tn: tn.startswith('inspectdb_'),
|
2012-06-02 07:58:53 +08:00
|
|
|
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)
|
|
|
|
|
2014-01-30 01:18:32 +08:00
|
|
|
def make_field_type_asserter(self):
|
|
|
|
"""Call inspectdb and return a function to validate a field type in its output"""
|
2013-02-01 03:34:36 +08:00
|
|
|
out = StringIO()
|
|
|
|
call_command('inspectdb',
|
2013-10-25 01:30:03 +08:00
|
|
|
table_name_filter=lambda tn: tn.startswith('inspectdb_columntypes'),
|
2013-02-01 03:34:36 +08:00
|
|
|
stdout=out)
|
|
|
|
output = out.getvalue()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-02-01 03:34:36 +08:00
|
|
|
def assertFieldType(name, definition):
|
|
|
|
out_def = re.search(r'^\s*%s = (models.*)$' % name, output, re.MULTILINE).groups()[0]
|
|
|
|
self.assertEqual(definition, out_def)
|
|
|
|
|
2014-01-30 01:18:32 +08:00
|
|
|
return assertFieldType
|
|
|
|
|
|
|
|
def test_field_types(self):
|
|
|
|
"""Test introspection of various Django field types"""
|
|
|
|
assertFieldType = self.make_field_type_asserter()
|
|
|
|
|
2014-05-08 04:14:39 +08:00
|
|
|
# Inspecting Oracle DB doesn't produce correct results (#19884):
|
|
|
|
# - it gets max_length wrong: it returns a number of bytes.
|
|
|
|
# - it reports fields as blank=True when they aren't.
|
|
|
|
if (connection.features.can_introspect_max_length and
|
|
|
|
not connection.features.interprets_empty_strings_as_nulls):
|
|
|
|
assertFieldType('char_field', "models.CharField(max_length=10)")
|
|
|
|
assertFieldType('comma_separated_int_field', "models.CharField(max_length=99)")
|
2014-01-30 01:18:32 +08:00
|
|
|
assertFieldType('date_field', "models.DateField()")
|
|
|
|
assertFieldType('date_time_field', "models.DateTimeField()")
|
2014-05-08 04:14:39 +08:00
|
|
|
if (connection.features.can_introspect_max_length and
|
|
|
|
not connection.features.interprets_empty_strings_as_nulls):
|
2014-07-01 02:34:45 +08:00
|
|
|
assertFieldType('email_field', "models.CharField(max_length=254)")
|
2014-05-08 04:14:39 +08:00
|
|
|
assertFieldType('file_field', "models.CharField(max_length=100)")
|
|
|
|
assertFieldType('file_path_field', "models.CharField(max_length=100)")
|
|
|
|
if connection.features.can_introspect_ip_address_field:
|
2014-01-30 01:18:32 +08:00
|
|
|
assertFieldType('ip_address_field', "models.GenericIPAddressField()")
|
|
|
|
assertFieldType('gen_ip_adress_field', "models.GenericIPAddressField()")
|
2014-05-08 04:14:39 +08:00
|
|
|
elif (connection.features.can_introspect_max_length and
|
|
|
|
not connection.features.interprets_empty_strings_as_nulls):
|
2014-01-30 01:18:32 +08:00
|
|
|
assertFieldType('ip_address_field', "models.CharField(max_length=15)")
|
|
|
|
assertFieldType('gen_ip_adress_field', "models.CharField(max_length=39)")
|
2014-05-08 04:14:39 +08:00
|
|
|
if (connection.features.can_introspect_max_length and
|
|
|
|
not connection.features.interprets_empty_strings_as_nulls):
|
|
|
|
assertFieldType('slug_field', "models.CharField(max_length=50)")
|
|
|
|
if not connection.features.interprets_empty_strings_as_nulls:
|
|
|
|
assertFieldType('text_field', "models.TextField()")
|
|
|
|
if connection.features.can_introspect_time_field:
|
|
|
|
assertFieldType('time_field', "models.TimeField()")
|
|
|
|
if (connection.features.can_introspect_max_length and
|
|
|
|
not connection.features.interprets_empty_strings_as_nulls):
|
|
|
|
assertFieldType('url_field', "models.CharField(max_length=200)")
|
2014-01-30 01:18:32 +08:00
|
|
|
|
|
|
|
def test_number_field_types(self):
|
|
|
|
"""Test introspection of various Django field types"""
|
|
|
|
assertFieldType = self.make_field_type_asserter()
|
|
|
|
|
2013-09-12 22:03:29 +08:00
|
|
|
if not connection.features.can_introspect_autofield:
|
2014-01-16 07:01:30 +08:00
|
|
|
assertFieldType('id', "models.IntegerField(primary_key=True) # AutoField?")
|
2014-05-08 04:14:39 +08:00
|
|
|
|
|
|
|
if connection.features.can_introspect_big_integer_field:
|
|
|
|
assertFieldType('big_int_field', "models.BigIntegerField()")
|
2013-02-01 03:34:36 +08:00
|
|
|
else:
|
2014-05-08 04:14:39 +08:00
|
|
|
assertFieldType('big_int_field', "models.IntegerField()")
|
|
|
|
|
2014-09-26 14:43:50 +08:00
|
|
|
if connection.features.introspected_boolean_field_type == 'BooleanField':
|
2013-02-01 09:00:25 +08:00
|
|
|
assertFieldType('bool_field', "models.BooleanField()")
|
2014-06-06 06:26:40 +08:00
|
|
|
if connection.features.can_introspect_null:
|
|
|
|
assertFieldType('null_bool_field', "models.NullBooleanField()")
|
|
|
|
else:
|
|
|
|
assertFieldType('null_bool_field', "models.BooleanField()")
|
2014-05-08 04:14:39 +08:00
|
|
|
else:
|
2014-09-26 14:43:50 +08:00
|
|
|
field_type = connection.features.introspected_boolean_field_type
|
|
|
|
assertFieldType('bool_field', "models.{}()".format(field_type))
|
2014-06-06 06:26:40 +08:00
|
|
|
if connection.features.can_introspect_null:
|
2014-09-26 14:43:50 +08:00
|
|
|
assertFieldType('null_bool_field', "models.{}(blank=True, null=True)".format(field_type))
|
2014-06-06 06:26:40 +08:00
|
|
|
else:
|
2014-09-26 14:43:50 +08:00
|
|
|
assertFieldType('null_bool_field', "models.{}()".format(field_type))
|
2014-05-08 04:14:39 +08:00
|
|
|
|
2014-06-06 06:29:24 +08:00
|
|
|
if connection.features.can_introspect_decimal_field:
|
|
|
|
assertFieldType('decimal_field', "models.DecimalField(max_digits=6, decimal_places=1)")
|
|
|
|
else: # Guessed arguments on SQLite, see #5014
|
2014-01-16 07:01:30 +08:00
|
|
|
assertFieldType('decimal_field', "models.DecimalField(max_digits=10, decimal_places=5) "
|
2014-01-30 01:18:32 +08:00
|
|
|
"# max_digits and decimal_places have been guessed, "
|
|
|
|
"as this database handles decimal fields as float")
|
2014-05-08 04:14:39 +08:00
|
|
|
|
2013-02-01 09:00:25 +08:00
|
|
|
assertFieldType('float_field', "models.FloatField()")
|
2014-05-08 04:14:39 +08:00
|
|
|
|
2013-02-01 09:00:25 +08:00
|
|
|
assertFieldType('int_field', "models.IntegerField()")
|
2014-05-08 04:14:39 +08:00
|
|
|
|
|
|
|
if connection.features.can_introspect_positive_integer_field:
|
2013-02-01 09:00:25 +08:00
|
|
|
assertFieldType('pos_int_field', "models.PositiveIntegerField()")
|
2013-02-01 03:34:36 +08:00
|
|
|
else:
|
2013-02-01 09:00:25 +08:00
|
|
|
assertFieldType('pos_int_field', "models.IntegerField()")
|
2014-05-08 04:14:39 +08:00
|
|
|
|
|
|
|
if connection.features.can_introspect_positive_integer_field:
|
|
|
|
if connection.features.can_introspect_small_integer_field:
|
|
|
|
assertFieldType('pos_small_int_field', "models.PositiveSmallIntegerField()")
|
|
|
|
else:
|
|
|
|
assertFieldType('pos_small_int_field', "models.PositiveIntegerField()")
|
|
|
|
else:
|
|
|
|
if connection.features.can_introspect_small_integer_field:
|
2013-02-01 09:00:25 +08:00
|
|
|
assertFieldType('pos_small_int_field', "models.SmallIntegerField()")
|
2013-02-01 03:34:36 +08:00
|
|
|
else:
|
2013-02-01 09:00:25 +08:00
|
|
|
assertFieldType('pos_small_int_field', "models.IntegerField()")
|
2014-05-08 04:14:39 +08:00
|
|
|
|
|
|
|
if connection.features.can_introspect_small_integer_field:
|
2013-02-01 09:00:25 +08:00
|
|
|
assertFieldType('small_int_field', "models.SmallIntegerField()")
|
2013-02-01 03:34:36 +08:00
|
|
|
else:
|
2013-02-01 09:00:25 +08:00
|
|
|
assertFieldType('small_int_field', "models.IntegerField()")
|
2013-02-01 03:34:36 +08:00
|
|
|
|
2011-02-01 22:42:52 +08:00
|
|
|
@skipUnlessDBFeature('can_introspect_foreign_keys')
|
2011-01-24 22:58:05 +08:00
|
|
|
def test_attribute_name_not_python_keyword(self):
|
|
|
|
out = StringIO()
|
2012-06-02 07:58:53 +08:00
|
|
|
# Lets limit the introspection to tables created for models of this
|
|
|
|
# application
|
|
|
|
call_command('inspectdb',
|
2013-10-27 03:15:03 +08:00
|
|
|
table_name_filter=lambda tn: tn.startswith('inspectdb_'),
|
2012-06-02 07:58:53 +08:00
|
|
|
stdout=out)
|
2012-08-24 03:07:56 +08:00
|
|
|
output = out.getvalue()
|
2011-01-24 22:58:05 +08:00
|
|
|
error_message = "inspectdb generated an attribute name which is a python keyword"
|
2013-01-28 17:21:07 +08:00
|
|
|
# Recursive foreign keys should be set to 'self'
|
|
|
|
self.assertIn("parent = models.ForeignKey('self')", output)
|
2012-08-24 03:07:56 +08:00
|
|
|
self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", output, msg=error_message)
|
2012-04-27 14:56:31 +08:00
|
|
|
# As InspectdbPeople model is defined after InspectdbMessage, it should be quoted
|
2012-08-24 03:07:56 +08:00
|
|
|
self.assertIn("from_field = models.ForeignKey('InspectdbPeople', db_column='from_id')",
|
2014-01-30 01:18:32 +08:00
|
|
|
output)
|
2012-02-05 15:51:37 +08:00
|
|
|
self.assertIn("people_pk = models.ForeignKey(InspectdbPeople, primary_key=True)",
|
2014-01-30 01:18:32 +08:00
|
|
|
output)
|
2012-02-05 15:51:37 +08:00
|
|
|
self.assertIn("people_unique = models.ForeignKey(InspectdbPeople, unique=True)",
|
2014-01-30 01:18:32 +08:00
|
|
|
output)
|
2012-02-12 04:53:48 +08:00
|
|
|
|
|
|
|
def test_digits_column_name_introspection(self):
|
|
|
|
"""Introspection of column names consist/start with digits (#16536/#17676)"""
|
|
|
|
out = StringIO()
|
2012-06-02 07:58:53 +08:00
|
|
|
# Lets limit the introspection to tables created for models of this
|
|
|
|
# application
|
|
|
|
call_command('inspectdb',
|
2013-10-27 03:15:03 +08:00
|
|
|
table_name_filter=lambda tn: tn.startswith('inspectdb_'),
|
2012-06-02 07:58:53 +08:00
|
|
|
stdout=out)
|
2012-08-24 03:07:56 +08:00
|
|
|
output = out.getvalue()
|
2012-02-12 04:53:48 +08:00
|
|
|
error_message = "inspectdb generated a model field name which is a number"
|
2012-08-24 03:07:56 +08:00
|
|
|
self.assertNotIn(" 123 = models.CharField", output, msg=error_message)
|
|
|
|
self.assertIn("number_123 = models.CharField", output)
|
2012-02-12 04:53:48 +08:00
|
|
|
|
|
|
|
error_message = "inspectdb generated a model field name which starts with a digit"
|
2012-08-24 03:07:56 +08: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-12 04:53:48 +08:00
|
|
|
|
2012-08-24 04:50:25 +08:00
|
|
|
def test_special_column_name_introspection(self):
|
2012-10-27 09:51:14 +08:00
|
|
|
"""
|
|
|
|
Introspection of column names containing special characters,
|
|
|
|
unsuitable for Python identifiers
|
2012-08-24 04:50:25 +08:00
|
|
|
"""
|
2012-08-24 03:07:56 +08:00
|
|
|
out = StringIO()
|
2014-07-06 02:48:57 +08:00
|
|
|
call_command('inspectdb',
|
|
|
|
table_name_filter=lambda tn: tn.startswith('inspectdb_'),
|
|
|
|
stdout=out)
|
2012-08-24 03:07:56 +08:00
|
|
|
output = out.getvalue()
|
2014-05-29 02:16:24 +08:00
|
|
|
base_name = 'Field' if not connection.features.uppercases_column_names else 'field'
|
2012-08-24 03:07:56 +08:00
|
|
|
self.assertIn("field = models.IntegerField()", output)
|
2012-10-27 09:51:14 +08: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-24 03:07:56 +08:00
|
|
|
self.assertIn("field_field_1 = models.IntegerField(db_column='__field')", output)
|
2012-08-24 04:50:25 +08:00
|
|
|
self.assertIn("prc_x = models.IntegerField(db_column='prc(%) x')", output)
|
2013-04-02 01:51:53 +08:00
|
|
|
if PY3:
|
2014-03-02 22:25:53 +08:00
|
|
|
# Python 3 allows non-ASCII identifiers
|
2013-04-02 01:51:53 +08:00
|
|
|
self.assertIn("tamaño = models.IntegerField()", output)
|
|
|
|
else:
|
|
|
|
self.assertIn("tama_o = models.IntegerField(db_column='tama\\xf1o')", output)
|
2013-02-03 08:08:45 +08:00
|
|
|
|
2014-07-06 02:48:57 +08:00
|
|
|
def test_table_name_introspection(self):
|
|
|
|
"""
|
|
|
|
Introspection of table names containing special characters,
|
|
|
|
unsuitable for Python identifiers
|
|
|
|
"""
|
|
|
|
out = StringIO()
|
|
|
|
call_command('inspectdb',
|
|
|
|
table_name_filter=lambda tn: tn.startswith('inspectdb_'),
|
|
|
|
stdout=out)
|
|
|
|
output = out.getvalue()
|
|
|
|
self.assertIn("class InspectdbSpecialTableName(models.Model):", output)
|
|
|
|
|
2013-02-03 08:08:45 +08:00
|
|
|
def test_managed_models(self):
|
|
|
|
"""Test that by default the command generates models with `Meta.managed = False` (#14305)"""
|
|
|
|
out = StringIO()
|
|
|
|
call_command('inspectdb',
|
2013-10-27 03:15:03 +08:00
|
|
|
table_name_filter=lambda tn: tn.startswith('inspectdb_columntypes'),
|
2013-02-03 08:08:45 +08:00
|
|
|
stdout=out)
|
|
|
|
output = out.getvalue()
|
|
|
|
self.longMessage = False
|
|
|
|
self.assertIn(" managed = False", output, msg='inspectdb should generate unmanaged models.')
|
2013-09-11 23:12:56 +08:00
|
|
|
|
2014-07-15 04:42:05 +08:00
|
|
|
def test_unique_together_meta(self):
|
|
|
|
out = StringIO()
|
|
|
|
call_command('inspectdb',
|
|
|
|
table_name_filter=lambda tn: tn.startswith('inspectdb_uniquetogether'),
|
|
|
|
stdout=out)
|
|
|
|
output = out.getvalue()
|
|
|
|
self.assertIn(" unique_together = (('field1', 'field2'),)", output, msg='inspectdb should generate unique_together.')
|
|
|
|
|
2013-09-11 23:12:56 +08:00
|
|
|
@skipUnless(connection.vendor == 'sqlite',
|
2014-01-30 01:18:32 +08:00
|
|
|
"Only patched sqlite's DatabaseIntrospection.data_types_reverse for this test")
|
2013-09-11 23:12:56 +08:00
|
|
|
def test_custom_fields(self):
|
|
|
|
"""
|
|
|
|
Introspection of columns with a custom field (#21090)
|
|
|
|
"""
|
|
|
|
out = StringIO()
|
|
|
|
orig_data_types_reverse = connection.introspection.data_types_reverse
|
|
|
|
try:
|
|
|
|
connection.introspection.data_types_reverse = {
|
|
|
|
'text': 'myfields.TextField',
|
|
|
|
'bigint': 'BigIntegerField',
|
|
|
|
}
|
|
|
|
call_command('inspectdb',
|
|
|
|
table_name_filter=lambda tn: tn.startswith('inspectdb_columntypes'),
|
|
|
|
stdout=out)
|
|
|
|
output = out.getvalue()
|
|
|
|
self.assertIn("text_field = myfields.TextField()", output)
|
|
|
|
self.assertIn("big_int_field = models.BigIntegerField()", output)
|
|
|
|
finally:
|
|
|
|
connection.introspection.data_types_reverse = orig_data_types_reverse
|