Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit fbdb101

Browse files
author
Sergey Vasilyev
committed
Escape and serialise the Postgres JSON/JSONB values in tests
1 parent 6cdd0a6 commit fbdb101

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

tests/test_database_types.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,15 @@ def _insert_to_table(conn, table_path, values, coltype):
604604
elif isinstance(conn, db.BigQuery) and coltype == "datetime":
605605
values = [(i, Code(f"cast(timestamp '{sample}' as datetime)")) for i, sample in values]
606606

607-
if isinstance(conn, db.Redshift) and coltype == "json":
608-
values = [(i, Code(f"JSON_PARSE('{sample}')")) for i, sample in values]
607+
elif isinstance(conn, db.Redshift) and coltype in ("json", "jsonb"):
608+
values = [(i, Code(f"JSON_PARSE({sample})")) for i, sample in values]
609+
elif isinstance(conn, db.PostgreSQL) and coltype in ("json", "jsonb"):
610+
values = [(i, Code(
611+
"'{}'".format(
612+
(json.dumps(sample) if isinstance(sample, (dict, list)) else sample)
613+
.replace('\'', '\'\'')
614+
)
615+
)) for i, sample in values]
609616

610617
insert_rows_in_batches(conn, tbl, values, columns=["id", "col"])
611618
conn.query(commit)

0 commit comments

Comments
 (0)