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 3 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
2 changes: 1 addition & 1 deletion singer_sdk/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ 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
31 changes: 31 additions & 0 deletions tests/core/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
PropertiesList,
Property,
StringType,
to_sql_type,
)

if t.TYPE_CHECKING:
Expand Down Expand Up @@ -292,3 +293,33 @@ 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


import pytest
import sqlalchemy

edgarrmondragon marked this conversation as resolved.
Show resolved Hide resolved

@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)