diff options
| author | Claude Paroz <claude@2xlibre.net> | 2012-08-23 21:07:56 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2012-08-23 21:46:57 +0200 |
| commit | 395c6083af93cc37c7d16ef4db451091841cefdc (patch) | |
| tree | 6e0971b5ae1d9846db98c49a3faf0a6879ebfd97 /tests | |
| parent | 10d32072afa1e1d4391108191800ce7007a63f2e (diff) | |
Fixed #12460 -- Improved inspectdb handling of special field names
Thanks mihail lukin for the report and elijahr and kgibula for their
work on the patch.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/regressiontests/inspectdb/models.py | 6 | ||||
| -rw-r--r-- | tests/regressiontests/inspectdb/tests.py | 37 |
2 files changed, 31 insertions, 12 deletions
diff --git a/tests/regressiontests/inspectdb/models.py b/tests/regressiontests/inspectdb/models.py index 9f815855b6..352053aafe 100644 --- a/tests/regressiontests/inspectdb/models.py +++ b/tests/regressiontests/inspectdb/models.py @@ -19,3 +19,9 @@ class DigitsInColumnName(models.Model): all_digits = models.CharField(max_length=11, db_column='123') leading_digit = models.CharField(max_length=11, db_column='4extra') leading_digits = models.CharField(max_length=11, db_column='45extra') + +class UnderscoresInColumnName(models.Model): + field = models.IntegerField(db_column='field') + field_field_0 = models.IntegerField(db_column='Field_') + field_field_1 = models.IntegerField(db_column='Field__') + field_field_2 = models.IntegerField(db_column='__field') diff --git a/tests/regressiontests/inspectdb/tests.py b/tests/regressiontests/inspectdb/tests.py index aae7bc5cc7..b5647e9e38 100644 --- a/tests/regressiontests/inspectdb/tests.py +++ b/tests/regressiontests/inspectdb/tests.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + from django.core.management import call_command from django.test import TestCase, skipUnlessDBFeature from django.utils.six import StringIO @@ -17,7 +19,6 @@ class InspectDBTestCase(TestCase): # 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) - out.close() @skipUnlessDBFeature('can_introspect_foreign_keys') def test_attribute_name_not_python_keyword(self): @@ -27,15 +28,16 @@ class InspectDBTestCase(TestCase): call_command('inspectdb', table_name_filter=lambda tn:tn.startswith('inspectdb_'), stdout=out) + output = out.getvalue() error_message = "inspectdb generated an attribute name which is a python keyword" - self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", out.getvalue(), msg=error_message) + self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", output, msg=error_message) # As InspectdbPeople model is defined after InspectdbMessage, it should be quoted - self.assertIn("from_field = models.ForeignKey('InspectdbPeople')", out.getvalue()) + self.assertIn("from_field = models.ForeignKey('InspectdbPeople', db_column='from_id')", + output) self.assertIn("people_pk = models.ForeignKey(InspectdbPeople, primary_key=True)", - out.getvalue()) + output) self.assertIn("people_unique = models.ForeignKey(InspectdbPeople, unique=True)", - out.getvalue()) - out.close() + output) def test_digits_column_name_introspection(self): """Introspection of column names consist/start with digits (#16536/#17676)""" @@ -45,13 +47,24 @@ class InspectDBTestCase(TestCase): call_command('inspectdb', table_name_filter=lambda tn:tn.startswith('inspectdb_'), stdout=out) + output = out.getvalue() error_message = "inspectdb generated a model field name which is a number" - self.assertNotIn(" 123 = models.CharField", out.getvalue(), msg=error_message) - self.assertIn("number_123 = models.CharField", out.getvalue()) + self.assertNotIn(" 123 = models.CharField", output, msg=error_message) + self.assertIn("number_123 = models.CharField", output) error_message = "inspectdb generated a model field name which starts with a digit" - self.assertNotIn(" 4extra = models.CharField", out.getvalue(), msg=error_message) - self.assertIn("number_4extra = models.CharField", out.getvalue()) + 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) - self.assertNotIn(" 45extra = models.CharField", out.getvalue(), msg=error_message) - self.assertIn("number_45extra = models.CharField", out.getvalue()) + def test_underscores_column_name_introspection(self): + """Introspection of column names containing underscores (#12460)""" + out = StringIO() + call_command('inspectdb', stdout=out) + output = out.getvalue() + self.assertIn("field = models.IntegerField()", output) + self.assertIn("field_field = models.IntegerField(db_column='Field_')", output) + self.assertIn("field_field_0 = models.IntegerField(db_column='Field__')", output) + self.assertIn("field_field_1 = models.IntegerField(db_column='__field')", output) |
