From a1e84d234108c950d2d00a3df0eb70247c154966 Mon Sep 17 00:00:00 2001 From: Mitul David <49085834+MitulDavid@users.noreply.github.com> Date: Tue, 6 Jul 2021 13:28:18 +0530 Subject: [PATCH 1/6] fix: Accurately cast fieldtype in frappe.db.get_single_value() --- frappe/database/database.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/frappe/database/database.py b/frappe/database/database.py index 81e24cc7ad..d113adcdab 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -15,7 +15,7 @@ from frappe import _ from time import time from frappe.utils import now, getdate, cast_fieldtype, get_datetime from frappe.model.utils.link_count import flush_local_link_count -from frappe.utils import cint +from frappe.utils import cint, cast_fieldtype class Database(object): @@ -556,8 +556,7 @@ class Database(object): if not df: frappe.throw(_('Invalid field name: {0}').format(frappe.bold(fieldname)), self.InvalidColumnName) - if df.fieldtype in frappe.model.numeric_fieldtypes: - val = cint(val) + val = cast_fieldtype(df.fieldtype, val) self.value_cache[doctype][fieldname] = val From 64840aac6bcca0e1af5203a0d3e005359ade0f1b Mon Sep 17 00:00:00 2001 From: Mitul David <49085834+MitulDavid@users.noreply.github.com> Date: Tue, 6 Jul 2021 14:13:27 +0530 Subject: [PATCH 2/6] refactor(minor): Delete redundant imports --- frappe/database/database.py | 1 - 1 file changed, 1 deletion(-) diff --git a/frappe/database/database.py b/frappe/database/database.py index d113adcdab..6012e47445 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -15,7 +15,6 @@ from frappe import _ from time import time from frappe.utils import now, getdate, cast_fieldtype, get_datetime from frappe.model.utils.link_count import flush_local_link_count -from frappe.utils import cint, cast_fieldtype class Database(object): From 1e839f0ab1ca0e00a0492a2f67fcb7eab13a6720 Mon Sep 17 00:00:00 2001 From: Mitul David <49085834+MitulDavid@users.noreply.github.com> Date: Fri, 9 Jul 2021 18:36:06 +0530 Subject: [PATCH 3/6] test: Add test for frappe.db.get_single_value --- frappe/tests/test_db.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/frappe/tests/test_db.py b/frappe/tests/test_db.py index a31a898d73..dcc70969a1 100644 --- a/frappe/tests/test_db.py +++ b/frappe/tests/test_db.py @@ -5,6 +5,7 @@ import unittest from random import choice +import datetime import frappe from frappe.custom.doctype.custom_field.custom_field import create_custom_field @@ -45,11 +46,27 @@ class TestDB(unittest.TestCase): frappe.db.escape("香港濟生堂製藥有限公司 - IT".encode("utf-8")) def test_get_single_value(self): - frappe.db.set_value('System Settings', 'System Settings', 'backup_limit', 5) - frappe.db.commit() + #setup + fieldtypes = ['Float', 'Int', 'Percent', 'Currency', 'Data', 'Date', 'Datetime', 'Time'] + values = [1.5, 1, 55.5, 12.5, 'Test', datetime.datetime.now().date(), datetime.datetime.now(), datetime.timedelta(hours=9, minutes=45, seconds=10)] + test_inputs = [{ + 'fieldtype': fieldtypes[i], + 'value': values[i]} for i in range(len(fieldtypes))] + for fieldtype in fieldtypes: + create_custom_field('Print Settings', { + "fieldname": 'test_'+fieldtype.lower(), + "label": 'Test '+fieldtype, + "fieldtype": fieldtype, + }) + + #test + for inp in test_inputs: + fieldname = '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']) - limit = frappe.db.get_single_value('System Settings', 'backup_limit') - self.assertEqual(limit, 5) + #teardown + clear_custom_fields('Print Settings') def test_log_touched_tables(self): frappe.flags.in_migrate = True From 95550861a74eb1d56b72ef7536e111b703b2fa86 Mon Sep 17 00:00:00 2001 From: Mitul David <49085834+MitulDavid@users.noreply.github.com> Date: Fri, 9 Jul 2021 18:48:12 +0530 Subject: [PATCH 4/6] refactor(minor): Fix indentation --- frappe/tests/test_db.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frappe/tests/test_db.py b/frappe/tests/test_db.py index dcc70969a1..920ec0d827 100644 --- a/frappe/tests/test_db.py +++ b/frappe/tests/test_db.py @@ -57,8 +57,8 @@ class TestDB(unittest.TestCase): "fieldname": 'test_'+fieldtype.lower(), "label": 'Test '+fieldtype, "fieldtype": fieldtype, - }) - + }) + #test for inp in test_inputs: fieldname = 'test_'+inp['fieldtype'].lower() From 934fc17a3b0aa10c65cd24e87606c674070c61f7 Mon Sep 17 00:00:00 2001 From: Mitul David <49085834+MitulDavid@users.noreply.github.com> Date: Mon, 12 Jul 2021 15:52:57 +0530 Subject: [PATCH 5/6] refactor: Improve readability, consistency --- frappe/tests/test_db.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/frappe/tests/test_db.py b/frappe/tests/test_db.py index 920ec0d827..fc87c5a336 100644 --- a/frappe/tests/test_db.py +++ b/frappe/tests/test_db.py @@ -47,26 +47,34 @@ class TestDB(unittest.TestCase): def test_get_single_value(self): #setup - fieldtypes = ['Float', 'Int', 'Percent', 'Currency', 'Data', 'Date', 'Datetime', 'Time'] - values = [1.5, 1, 55.5, 12.5, 'Test', datetime.datetime.now().date(), datetime.datetime.now(), datetime.timedelta(hours=9, minutes=45, seconds=10)] + 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': fieldtypes[i], - 'value': values[i]} for i in range(len(fieldtypes))] - for fieldtype in fieldtypes: - create_custom_field('Print Settings', { - "fieldname": 'test_'+fieldtype.lower(), - "label": 'Test '+fieldtype, + "fieldtype": fieldtype, + "value": value} for fieldtype, value in values_dict.items()] + for fieldtype in values_dict.keys(): + create_custom_field("Print Settings", { + "fieldname": "test_{0}".format(fieldtype.lower()), + "label": "Test {0}".format(fieldtype), "fieldtype": fieldtype, }) #test for inp in test_inputs: - fieldname = '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']) + fieldname = "test_{0}".format(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') + clear_custom_fields("Print Settings") def test_log_touched_tables(self): frappe.flags.in_migrate = True From 42585579e6faf3ed285752cc8e408dd59a2e3d90 Mon Sep 17 00:00:00 2001 From: Mitul David <49085834+MitulDavid@users.noreply.github.com> Date: Mon, 12 Jul 2021 19:13:29 +0530 Subject: [PATCH 6/6] refactor: Replace str.format() with f-strings --- frappe/tests/test_db.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/frappe/tests/test_db.py b/frappe/tests/test_db.py index fc87c5a336..04c9a525b1 100644 --- a/frappe/tests/test_db.py +++ b/frappe/tests/test_db.py @@ -62,14 +62,14 @@ class TestDB(unittest.TestCase): "value": value} for fieldtype, value in values_dict.items()] for fieldtype in values_dict.keys(): create_custom_field("Print Settings", { - "fieldname": "test_{0}".format(fieldtype.lower()), - "label": "Test {0}".format(fieldtype), + "fieldname": f"test_{fieldtype.lower()}", + "label": f"Test {fieldtype}", "fieldtype": fieldtype, }) #test for inp in test_inputs: - fieldname = "test_{0}".format(inp["fieldtype"].lower()) + 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"]) @@ -157,29 +157,29 @@ class TestDB(unittest.TestCase): # 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") + self.assertEqual(list(frappe.get_all("ToDo", fields=[f"`{random_field}` as total"], 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)], + fields=[f"`{random_field}` as total"], distinct=True, limit=1, )[0] )[0], "total") self.assertEqual(list( frappe.get_all("ToDo", - fields=["`{0}`".format(random_field)], + fields=[f"`{random_field}`"], distinct=True, limit=1, )[0] )[0], random_field) self.assertEqual(list( frappe.get_all("ToDo", - fields=["count(`{0}`)".format(random_field)], + fields=[f"count(`{random_field}`)"], limit=1 )[0] - )[0], "count" if frappe.conf.db_type == "postgres" else "count(`{0}`)".format(random_field)) + )[0], "count" if frappe.conf.db_type == "postgres" else f"count(`{random_field}`)") # Testing update frappe.db.set_value(test_doctype, random_doc, random_field, random_value)