44
40
return assertFieldType
46
# Inspecting oracle DB doesn't produce correct results, see #19884
47
@expectedFailureOnOracle
48
42
def test_field_types(self):
49
43
"""Test introspection of various Django field types"""
50
44
assertFieldType = self.make_field_type_asserter()
52
assertFieldType('char_field', "models.CharField(max_length=10)")
53
assertFieldType('comma_separated_int_field', "models.CharField(max_length=99)")
46
# Inspecting Oracle DB doesn't produce correct results (#19884):
47
# - it gets max_length wrong: it returns a number of bytes.
48
# - it reports fields as blank=True when they aren't.
49
if (connection.features.can_introspect_max_length and
50
not connection.features.interprets_empty_strings_as_nulls):
51
assertFieldType('char_field', "models.CharField(max_length=10)")
52
assertFieldType('comma_separated_int_field', "models.CharField(max_length=99)")
54
53
assertFieldType('date_field', "models.DateField()")
55
54
assertFieldType('date_time_field', "models.DateTimeField()")
56
assertFieldType('email_field', "models.CharField(max_length=75)")
57
assertFieldType('file_field', "models.CharField(max_length=100)")
58
assertFieldType('file_path_field', "models.CharField(max_length=100)")
59
if connection.vendor == 'postgresql':
60
# Only PostgreSQL has a specific type
55
if (connection.features.can_introspect_max_length and
56
not connection.features.interprets_empty_strings_as_nulls):
57
assertFieldType('email_field', "models.CharField(max_length=75)")
58
assertFieldType('file_field', "models.CharField(max_length=100)")
59
assertFieldType('file_path_field', "models.CharField(max_length=100)")
60
if connection.features.can_introspect_ip_address_field:
61
61
assertFieldType('ip_address_field', "models.GenericIPAddressField()")
62
62
assertFieldType('gen_ip_adress_field', "models.GenericIPAddressField()")
63
elif (connection.features.can_introspect_max_length and
64
not connection.features.interprets_empty_strings_as_nulls):
64
65
assertFieldType('ip_address_field', "models.CharField(max_length=15)")
65
66
assertFieldType('gen_ip_adress_field', "models.CharField(max_length=39)")
66
assertFieldType('slug_field', "models.CharField(max_length=50)")
67
assertFieldType('text_field', "models.TextField()")
68
assertFieldType('time_field', "models.TimeField()")
69
assertFieldType('url_field', "models.CharField(max_length=200)")
67
if (connection.features.can_introspect_max_length and
68
not connection.features.interprets_empty_strings_as_nulls):
69
assertFieldType('slug_field', "models.CharField(max_length=50)")
70
if not connection.features.interprets_empty_strings_as_nulls:
71
assertFieldType('text_field', "models.TextField()")
72
if connection.features.can_introspect_time_field:
73
assertFieldType('time_field', "models.TimeField()")
74
if (connection.features.can_introspect_max_length and
75
not connection.features.interprets_empty_strings_as_nulls):
76
assertFieldType('url_field', "models.CharField(max_length=200)")
71
78
def test_number_field_types(self):
72
79
"""Test introspection of various Django field types"""
73
80
assertFieldType = self.make_field_type_asserter()
75
assertFieldType('id', "models.IntegerField(primary_key=True)")
76
assertFieldType('big_int_field', "models.BigIntegerField()")
77
if connection.vendor == 'mysql':
78
# No native boolean type on MySQL
82
if not connection.features.can_introspect_autofield:
83
assertFieldType('id', "models.IntegerField(primary_key=True) # AutoField?")
85
if connection.features.can_introspect_big_integer_field:
86
assertFieldType('big_int_field', "models.BigIntegerField()")
88
assertFieldType('big_int_field', "models.IntegerField()")
90
if connection.features.can_introspect_boolean_field:
91
assertFieldType('bool_field', "models.BooleanField()")
92
if connection.features.can_introspect_null:
93
assertFieldType('null_bool_field', "models.NullBooleanField()")
95
assertFieldType('null_bool_field', "models.BooleanField()")
79
97
assertFieldType('bool_field', "models.IntegerField()")
80
assertFieldType('null_bool_field', "models.IntegerField(blank=True, null=True)")
82
assertFieldType('bool_field', "models.BooleanField()")
83
assertFieldType('null_bool_field', "models.NullBooleanField()")
84
if connection.vendor == 'sqlite':
85
# Guessed arguments, see #5014
86
assertFieldType('decimal_field', "models.DecimalField(max_digits=10, decimal_places=5) "
87
"# max_digits and decimal_places have been guessed, as this database handles decimal fields as float")
98
if connection.features.can_introspect_null:
99
assertFieldType('null_bool_field', "models.IntegerField(blank=True, null=True)")
101
assertFieldType('null_bool_field', "models.IntegerField()")
103
if connection.features.can_introspect_decimal_field:
89
104
assertFieldType('decimal_field', "models.DecimalField(max_digits=6, decimal_places=1)")
105
else: # Guessed arguments on SQLite, see #5014
106
assertFieldType('decimal_field', "models.DecimalField(max_digits=10, decimal_places=5) "
107
"# max_digits and decimal_places have been guessed, "
108
"as this database handles decimal fields as float")
90
110
assertFieldType('float_field', "models.FloatField()")
91
112
assertFieldType('int_field', "models.IntegerField()")
92
if connection.vendor == 'sqlite':
114
if connection.features.can_introspect_positive_integer_field:
93
115
assertFieldType('pos_int_field', "models.PositiveIntegerField()")
94
assertFieldType('pos_small_int_field', "models.PositiveSmallIntegerField()")
96
# 'unsigned' property undetected on other backends
97
117
assertFieldType('pos_int_field', "models.IntegerField()")
98
if connection.vendor == 'postgresql':
119
if connection.features.can_introspect_positive_integer_field:
120
if connection.features.can_introspect_small_integer_field:
121
assertFieldType('pos_small_int_field', "models.PositiveSmallIntegerField()")
123
assertFieldType('pos_small_int_field', "models.PositiveIntegerField()")
125
if connection.features.can_introspect_small_integer_field:
99
126
assertFieldType('pos_small_int_field', "models.SmallIntegerField()")
101
128
assertFieldType('pos_small_int_field', "models.IntegerField()")
102
if connection.vendor in ('sqlite', 'postgresql'):
130
if connection.features.can_introspect_small_integer_field:
103
131
assertFieldType('small_int_field', "models.SmallIntegerField()")
105
133
assertFieldType('small_int_field', "models.IntegerField()")
169
197
"""Test that by default the command generates models with `Meta.managed = False` (#14305)"""
171
199
call_command('inspectdb',
172
table_name_filter=lambda tn:tn.startswith('inspectdb_columntypes'),
200
table_name_filter=lambda tn: tn.startswith('inspectdb_columntypes'),
174
202
output = out.getvalue()
175
203
self.longMessage = False
176
204
self.assertIn(" managed = False", output, msg='inspectdb should generate unmanaged models.')
206
@skipUnless(connection.vendor == 'sqlite',
207
"Only patched sqlite's DatabaseIntrospection.data_types_reverse for this test")
208
def test_custom_fields(self):
210
Introspection of columns with a custom field (#21090)
213
orig_data_types_reverse = connection.introspection.data_types_reverse
215
connection.introspection.data_types_reverse = {
216
'text': 'myfields.TextField',
217
'bigint': 'BigIntegerField',
219
call_command('inspectdb',
220
table_name_filter=lambda tn: tn.startswith('inspectdb_columntypes'),
222
output = out.getvalue()
223
self.assertIn("text_field = myfields.TextField()", output)
224
self.assertIn("big_int_field = models.BigIntegerField()", output)
226
connection.introspection.data_types_reverse = orig_data_types_reverse