Return None when date_trunc is called with NULL in SQLite

Closes #12649
This commit is contained in:
Cédric Krier
2023-10-17 17:09:36 +02:00
parent 7976c25985
commit ada6602717
2 changed files with 13 additions and 0 deletions
@@ -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',
+11
View File
@@ -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)