test: Add cases to validate index and unique constraints

This commit is contained in:
Suraj Shetty 2022-01-21 21:20:47 +05:30
parent c0fe37b94e
commit b3f9e8e2cf

View file

@ -34,6 +34,52 @@ class TestDBUpdate(unittest.TestCase):
self.assertEqual(fieldtype, table_column.type)
self.assertIn(cstr(table_column.default) or 'NULL', [cstr(default), "'{}'".format(default)])
def test_index_and_unique_constraints(self):
doctype = "User"
frappe.reload_doctype('User', force=True)
frappe.model.meta.trim_tables('User')
make_property_setter(doctype, 'restrict_ip', 'unique', '1', 'Int')
frappe.db.updatedb(doctype)
restrict_ip_in_table = get_table_column("User", "restrict_ip")
self.assertEqual(restrict_ip_in_table.unique, 1)
make_property_setter(doctype, 'restrict_ip', 'unique', '0', 'Int')
frappe.db.updatedb(doctype)
restrict_ip_in_table = get_table_column("User", "restrict_ip")
self.assertEqual(restrict_ip_in_table.unique, 0)
make_property_setter(doctype, 'restrict_ip', 'search_index', '1', 'Int')
frappe.db.updatedb(doctype)
restrict_ip_in_table = get_table_column("User", "restrict_ip")
self.assertEqual(restrict_ip_in_table.index, 1)
make_property_setter(doctype, 'restrict_ip', 'search_index', '0', 'Int')
frappe.db.updatedb(doctype)
restrict_ip_in_table = get_table_column("User", "restrict_ip")
self.assertEqual(restrict_ip_in_table.index, 0)
make_property_setter(doctype, 'restrict_ip', 'search_index', '1', 'Int')
make_property_setter(doctype, 'restrict_ip', 'unique', '1', 'Int')
frappe.db.updatedb(doctype)
restrict_ip_in_table = get_table_column("User", "restrict_ip")
self.assertEqual(restrict_ip_in_table.index, 1)
self.assertEqual(restrict_ip_in_table.unique, 1)
make_property_setter(doctype, 'restrict_ip', 'search_index', '1', 'Int')
make_property_setter(doctype, 'restrict_ip', 'unique', '0', 'Int')
frappe.db.updatedb(doctype)
restrict_ip_in_table = get_table_column("User", "restrict_ip")
self.assertEqual(restrict_ip_in_table.index, 1)
self.assertEqual(restrict_ip_in_table.unique, 0)
make_property_setter(doctype, 'restrict_ip', 'search_index', '0', 'Int')
make_property_setter(doctype, 'restrict_ip', 'unique', '1', 'Int')
frappe.db.updatedb(doctype)
restrict_ip_in_table = get_table_column("User", "restrict_ip")
self.assertEqual(restrict_ip_in_table.index, 0)
self.assertEqual(restrict_ip_in_table.unique, 1)
def get_fieldtype_from_def(field_def):
fieldtuple = frappe.db.type_map.get(field_def.fieldtype, ('', 0))
fieldtype = fieldtuple[0]
@ -69,4 +115,8 @@ def get_other_fields_meta(meta):
fields = dict(default_fields_map, **optional_fields_map)
field_map = [frappe._dict({'fieldname': field, 'fieldtype': _type, 'length': _length}) for field, (_type, _length) in fields.items()]
return field_map
return field_map
def get_table_column(doctype, fieldname):
table_columns = frappe.db.get_table_columns_description('tab{}'.format(doctype))
return find(table_columns, lambda d: d.get('name') == fieldname)