-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add (DuckDB) SQL sink tests (#2548)
- Loading branch information
1 parent
9a7c539
commit 3bd3770
Showing
4 changed files
with
96 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from __future__ import annotations | ||
|
||
from .connector import DuckDBConnector | ||
|
||
__all__ = [ | ||
"DuckDBConnector", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from __future__ import annotations | ||
|
||
import sqlalchemy as sa | ||
|
||
from singer_sdk.connectors import SQLConnector | ||
|
||
|
||
class DuckDBConnector(SQLConnector): | ||
allow_column_alter = True | ||
|
||
@staticmethod | ||
def get_column_alter_ddl( | ||
table_name: str, | ||
column_name: str, | ||
column_type: sa.types.TypeEngine, | ||
) -> sa.DDL: | ||
return sa.DDL( | ||
"ALTER TABLE %(table_name)s ALTER COLUMN %(column_name)s TYPE %(column_type)s", # noqa: E501 | ||
{ | ||
"table_name": table_name, | ||
"column_name": column_name, | ||
"column_type": column_type, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
from textwrap import dedent | ||
|
||
import pytest | ||
|
||
from samples.sample_duckdb import DuckDBConnector | ||
from singer_sdk.sinks.sql import SQLSink | ||
from singer_sdk.target_base import SQLTarget | ||
|
||
|
||
class DuckDBSink(SQLSink): | ||
connector_class = DuckDBConnector | ||
|
||
|
||
class DuckDBTarget(SQLTarget): | ||
"""DuckDB target class.""" | ||
|
||
name = "sql-target-mock" | ||
config_jsonschema: t.ClassVar[dict] = {"type": "object", "properties": {}} | ||
default_sink_class = DuckDBSink | ||
|
||
|
||
class TestDuckDBSink: | ||
@pytest.fixture | ||
def target(self) -> DuckDBTarget: | ||
return DuckDBTarget(config={"sqlalchemy_url": "duckdb:///"}) | ||
|
||
@pytest.fixture | ||
def schema(self) -> dict: | ||
return { | ||
"properties": { | ||
"id": { | ||
"type": ["string", "null"], | ||
}, | ||
"col_ts": { | ||
"format": "date-time", | ||
"type": ["string", "null"], | ||
}, | ||
"table": { | ||
"type": ["string", "null"], | ||
}, | ||
}, | ||
} | ||
|
||
@pytest.fixture | ||
def sink(self, target: DuckDBTarget, schema: dict) -> DuckDBSink: | ||
return DuckDBSink( | ||
target, | ||
stream_name="foo", | ||
schema=schema, | ||
key_properties=["id"], | ||
) | ||
|
||
def test_generate_insert_statement(self, sink: DuckDBSink, schema: dict): | ||
"""Test that the insert statement is generated correctly.""" | ||
expected = dedent( | ||
"""\ | ||
INSERT INTO foo | ||
(id, col_ts, "table") | ||
VALUES (:id, :col_ts, :table)""" | ||
) | ||
assert sink.generate_insert_statement("foo", schema=schema) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters