feat: Truncate QB function

This commit is contained in:
Sagar Sharma 2023-05-26 13:23:47 +05:30
parent 3f0b03c808
commit a43ad15eab
2 changed files with 16 additions and 0 deletions

View file

@ -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})

View file

@ -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):