Skip to content

Commit 3f64e05

Browse files
pre-commit-ci[bot]sdebruyn
authored andcommitted
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent fb57607 commit 3f64e05

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

dbt/adapters/sqlserver/sql_server_connection_manager.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import datetime as dt
12
import struct
23
import time
3-
import datetime as dt
44
from contextlib import contextmanager
55
from itertools import chain, repeat
66
from typing import Any, Callable, Dict, Mapping, Optional, Tuple
@@ -237,16 +237,16 @@ def bool_to_connection_string_arg(key: str, value: bool) -> str:
237237
def byte_array_to_datetime(value: bytes) -> dt.datetime:
238238
"""
239239
Converts a DATETIMEOFFSET byte array to a timezone-aware datetime object
240-
240+
241241
Parameters
242242
----------
243243
value : buffer
244244
A binary value conforming to SQL_SS_TIMESTAMPOFFSET_STRUCT
245-
245+
246246
Returns
247247
-------
248248
out : datetime
249-
249+
250250
Source
251251
------
252252
SQL_SS_TIMESTAMPOFFSET datatype and SQL_SS_TIMESTAMPOFFSET_STRUCT layout:
@@ -263,10 +263,11 @@ def byte_array_to_datetime(value: bytes) -> dt.datetime:
263263
hour=tup[3],
264264
minute=tup[4],
265265
second=tup[5],
266-
microsecond=tup[6] // 1000, # https://bugs.python.org/issue15443
266+
microsecond=tup[6] // 1000, # https://bugs.python.org/issue15443
267267
tzinfo=dt.timezone(dt.timedelta(hours=tup[7], minutes=tup[8])),
268268
)
269269

270+
270271
class SQLServerConnectionManager(SQLConnectionManager):
271272
TYPE = "sqlserver"
272273

@@ -431,7 +432,7 @@ def add_query(
431432
# convert DATETIMEOFFSET binary structures to datetime ojbects
432433
# https://github.com/mkleehammer/pyodbc/issues/134#issuecomment-281739794
433434
connection.handle.add_output_converter(-155, byte_array_to_datetime)
434-
435+
435436
logger.debug(
436437
"SQL status: {} in {:0.2f} seconds".format(
437438
self.get_response(cursor), (time.time() - pre)

tests/unit/adapters/sqlserver/test_sql_server_connection_manager.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import datetime as dt
2-
import struct
32
import json
43
from unittest import mock
54

@@ -8,8 +7,8 @@
87

98
from dbt.adapters.sqlserver.sql_server_connection_manager import (
109
bool_to_connection_string_arg,
10+
byte_array_to_datetime,
1111
get_pyodbc_attrs_before,
12-
byte_array_to_datetime
1312
)
1413
from dbt.adapters.sqlserver.sql_server_credentials import SQLServerCredentials
1514

@@ -79,35 +78,52 @@ def test_get_pyodbc_attrs_before_contains_access_token_key_for_cli_authenticatio
7978
def test_bool_to_connection_string_arg(key: str, value: bool, expected: str) -> None:
8079
assert bool_to_connection_string_arg(key, value) == expected
8180

81+
8282
@pytest.mark.parametrize(
83-
"value, expected_datetime, expected_str", [
83+
"value, expected_datetime, expected_str",
84+
[
8485
(
85-
bytes([
86-
0xE5, 0x07, # 2022 year unsigned short
87-
0x0C, 0x00, # 12 month unsigned short
88-
0x11, 0x00, # 17 day unsigned short
89-
0x16, 0x00, # 17 hour unsigned short
90-
0x16, 0x00, # 52 minute unsigned short
91-
0x12, 0x00, # 18 second unsigned short
92-
0xBC, 0xCC, 0x5B, 0x07, # 123456700 10⁻⁷ second unsigned long
93-
0xFE, 0xFF, # -2 offset hour signed short
94-
0xE2, 0xFF # -30 offset minute signed short
95-
]),
86+
bytes(
87+
[
88+
0xE5,
89+
0x07, # 2022 year unsigned short
90+
0x0C,
91+
0x00, # 12 month unsigned short
92+
0x11,
93+
0x00, # 17 day unsigned short
94+
0x16,
95+
0x00, # 17 hour unsigned short
96+
0x16,
97+
0x00, # 52 minute unsigned short
98+
0x12,
99+
0x00, # 18 second unsigned short
100+
0xBC,
101+
0xCC,
102+
0x5B,
103+
0x07, # 123456700 10⁻⁷ second unsigned long
104+
0xFE,
105+
0xFF, # -2 offset hour signed short
106+
0xE2,
107+
0xFF, # -30 offset minute signed short
108+
]
109+
),
96110
dt.datetime(
97111
year=2022,
98112
month=12,
99113
day=17,
100114
hour=17,
101115
minute=52,
102116
second=18,
103-
microsecond=123456700 // 1000, # 10⁻⁶ second
104-
tzinfo=dt.timezone(dt.timedelta(hours=-2, minutes=-30))
117+
microsecond=123456700 // 1000, # 10⁻⁶ second
118+
tzinfo=dt.timezone(dt.timedelta(hours=-2, minutes=-30)),
105119
),
106-
"2021-12-17 17:52:18.123456-02:30"
120+
"2021-12-17 17:52:18.123456-02:30",
107121
)
108-
]
122+
],
109123
)
110-
def test_byte_array_to_datetime(value: bytes, expected_datetime: dt.datetime, expected_str: str) -> None:
124+
def test_byte_array_to_datetime(
125+
value: bytes, expected_datetime: dt.datetime, expected_str: str
126+
) -> None:
111127
"""
112128
Assert SQL_SS_TIMESTAMPOFFSET_STRUCT bytes are converted to datetime and str
113129
https://learn.microsoft.com/sql/relational-databases/native-client-odbc-date-time/data-type-support-for-odbc-date-and-time-improvements#sql_ss_timestampoffset_struct

0 commit comments

Comments
 (0)