1
from __future__ import unicode_literals
3
from django.core.management import call_command
4
from django.db import connection
5
from django.test import TestCase, skipUnlessDBFeature
6
from django.utils.six import StringIO
9
class InspectDBTestCase(TestCase):
11
def test_stealth_table_name_filter_option(self):
13
# Lets limit the introspection to tables created for models of this
15
call_command('inspectdb',
16
table_name_filter=lambda tn:tn.startswith('inspectdb_'),
18
error_message = "inspectdb has examined a table that should have been filtered out."
19
# contrib.contenttypes is one of the apps always installed when running
20
# the Django test suite, check that one of its tables hasn't been
22
self.assertNotIn("class DjangoContentType(models.Model):", out.getvalue(), msg=error_message)
24
@skipUnlessDBFeature('can_introspect_foreign_keys')
25
def test_attribute_name_not_python_keyword(self):
27
# Lets limit the introspection to tables created for models of this
29
call_command('inspectdb',
30
table_name_filter=lambda tn:tn.startswith('inspectdb_'),
32
output = out.getvalue()
33
error_message = "inspectdb generated an attribute name which is a python keyword"
34
self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", output, msg=error_message)
35
# As InspectdbPeople model is defined after InspectdbMessage, it should be quoted
36
self.assertIn("from_field = models.ForeignKey('InspectdbPeople', db_column='from_id')",
38
self.assertIn("people_pk = models.ForeignKey(InspectdbPeople, primary_key=True)",
40
self.assertIn("people_unique = models.ForeignKey(InspectdbPeople, unique=True)",
43
def test_digits_column_name_introspection(self):
44
"""Introspection of column names consist/start with digits (#16536/#17676)"""
46
# Lets limit the introspection to tables created for models of this
48
call_command('inspectdb',
49
table_name_filter=lambda tn:tn.startswith('inspectdb_'),
51
output = out.getvalue()
52
error_message = "inspectdb generated a model field name which is a number"
53
self.assertNotIn(" 123 = models.CharField", output, msg=error_message)
54
self.assertIn("number_123 = models.CharField", output)
56
error_message = "inspectdb generated a model field name which starts with a digit"
57
self.assertNotIn(" 4extra = models.CharField", output, msg=error_message)
58
self.assertIn("number_4extra = models.CharField", output)
60
self.assertNotIn(" 45extra = models.CharField", output, msg=error_message)
61
self.assertIn("number_45extra = models.CharField", output)
63
def test_special_column_name_introspection(self):
65
Introspection of column names containing special characters,
66
unsuitable for Python identifiers
69
call_command('inspectdb', stdout=out)
70
output = out.getvalue()
71
base_name = 'Field' if connection.vendor != 'oracle' else 'field'
72
self.assertIn("field = models.IntegerField()", output)
73
self.assertIn("field_field = models.IntegerField(db_column='%s_')" % base_name, output)
74
self.assertIn("field_field_0 = models.IntegerField(db_column='%s__')" % base_name, output)
75
self.assertIn("field_field_1 = models.IntegerField(db_column='__field')", output)
76
self.assertIn("prc_x = models.IntegerField(db_column='prc(%) x')", output)