From a43ad15eab731863548f201292fc73b8524584a3 Mon Sep 17 00:00:00 2001 From: Sagar Sharma Date: Fri, 26 May 2023 13:23:47 +0530 Subject: [PATCH] feat: Truncate QB function --- frappe/query_builder/functions.py | 5 +++++ frappe/tests/test_query_builder.py | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/frappe/query_builder/functions.py b/frappe/query_builder/functions.py index 054e33c31d..aa25fa1215 100644 --- a/frappe/query_builder/functions.py +++ b/frappe/query_builder/functions.py @@ -46,6 +46,11 @@ class Round(Function): super().__init__("ROUND", term, decimal, **kwargs) +class Truncate(Function): + def __init__(self, term, decimal, **kwargs): + super().__init__("TRUNCATE", term, decimal, **kwargs) + + GroupConcat = ImportMapper({db_type_is.MARIADB: GROUP_CONCAT, db_type_is.POSTGRES: STRING_AGG}) Match = ImportMapper({db_type_is.MARIADB: MATCH, db_type_is.POSTGRES: TO_TSVECTOR}) diff --git a/frappe/tests/test_query_builder.py b/frappe/tests/test_query_builder.py index 6d6937038a..e3ca63abf1 100644 --- a/frappe/tests/test_query_builder.py +++ b/frappe/tests/test_query_builder.py @@ -14,6 +14,7 @@ from frappe.query_builder.functions import ( GroupConcat, Match, Round, + Truncate, UnixTimestamp, ) from frappe.query_builder.utils import db_type_is @@ -163,6 +164,11 @@ class TestCustomFunctionsMariaDB(FrappeTestCase): query = frappe.qb.from_(note).select(Round(note.price, 3)) self.assertEqual("select round(`price`,3) from `tabnote`", str(query).lower()) + def test_truncate(self): + note = frappe.qb.DocType("Note") + query = frappe.qb.from_(note).select(Truncate(note.price, 3)) + self.assertEqual("select truncate(`price`,3) from `tabnote`", str(query).lower()) + @run_only_if(db_type_is.POSTGRES) class TestCustomFunctionsPostgres(FrappeTestCase): @@ -302,6 +308,11 @@ class TestCustomFunctionsPostgres(FrappeTestCase): query = frappe.qb.from_(note).select(Round(note.price, 3)) self.assertEqual('select round("price",3) from "tabnote"', str(query).lower()) + def test_truncate(self): + note = frappe.qb.DocType("Note") + query = frappe.qb.from_(note).select(Truncate(note.price, 3)) + self.assertEqual('select truncate("price",3) from "tabnote"', str(query).lower()) + class TestBuilderBase: def test_adding_tabs(self):