Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for json libraries #130

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Python & Poetry Environment
uses: exasol/python-toolbox/.github/actions/python-environment@0.7.0
uses: exasol/python-toolbox/.github/actions/python-environment@0.13.0
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -43,7 +43,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Python & Poetry Environment
uses: exasol/python-toolbox/.github/actions/python-environment@0.7.0
uses: exasol/python-toolbox/.github/actions/python-environment@0.13.0
with:
python-version: ${{ matrix.python-version }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: actions/checkout@v2

- name: "Setup Python & Poetry Environment"
uses: exasol/python-toolbox/.github/actions/python-environment@0.7.0
uses: exasol/python-toolbox/.github/actions/python-environment@0.13.0
with:
python-version: ${{ matrix.python-version }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ssl_cert.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: actions/checkout@v2

- name: "Setup Python & Poetry Environment"
uses: exasol/python-toolbox/.github/actions/python-environment@0.7.0
uses: exasol/python-toolbox/.github/actions/python-environment@0.13.0
with:
python-version: ${{ matrix.python-version }}

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ markers = [
"format: tests related to f-string based statement formatting.",
"transaction: tests related to transaction management.",
"exceptions: tests related to exceptions in pyexasol.",
"extensions: tests related to pyexasol extensions.",
"context_managers: tests related to pyexasol context_managers.",
"metadata: tests related to metadata retrieval with pyexasol."
"metadata: tests related to metadata retrieval with pyexasol.",
"json: tests related to json serialization in pyexasol."
]

139 changes: 139 additions & 0 deletions test/integration/json_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import pytest
import pyexasol
import decimal

from inspect import cleandoc


@pytest.fixture
def connection_factory(dsn, user, password, schema):
connections = []

def factory(json_lib):
con = pyexasol.connect(
dsn=dsn,
user=user,
password=password,
schema=schema,
json_lib=json_lib,
)
connections.append(con)
return con

yield factory

for c in connections:
c.close()


@pytest.fixture
def table(connection):
name = "edge_case"
ddl = cleandoc(
f"""CREATE OR REPLACE TABLE {name}
(
dec36_0 DECIMAL(36,0),
dec36_36 DECIMAL(36,36),
dbl DOUBLE,
bl BOOLEAN,
dt DATE,
ts TIMESTAMP,
var100 VARCHAR(100),
var2000000 VARCHAR(2000000)
)
"""
)
connection.execute(ddl)
connection.commit()

yield name

delete_stmt = f"DROP TABLE IF EXISTS {name};"
connection.execute(delete_stmt)
connection.commit()


@pytest.fixture
def edge_cases():
return [
# Biggest values
{
"DEC36_0": decimal.Decimal("+" + ("9" * 36)),
"DEC36_36": decimal.Decimal("+0." + ("9" * 36)),
"DBL": 1.7e308,
"BL": True,
"DT": "9999-12-31",
"TS": "9999-12-31 23:59:59.999",
"VAR100": "ひ" * 100,
"VAR2000000": "ひ" * 2000000,
},
# Smallest values
{
"DEC36_0": decimal.Decimal("-" + ("9" * 36)),
"DEC36_36": decimal.Decimal("-0." + ("9" * 36)),
"DBL": -1.7e308,
"BL": False,
"DT": "0001-01-01",
"TS": "0001-01-01 00:00:00",
"VAR100": "",
"VAR2000000": "ひ",
},
# All nulls
{
"DEC36_0": None,
"DEC36_36": None,
"DBL": None,
"BL": None,
"DT": None,
"TS": None,
"VAR100": None,
"VAR2000000": None,
},
]


@pytest.mark.json
@pytest.mark.parametrize("json_lib", ["orjson", "ujson", "rapidjson"])
def test_insert(table, connection_factory, edge_cases, json_lib):
connection = connection_factory(json_lib)
insert_stmt = (
"INSERT INTO edge_case VALUES"
"({DEC36_0!d}, {DEC36_36!d}, {DBL!f}, {BL}, {DT}, {TS}, {VAR100}, {VAR2000000})"
)
for edge_case in edge_cases:
connection.execute(insert_stmt, edge_case)

expected = len(edge_cases)
actual = connection.execute(f"SELECT COUNT(*) FROM {table};").fetchval()

assert actual == expected


@pytest.mark.json
@pytest.mark.parametrize("json_lib", ["orjson", "ujson", "rapidjson"])
def test_select(table, connection_factory, edge_cases, json_lib):
connection = connection_factory(json_lib)

insert_stmt = (
"INSERT INTO edge_case VALUES"
"({DEC36_0!d}, {DEC36_36!d}, {DBL!f}, {BL}, {DT}, {TS}, {VAR100}, {VAR2000000})"
)
for edge_case in edge_cases:
connection.execute(insert_stmt, edge_case)

select_stmt = (
"SELECT DEC36_0, DEC36_36, DBL, BL, DT, TS, VAR100, LENGTH(VAR2000000) "
"AS LEN_VAR FROM EDGE_CASE"
)

expected = {
# Biggest values
("9" * 36, f"0.{'9' * 36}", 1.7e308, True, "9999-12-31", "9999-12-31 23:59:59.999000", "ひ" * 100, 2000000),
# Smallest values
(f"-{'9' * 36}", f"-0.{'9' * 36}", -1.7e308, False, "0001-01-01", "0001-01-01 00:00:00.000000", None, 1),
# All nulls
(None, None, None, None, None, None, None, None),
}
actual = set(connection.execute(select_stmt).fetchall())

assert actual == expected
Loading