Skip to content

Commit

Permalink
fix: Add tests for SQL type conversion from JSON schemas (#1775)
Browse files Browse the repository at this point in the history
* add tests for sql type parsing

* fix anyof sqltype parsing test

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* cleanup precommit errors

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update imports in tests

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
pnadolny13 and pre-commit-ci[bot] authored Jun 19, 2023
1 parent e877215 commit 7e8997f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
4 changes: 3 additions & 1 deletion singer_sdk/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,9 @@ def _jsonschema_type_check(jsonschema_type: dict, type_check: tuple[str]) -> boo
if jsonschema_type.get("type") in type_check: # noqa: PLR5501
return True

if any(t in type_check for t in jsonschema_type.get("anyOf", ())):
if any(
_jsonschema_type_check(t, type_check) for t in jsonschema_type.get("anyOf", ())
):
return True

return False
Expand Down
34 changes: 30 additions & 4 deletions tests/core/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import datetime
import logging
import typing as t

import pytest
import sqlalchemy

from singer_sdk.helpers._typing import (
TypeConformanceLevel,
Expand All @@ -17,11 +19,9 @@
PropertiesList,
Property,
StringType,
to_sql_type,
)

if t.TYPE_CHECKING:
import pytest

logger = logging.getLogger("log")


Expand Down Expand Up @@ -292,3 +292,29 @@ def test_conform_primitives():
assert _conform_primitive_property(None, {"type": "boolean"}) is None
assert _conform_primitive_property(0, {"type": "boolean"}) is False
assert _conform_primitive_property(1, {"type": "boolean"}) is True


@pytest.mark.parametrize(
"jsonschema_type,expected",
[
({"type": ["string", "null"]}, sqlalchemy.types.VARCHAR),
({"type": ["integer", "null"]}, sqlalchemy.types.INTEGER),
({"type": ["number", "null"]}, sqlalchemy.types.DECIMAL),
({"type": ["boolean", "null"]}, sqlalchemy.types.BOOLEAN),
({"type": "object", "properties": {}}, sqlalchemy.types.VARCHAR),
({"type": "array"}, sqlalchemy.types.VARCHAR),
({"format": "date", "type": ["string", "null"]}, sqlalchemy.types.DATE),
({"format": "time", "type": ["string", "null"]}, sqlalchemy.types.TIME),
(
{"format": "date-time", "type": ["string", "null"]},
sqlalchemy.types.DATETIME,
),
(
{"anyOf": [{"type": "string", "format": "date-time"}, {"type": "null"}]},
sqlalchemy.types.DATETIME,
),
({"anyOf": [{"type": "integer"}, {"type": "null"}]}, sqlalchemy.types.INTEGER),
],
)
def test_to_sql_type(jsonschema_type, expected):
assert isinstance(to_sql_type(jsonschema_type), expected)

0 comments on commit 7e8997f

Please sign in to comment.