From ada6602717c19c058f9a7a95c17ebcddc5911e14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 17 Oct 2023 17:09:36 +0200 Subject: [PATCH] Return None when date_trunc is called with NULL in SQLite Closes #12649 --- trytond/trytond/backend/sqlite/database.py | 2 ++ trytond/trytond/tests/test_backend.py | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/trytond/trytond/backend/sqlite/database.py b/trytond/trytond/backend/sqlite/database.py index 534d8260fe..51fd17e616 100644 --- a/trytond/trytond/backend/sqlite/database.py +++ b/trytond/trytond/backend/sqlite/database.py @@ -87,6 +87,8 @@ class SQLiteExtract(Function): def date_trunc(_type, date): if not _type: return date + if date is None: + return None for format_ in [ '%Y-%m-%d %H:%M:%S.%f', '%Y-%m-%d %H:%M:%S', diff --git a/trytond/trytond/tests/test_backend.py b/trytond/trytond/tests/test_backend.py index 3bb8376076..0af6951d5b 100644 --- a/trytond/trytond/tests/test_backend.py +++ b/trytond/trytond/tests/test_backend.py @@ -7,6 +7,7 @@ import unittest from sql import Literal, Select, functions from sql.functions import CurrentTimestamp, DateTrunc, ToChar +from trytond.model import fields from trytond.tests.test_tryton import activate_module, with_transaction from trytond.transaction import Transaction @@ -171,3 +172,13 @@ class BackendTestCase(unittest.TestCase): cursor.execute(*Select([DateTrunc(type_, date)])) value, = cursor.fetchone() self.assertEqual(str(value), str(result)) + + @with_transaction() + def test_function_date_trunc_null(self): + "test DateTrunc function with NULL" + cursor = Transaction().connection.cursor() + date = fields.Date("Test") + + cursor.execute(*Select([DateTrunc('month', date.sql_cast(None))])) + value, = cursor.fetchone() + self.assertEqual(value, None)