From 21870c750302704890f5fc7a4e15c7e27c3e512f Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 22 Jan 2012 19:17:21 -0500 Subject: [PATCH] - [bug] Adjusted the regexp used in the mssql.TIME type to ensure only six digits are received for the "microseconds" portion of the value, which is expected by Python's datetime.time(). Note that support for sending microseconds doesn't seem to be possible yet with pyodbc at least. [ticket:2340] --- CHANGES | 10 ++++++++++ lib/sqlalchemy/dialects/mssql/base.py | 2 +- test/dialect/test_mssql.py | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 53220bfe59..b28618d23d 100644 --- a/CHANGES +++ b/CHANGES @@ -71,6 +71,16 @@ CHANGES for non-reflected "PARTITION" directives, thanks to George Reilly [ticket:2376] +- mssql + - [bug] Adjusted the regexp used in the + mssql.TIME type to ensure only six digits + are received for the "microseconds" portion + of the value, which is expected by + Python's datetime.time(). Note that + support for sending microseconds doesn't + seem to be possible yet with pyodbc + at least. [ticket:2340] + - Py3K - [bug] Fixed inappropriate usage of util.py3k flag and renamed it to util.py3k_warning, since diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index 7df83d0574..4d7dd1c582 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -296,7 +296,7 @@ class TIME(sqltypes.TIME): return value return process - _reg = re.compile(r"(\d+):(\d+):(\d+)(?:\.(\d+))?") + _reg = re.compile(r"(\d+):(\d+):(\d+)(?:\.(\d{0,6}))?") def result_processor(self, dialect, coltype): def process(value): if isinstance(value, datetime.datetime): diff --git a/test/dialect/test_mssql.py b/test/dialect/test_mssql.py index 75e9510eb3..94609d9534 100644 --- a/test/dialect/test_mssql.py +++ b/test/dialect/test_mssql.py @@ -10,6 +10,7 @@ from sqlalchemy.orm import * from sqlalchemy.sql import table, column from sqlalchemy.databases import mssql from sqlalchemy.dialects.mssql import pyodbc, mxodbc, pymssql +from sqlalchemy.dialects.mssql.base import TIME from sqlalchemy.engine import url from test.lib import * from test.lib.testing import eq_, emits_warning_on, \ @@ -1108,6 +1109,22 @@ class ParseConnectTest(fixtures.TestBase, AssertsCompiledSQL): 'Unrecognized server version info', engine.connect) +class TimeTypeTest(fixtures.TestBase): + + def test_result_processor_no_microseconds(self): + expected = datetime.time(12, 34, 56) + self._assert_result_processor(expected, '12:34:56') + + def test_result_processor_too_many_microseconds(self): + # microsecond must be in 0..999999, should truncate (6 vs 7 digits) + expected = datetime.time(12, 34, 56, 123456) + self._assert_result_processor(expected, '12:34:56.1234567') + + def _assert_result_processor(self, expected, value): + mssql_time_type = TIME() + result_processor = mssql_time_type.result_processor(None, None) + eq_(expected, result_processor(value)) + class TypesTest(fixtures.TestBase, AssertsExecutionResults, ComparesTables): __only_on__ = 'mssql' -- 2.47.2