From deec15a9a613558b35024637d02d348a8a95deb1 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Fri, 17 Dec 2021 07:50:27 +0000 Subject: [PATCH] Refs #33355 -- Made trunc functions raise ValueError on invalid lookups on SQLite. Co-Authored-By: Nick Pope --- django/db/backends/sqlite3/_functions.py | 3 +++ tests/backends/sqlite/test_functions.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/backends/sqlite/test_functions.py diff --git a/django/db/backends/sqlite3/_functions.py b/django/db/backends/sqlite3/_functions.py index a699db0591..f1fcd6e3a5 100644 --- a/django/db/backends/sqlite3/_functions.py +++ b/django/db/backends/sqlite3/_functions.py @@ -135,6 +135,7 @@ def _sqlite_date_trunc(lookup_type, dt, tzname, conn_tzname): return f'{dt.year:04d}-{dt.month:02d}-{dt.day:02d}' elif lookup_type == 'day': return f'{dt.year:04d}-{dt.month:02d}-{dt.day:02d}' + raise ValueError(f'Unsupported lookup type: {lookup_type!r}') def _sqlite_time_trunc(lookup_type, dt, tzname, conn_tzname): @@ -154,6 +155,7 @@ def _sqlite_time_trunc(lookup_type, dt, tzname, conn_tzname): return f'{dt.hour:02d}:{dt.minute:02d}:00' elif lookup_type == 'second': return f'{dt.hour:02d}:{dt.minute:02d}:{dt.second:02d}' + raise ValueError(f'Unsupported lookup type: {lookup_type!r}') def _sqlite_datetime_cast_date(dt, tzname, conn_tzname): @@ -210,6 +212,7 @@ def _sqlite_datetime_trunc(lookup_type, dt, tzname, conn_tzname): return f'{dt.year:04d}-{dt.month:02d}-{dt.day:02d} {dt.hour:02d}:{dt.minute:02d}:00' elif lookup_type == 'second': return f'{dt.year:04d}-{dt.month:02d}-{dt.day:02d} {dt.hour:02d}:{dt.minute:02d}:{dt.second:02d}' + raise ValueError(f'Unsupported lookup type: {lookup_type!r}') def _sqlite_time_extract(lookup_type, dt): diff --git a/tests/backends/sqlite/test_functions.py b/tests/backends/sqlite/test_functions.py new file mode 100644 index 0000000000..0659717799 --- /dev/null +++ b/tests/backends/sqlite/test_functions.py @@ -0,0 +1,21 @@ +from django.db.backends.sqlite3._functions import ( + _sqlite_date_trunc, _sqlite_datetime_trunc, _sqlite_time_trunc, +) +from django.test import SimpleTestCase + + +class FunctionTests(SimpleTestCase): + def test_sqlite_date_trunc(self): + msg = "Unsupported lookup type: 'unknown-lookup'" + with self.assertRaisesMessage(ValueError, msg): + _sqlite_date_trunc('unknown-lookup', '2005-08-11', None, None) + + def test_sqlite_datetime_trunc(self): + msg = "Unsupported lookup type: 'unknown-lookup'" + with self.assertRaisesMessage(ValueError, msg): + _sqlite_datetime_trunc('unknown-lookup', '2005-08-11 1:00:00', None, None) + + def test_sqlite_time_trunc(self): + msg = "Unsupported lookup type: 'unknown-lookup'" + with self.assertRaisesMessage(ValueError, msg): + _sqlite_time_trunc('unknown-lookup', '2005-08-11 1:00:00', None, None)