From b3f9e8e2cf00e85488dfbaef0fd5ba60af80a130 Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Fri, 21 Jan 2022 21:20:47 +0530 Subject: [PATCH] test: Add cases to validate index and unique constraints --- frappe/tests/test_db_update.py | 52 +++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/frappe/tests/test_db_update.py b/frappe/tests/test_db_update.py index 4ae33a2fab..7831ecd5f4 100644 --- a/frappe/tests/test_db_update.py +++ b/frappe/tests/test_db_update.py @@ -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 \ No newline at end of file + 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) \ No newline at end of file