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

fix: Add tests for SQL type conversion from JSON schemas #1775

Merged
merged 7 commits into from
Jun 19, 2023
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: 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
pnadolny13 marked this conversation as resolved.
Show resolved Hide resolved

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)