def test_custom_fields(self): from frappe.utils.testutils import add_custom_field, clear_custom_fields add_custom_field('Event', 'test_ref_doc', 'Link', 'DocType') add_custom_field('Event', 'test_ref_name', 'Dynamic Link', 'test_ref_doc') unsub = frappe.get_doc({ 'doctype': 'Email Unsubscribe', 'email': '*****@*****.**', 'global_unsubscribe': 1 }).insert() event = frappe.get_doc({ 'doctype': 'Event', 'subject': 'test-for-delete-2', 'starts_on': '2014-01-01', 'event_type': 'Public', 'test_ref_doc': unsub.doctype, 'test_ref_name': unsub.name }).insert() self.assertRaises(frappe.LinkExistsError, unsub.delete) event.test_ref_doc = None event.test_ref_name = None event.save() unsub.delete() clear_custom_fields('Event')
def test_points_based_on_max_points(self): frappe.set_user('*****@*****.**') # here multiplier is high # let see if points get capped to max_point limit multiplier_value = 15 max_points = 50 add_custom_field('ToDo', 'multiplier', 'Float') todo_point_rule = create_energy_point_rule_for_todo( 'multiplier', max_points=max_points) energy_point_of_user = get_points('*****@*****.**') created_todo = create_a_todo() created_todo.status = 'Closed' created_todo.multiplier = multiplier_value created_todo.save() points_after_closing_todo = get_points('*****@*****.**') # test max_points cap self.assertNotEquals( points_after_closing_todo, energy_point_of_user + round(todo_point_rule.points * multiplier_value)) self.assertEqual(points_after_closing_todo, energy_point_of_user + max_points) clear_custom_fields('ToDo')
def test_points_based_on_max_points(self): frappe.set_user("*****@*****.**") # here multiplier is high # let see if points get capped to max_point limit multiplier_value = 15 max_points = 50 add_custom_field("ToDo", "multiplier", "Float") todo_point_rule = create_energy_point_rule_for_todo("multiplier", max_points=max_points) energy_point_of_user = get_points("*****@*****.**") created_todo = create_a_todo() created_todo.status = "Closed" created_todo.multiplier = multiplier_value created_todo.save() points_after_closing_todo = get_points("*****@*****.**") # test max_points cap self.assertNotEquals( points_after_closing_todo, energy_point_of_user + round(todo_point_rule.points * multiplier_value), ) self.assertEqual(points_after_closing_todo, energy_point_of_user + max_points) clear_custom_fields("ToDo")
def test_get_single_value(self): #setup values_dict = { "Float": 1.5, "Int": 1, "Percent": 55.5, "Currency": 12.5, "Data": "Test", "Date": datetime.datetime.now().date(), "Datetime": datetime.datetime.now(), "Time": datetime.timedelta(hours=9, minutes=45, seconds=10) } test_inputs = [{ "fieldtype": fieldtype, "value": value} for fieldtype, value in values_dict.items()] for fieldtype in values_dict.keys(): create_custom_field("Print Settings", { "fieldname": f"test_{fieldtype.lower()}", "label": f"Test {fieldtype}", "fieldtype": fieldtype, }) #test for inp in test_inputs: fieldname = f"test_{inp['fieldtype'].lower()}" frappe.db.set_value("Print Settings", "Print Settings", fieldname, inp["value"]) self.assertEqual(frappe.db.get_single_value("Print Settings", fieldname), inp["value"]) #teardown clear_custom_fields("Print Settings")
def test_custom_fields(self): from frappe.utils.testutils import add_custom_field, clear_custom_fields add_custom_field('Event', 'test_ref_doc', 'Link', 'DocType') add_custom_field('Event', 'test_ref_name', 'Dynamic Link', 'test_ref_doc') unsub = frappe.get_doc({ 'doctype': 'Email Unsubscribe', 'email': '*****@*****.**', 'global_unsubscribe': 1 }).insert() event = frappe.get_doc({ 'doctype': 'Event', 'subject':'test-for-delete-2', 'starts_on': '2014-01-01', 'event_type': 'Public', 'test_ref_doc': unsub.doctype, 'test_ref_name': unsub.name }).insert() self.assertRaises(frappe.LinkExistsError, unsub.delete) event.test_ref_doc = None event.test_ref_name = None event.save() unsub.delete() clear_custom_fields('Event')
def test_extract_tables(self): db_query = DatabaseQuery("DocType") add_custom_field("DocType", 'test_tab_field', 'Data') db_query.fields = [ "tabNote.creation", "test_tab_field", "tabDocType.test_tab_field" ] db_query.extract_tables() self.assertIn("`tabNote`", db_query.tables) self.assertIn("`tabDocType`", db_query.tables) self.assertNotIn("test_tab_field", db_query.tables) clear_custom_fields("DocType")
def test_default_of_dependent_field(self): add_custom_field('ToDo', 'parent_field', 'Data') add_custom_field('ToDo', 'dependent_field', 'Data', default='Some Data', depends_on='parent_field') add_custom_field('ToDo', 'independent_field', 'Data', default='Some Data') doc = frappe.new_doc('ToDo') self.assertFalse(doc.get('dependent_field')) self.assertEqual(doc.get('independent_field'), 'Some Data') clear_custom_fields('ToDo')
def test_points_based_on_multiplier_field(self): frappe.set_user('*****@*****.**') add_custom_field('ToDo', 'multiplier', 'Float') multiplier_value = 0.51 todo_point_rule = create_energy_point_rule_for_todo('multiplier') energy_point_of_user = get_points('*****@*****.**') created_todo = create_a_todo() points_after_closing_todo = get_points('*****@*****.**') created_todo.status = 'Closed' created_todo.multiplier = multiplier_value created_todo.save() points_after_closing_todo = get_points('*****@*****.**') self.assertEquals(points_after_closing_todo, energy_point_of_user + round(todo_point_rule.points * multiplier_value)) clear_custom_fields('ToDo')
def add_child_table_to_blog_post(): child_table = frappe.get_doc({ 'doctype': 'DocType', 'istable': 1, 'custom': 1, 'name': 'Test Child', 'module': 'Custom', 'autoname': 'Prompt', 'fields': [{ 'fieldname': 'test_field', 'fieldtype': 'Data', 'permlevel': 1 }], }) child_table.insert(ignore_permissions=True, ignore_if_duplicate=True) clear_custom_fields('Blog Post') add_custom_field('Blog Post', 'child_table', 'Table', child_table.name)
def test_points_based_on_multiplier_field(self): frappe.set_user("*****@*****.**") add_custom_field("ToDo", "multiplier", "Float") multiplier_value = 0.51 todo_point_rule = create_energy_point_rule_for_todo("multiplier") energy_point_of_user = get_points("*****@*****.**") created_todo = create_a_todo() created_todo.status = "Closed" created_todo.multiplier = multiplier_value created_todo.save() points_after_closing_todo = get_points("*****@*****.**") self.assertEqual( points_after_closing_todo, energy_point_of_user + round(todo_point_rule.points * multiplier_value), ) clear_custom_fields("ToDo")
def add_child_table_to_blog_post(): child_table = frappe.get_doc({ "doctype": "DocType", "istable": 1, "custom": 1, "name": "Test Child", "module": "Custom", "autoname": "Prompt", "fields": [{ "fieldname": "test_field", "fieldtype": "Data", "permlevel": 1 }], }) child_table.insert(ignore_permissions=True, ignore_if_duplicate=True) clear_custom_fields("Blog Post") add_custom_field("Blog Post", "child_table", "Table", child_table.name)
def test_db_keywords_as_fields(self): """Tests if DB keywords work as docfield names. If they're wrapped with grave accents.""" # Using random.choices, picked out a list of 40 keywords for testing all_keywords = { "mariadb": [ "CHARACTER", "DELAYED", "LINES", "EXISTS", "YEAR_MONTH", "LOCALTIME", "BOTH", "MEDIUMINT", "LEFT", "BINARY", "DEFAULT", "KILL", "WRITE", "SQL_SMALL_RESULT", "CURRENT_TIME", "CROSS", "INHERITS", "SELECT", "TABLE", "ALTER", "CURRENT_TIMESTAMP", "XOR", "CASE", "ALL", "WHERE", "INT", "TO", "SOME", "DAY_MINUTE", "ERRORS", "OPTIMIZE", "REPLACE", "HIGH_PRIORITY", "VARBINARY", "HELP", "IS", "CHAR", "DESCRIBE", "KEY" ], "postgres": [ "WORK", "LANCOMPILER", "REAL", "HAVING", "REPEATABLE", "DATA", "USING", "BIT", "DEALLOCATE", "SERIALIZABLE", "CURSOR", "INHERITS", "ARRAY", "TRUE", "IGNORE", "PARAMETER_MODE", "ROW", "CHECKPOINT", "SHOW", "BY", "SIZE", "SCALE", "UNENCRYPTED", "WITH", "AND", "CONVERT", "FIRST", "SCOPE", "WRITE", "INTERVAL", "CHARACTER_SET_SCHEMA", "ADD", "SCROLL", "NULL", "WHEN", "TRANSACTION_ACTIVE", "INT", "FORTRAN", "STABLE" ] } created_docs = [] # edit by rushabh: added [:1] # don't run every keyword! - if one works, they all do fields = all_keywords[frappe.conf.db_type][:1] test_doctype = "ToDo" def add_custom_field(field): create_custom_field( test_doctype, { "fieldname": field.lower(), "label": field.title(), "fieldtype": 'Data', }) # Create custom fields for test_doctype for field in fields: add_custom_field(field) # Create documents under that doctype and query them via ORM for _ in range(10): docfields = {key.lower(): random_string(10) for key in fields} doc = frappe.get_doc({ "doctype": test_doctype, "description": random_string(20), **docfields }) doc.insert() created_docs.append(doc.name) random_field = choice(fields).lower() random_doc = choice(created_docs) random_value = random_string(20) # Testing read self.assertEqual( list(frappe.get_all("ToDo", fields=[random_field], limit=1)[0])[0], random_field) self.assertEqual( list( frappe.get_all("ToDo", fields=["`{0}` as total".format(random_field)], limit=1)[0])[0], "total") # Testing read for distinct and sql functions self.assertEqual( list( frappe.get_all( "ToDo", fields=["`{0}` as total".format(random_field)], distinct=True, limit=1, )[0])[0], "total") self.assertEqual( list( frappe.get_all( "ToDo", fields=["`{0}`".format(random_field)], distinct=True, limit=1, )[0])[0], random_field) self.assertEqual( list( frappe.get_all("ToDo", fields=["count(`{0}`)".format(random_field)], limit=1)[0])[0], "count" if frappe.conf.db_type == "postgres" else "count(`{0}`)".format(random_field)) # Testing update frappe.db.set_value(test_doctype, random_doc, random_field, random_value) self.assertEqual( frappe.db.get_value(test_doctype, random_doc, random_field), random_value) # Cleanup - delete records and remove custom fields for doc in created_docs: frappe.delete_doc(test_doctype, doc) clear_custom_fields(test_doctype)