Skip to content

Commit 53617fc

Browse files
Merge pull request #744 from pyathena-dev/fix/athena-timestamp-test-and-date-isinstance
test(sqlalchemy): cover AthenaTimestamp.process and simplify AthenaDate isinstance
2 parents cb639cb + edbee3c commit 53617fc

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

pyathena/sqlalchemy/types.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ class AthenaDate(TypeEngine[date]):
7979
render_bind_cast = True
8080

8181
@staticmethod
82-
def process(value: date | datetime | Any) -> str:
83-
if isinstance(value, (date, datetime)):
82+
def process(value: date | Any) -> str:
83+
# datetime is a subclass of date, so this branch also covers datetime,
84+
# which is truncated to its date part.
85+
if isinstance(value, date):
8486
return f"DATE '{value:%Y-%m-%d}'"
8587
return f"DATE '{value!s}'"
8688

tests/pyathena/sqlalchemy/test_types.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
AthenaDate,
1313
AthenaMap,
1414
AthenaStruct,
15+
AthenaTimestamp,
1516
get_double_type,
1617
)
1718

@@ -178,3 +179,32 @@ class TestAthenaDate:
178179
)
179180
def test_process_renders_date_only_literal(self, value, expected):
180181
assert AthenaDate.process(value) == expected
182+
183+
def test_process_falls_back_to_str(self):
184+
assert AthenaDate.process("2017-01-01") == "DATE '2017-01-01'"
185+
186+
187+
class TestAthenaTimestamp:
188+
@pytest.mark.parametrize(
189+
("value", "expected"),
190+
[
191+
# Athena TIMESTAMP has millisecond precision, so the six digits
192+
# strftime("%f") emits are truncated to three.
193+
(
194+
datetime(2017, 1, 1, 12, 34, 56, 789012),
195+
"TIMESTAMP '2017-01-01 12:34:56.789'",
196+
),
197+
(
198+
datetime(2017, 1, 1, 12, 34, 56),
199+
"TIMESTAMP '2017-01-01 12:34:56.000'",
200+
),
201+
],
202+
)
203+
def test_process_renders_millisecond_precision_literal(self, value, expected):
204+
assert AthenaTimestamp.process(value) == expected
205+
206+
def test_process_falls_back_to_str(self):
207+
assert (
208+
AthenaTimestamp.process("2017-01-01 12:34:56.789")
209+
== "TIMESTAMP '2017-01-01 12:34:56.789'"
210+
)

0 commit comments

Comments
 (0)