diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-04-03 13:44:57 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-04-03 14:47:52 -0400 |
commit | ccadbec82555c53eefa889160510f5af1e224709 (patch) | |
tree | 959b4309fcc26191ef791034bda76e6fda0d1bdb /test/engine/test_processors.py | |
parent | 1dffb7cedeb009ca6c532db558bd0588dd846957 (diff) | |
download | sqlalchemy-ccadbec82555c53eefa889160510f5af1e224709.tar.gz |
use .fromisoformat() for sqlite datetime, date, time parsing
SQLite datetime, date, and time datatypes now use Python standard lib
``fromisoformat()`` methods in order to parse incoming datetime, date, and
time string values. This improves performance vs. the previous regular
expression-based approach, and also automatically accommodates for datetime
and time formats that contain either a six-digit "microseconds" format or a
three-digit "milliseconds" format.
Fixes: #7029
Change-Id: I67aab4fe5ee3055e5996050cf4564981413cc221
Diffstat (limited to 'test/engine/test_processors.py')
-rw-r--r-- | test/engine/test_processors.py | 59 |
1 files changed, 50 insertions, 9 deletions
diff --git a/test/engine/test_processors.py b/test/engine/test_processors.py index 392632327..5f28e3ea0 100644 --- a/test/engine/test_processors.py +++ b/test/engine/test_processors.py @@ -1,6 +1,9 @@ +import datetime +import re from types import MappingProxyType from sqlalchemy import exc +from sqlalchemy.engine import processors from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import eq_ from sqlalchemy.testing import expect_raises_message @@ -36,34 +39,72 @@ class CyBooleanProcessorTest(_BooleanProcessorTest): class _DateProcessorTest(fixtures.TestBase): + def test_iso_datetime(self): + eq_( + self.module.str_to_datetime("2022-04-03 17:12:34.353"), + datetime.datetime(2022, 4, 3, 17, 12, 34, 353000), + ) + + eq_( + self.module.str_to_datetime("2022-04-03 17:12:34.353123"), + datetime.datetime(2022, 4, 3, 17, 12, 34, 353123), + ) + + eq_( + self.module.str_to_datetime("2022-04-03 17:12:34"), + datetime.datetime(2022, 4, 3, 17, 12, 34), + ) + + eq_( + self.module.str_to_time("17:12:34.353123"), + datetime.time(17, 12, 34, 353123), + ) + + eq_( + self.module.str_to_time("17:12:34.353"), + datetime.time(17, 12, 34, 353000), + ) + + eq_( + self.module.str_to_time("17:12:34"), + datetime.time(17, 12, 34), + ) + + eq_(self.module.str_to_date("2022-04-03"), datetime.date(2022, 4, 3)) + def test_date_no_string(self): assert_raises_message( - ValueError, - "Couldn't parse date string '2012' - value is not a string", + TypeError, + "fromisoformat: argument must be str", self.module.str_to_date, 2012, ) - def test_datetime_no_string(self): + def test_datetime_no_string_custom_reg(self): assert_raises_message( ValueError, "Couldn't parse datetime string '2012' - value is not a string", - self.module.str_to_datetime, + processors.str_to_datetime_processor_factory( + re.compile(r"(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)(?:\.(\d+))?"), + datetime.datetime, + ), 2012, ) - def test_time_no_string(self): + def test_time_no_string_custom_reg(self): assert_raises_message( ValueError, "Couldn't parse time string '2012' - value is not a string", - self.module.str_to_time, + processors.str_to_datetime_processor_factory( + re.compile(r"^(\d+):(\d+):(\d+)(?:\.(\d{6}))?$"), datetime.time + ), 2012, ) def test_date_invalid_string(self): assert_raises_message( ValueError, - "Couldn't parse date string: '5:a'", + "Invalid isoformat string: '5:a'", self.module.str_to_date, "5:a", ) @@ -71,7 +112,7 @@ class _DateProcessorTest(fixtures.TestBase): def test_datetime_invalid_string(self): assert_raises_message( ValueError, - "Couldn't parse datetime string: '5:a'", + "Invalid isoformat string: '5:a'", self.module.str_to_datetime, "5:a", ) @@ -79,7 +120,7 @@ class _DateProcessorTest(fixtures.TestBase): def test_time_invalid_string(self): assert_raises_message( ValueError, - "Couldn't parse time string: '5:a'", + "Invalid isoformat string: '5:a'", self.module.str_to_time, "5:a", ) |