Skip to content

Commit

Permalink
fix: support JSON type (#186)
Browse files Browse the repository at this point in the history
Closes #184

See failing
[test](https://github.com/MeltanoLabs/tap-postgres/actions/runs/5649982107/job/15305459180)
before I added the change that Derek suggested.
  • Loading branch information
pnadolny13 authored Jul 25, 2023
1 parent e5a9da7 commit 260a507
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion tap_postgres/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def to_jsonschema_type(
elif isinstance(sql_type, sqlalchemy.types.TypeEngine):
type_name = type(sql_type).__name__

if type_name is not None and type_name == "JSONB":
if type_name is not None and type_name in ("JSONB", "JSON"):
return th.ObjectType().type_dict

if (
Expand Down
24 changes: 16 additions & 8 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from singer_sdk.testing import get_tap_test_class, suites
from singer_sdk.testing.runners import TapTestRunner
from sqlalchemy import Column, DateTime, Integer, MetaData, Numeric, String, Table
from sqlalchemy.dialects.postgresql import DATE, JSONB, TIME, TIMESTAMP
from sqlalchemy.dialects.postgresql import DATE, JSONB, TIME, TIMESTAMP, JSON
from test_replication_key import TABLE_NAME, TapTestReplicationKey
from test_selected_columns_only import (
TABLE_NAME_SELECTED_COLUMNS_ONLY,
Expand Down Expand Up @@ -193,22 +193,26 @@ def test_temporal_datatypes():
}


def test_jsonb():
"""JSONB Objects weren't being selected, make sure they are now"""
table_name = "test_jsonb"
def test_jsonb_json():
"""JSONB and JSON Objects weren't being selected, make sure they are now"""
table_name = "test_jsonb_json"
engine = sqlalchemy.create_engine(SAMPLE_CONFIG["sqlalchemy_url"])

metadata_obj = MetaData()
table = Table(
table_name,
metadata_obj,
Column("column", JSONB),
Column("column_jsonb", JSONB),
Column("column_json", JSON),
)
with engine.connect() as conn:
if table.exists(conn):
table.drop(conn)
metadata_obj.create_all(conn)
insert = table.insert().values(column={"foo": "bar"})
insert = table.insert().values(
column_jsonb={"foo": "bar"},
column_json={"baz": "foo"},
)
conn.execute(insert)
tap = TapPostgres(config=SAMPLE_CONFIG)
tap_catalog = json.loads(tap.catalog_json_text)
Expand All @@ -232,8 +236,12 @@ def test_jsonb():
"stream" in schema_message
and schema_message["stream"] == altered_table_name
):
assert "object" in schema_message["schema"]["properties"]["column"]["type"]
assert test_runner.records[altered_table_name][0] == {"column": {"foo": "bar"}}
assert "object" in schema_message["schema"]["properties"]["column_jsonb"]["type"]
assert "object" in schema_message["schema"]["properties"]["column_json"]["type"]
assert test_runner.records[altered_table_name][0] == {
"column_jsonb": {"foo": "bar"},
"column_json": {"baz": "foo"}
}


def test_decimal():
Expand Down

0 comments on commit 260a507

Please sign in to comment.