Fixed #21079 -- Further normalized table names in inspectdb

Thanks Michael Manfre for the report and Tim Graham for the review.
This commit is contained in:
Claude Paroz 2014-07-05 20:48:57 +02:00
parent 2572c07cc6
commit b144bfb5ce
3 changed files with 20 additions and 3 deletions

View File

@ -32,7 +32,7 @@ class Command(BaseCommand):
# 'table_name_filter' is a stealth option
table_name_filter = options.get('table_name_filter')
table2model = lambda table_name: table_name.title().replace('_', '').replace(' ', '').replace('-', '')
table2model = lambda table_name: re.sub(r'[^a-zA-Z0-9]', '', table_name.title())
strip_prefix = lambda s: s[1:] if s.startswith("u'") else s
with connection.cursor() as cursor:

View File

@ -30,7 +30,7 @@ class DigitsInColumnName(models.Model):
leading_digits = models.CharField(max_length=11, db_column='45extra')
class SpecialColumnName(models.Model):
class SpecialName(models.Model):
field = models.IntegerField(db_column='field')
# Underscores
field_field_0 = models.IntegerField(db_column='Field_')
@ -40,6 +40,9 @@ class SpecialColumnName(models.Model):
prc_x = models.IntegerField(db_column='prc(%) x')
non_ascii = models.IntegerField(db_column='tamaño')
class Meta:
db_table = "inspectdb_special.table name"
class ColumnTypes(models.Model):
id = models.AutoField(primary_key=True)

View File

@ -179,7 +179,9 @@ class InspectDBTestCase(TestCase):
unsuitable for Python identifiers
"""
out = StringIO()
call_command('inspectdb', stdout=out)
call_command('inspectdb',
table_name_filter=lambda tn: tn.startswith('inspectdb_'),
stdout=out)
output = out.getvalue()
base_name = 'Field' if not connection.features.uppercases_column_names else 'field'
self.assertIn("field = models.IntegerField()", output)
@ -193,6 +195,18 @@ class InspectDBTestCase(TestCase):
else:
self.assertIn("tama_o = models.IntegerField(db_column='tama\\xf1o')", output)
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)
def test_managed_models(self):
"""Test that by default the command generates models with `Meta.managed = False` (#14305)"""
out = StringIO()