Skip to content

Commit

Permalink
Types: Accept marshalling datetime.date values on DateTime fields
Browse files Browse the repository at this point in the history
The test suite of `meltano-tap-cratedb`, derived from the corresponding
PostgreSQL adapter, will supply `dt.date` objects. Without this patch,
those will otherwise fail on this routine.
  • Loading branch information
amotl committed Jun 25, 2024
1 parent fb4c7be commit bd7a00e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Fixed SQL rendering of special DDL table options in `CrateDDLCompiler`.
Before, configuring `crate_"translog.durability"` was not possible.
- Unlocked supporting timezone-aware `DateTime` fields
- Added support for marshalling Python `datetime.date` values on `sa.DateTime` fields

## 2024/06/13 0.37.0
- Added support for CrateDB's [FLOAT_VECTOR] data type and its accompanying
Expand Down
6 changes: 3 additions & 3 deletions src/sqlalchemy_cratedb/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ class DateTime(sqltypes.DateTime):

def bind_processor(self, dialect):
def process(value):
if value is not None:
assert isinstance(value, datetime)
if isinstance(value, (datetime, date)):

Check warning on line 118 in src/sqlalchemy_cratedb/dialect.py

View check run for this annotation

Codecov / codecov/patch

src/sqlalchemy_cratedb/dialect.py#L118

Added line #L118 was not covered by tests
return value.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
return value
else:
return value

Check warning on line 121 in src/sqlalchemy_cratedb/dialect.py

View check run for this annotation

Codecov / codecov/patch

src/sqlalchemy_cratedb/dialect.py#L121

Added line #L121 was not covered by tests
return process

def result_processor(self, dialect, coltype):
Expand Down
28 changes: 28 additions & 0 deletions tests/datetime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,31 @@ def test_datetime_tz(session):
assert result["datetime_tz"].tzname() is None
assert result["datetime_tz"].timetz() == OUTPUT_TIMETZ_TZ
assert result["datetime_tz"].tzinfo is None


@pytest.mark.skipif(SA_VERSION < SA_1_4, reason="Test case not supported on SQLAlchemy 1.3")
def test_datetime_date(session):
"""
Validate assigning a `date` object to a `datetime` column works.
It is needed by meltano-tap-cratedb.
The test suite of `meltano-tap-cratedb`, derived from the corresponding
PostgreSQL adapter, will supply `dt.date` objects. Without this improvement,
those will otherwise fail.
"""

# Insert record.
foo_item = FooBar(
name="foo",
datetime=dt.date(2009, 5, 13),
)
session.add(foo_item)
session.commit()
session.execute(sa.text("REFRESH TABLE foobar"))

# Query record.
result = session.execute(sa.select(FooBar.name, FooBar.date, FooBar.datetime)).mappings().first()

# Compare outcome.
assert result["datetime"] == dt.datetime(2009, 5, 13, 0, 0, 0)

0 comments on commit bd7a00e

Please sign in to comment.