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: Resolve error TypeError "unhashable type list" (2nd pass) #251

Merged
merged 8 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 7 additions & 2 deletions airbyte/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ def _get_airbyte_type( # noqa: PLR0911 # Too many return statements
if json_schema_format == "time":
return "time_without_timezone", None

if json_schema_type in {"string", "number", "boolean", "integer"}:
return cast(str, json_schema_type), None
if isinstance(json_schema_type, str) and json_schema_type in {
"string",
"number",
"boolean",
"integer",
}:
return json_schema_type, None

if json_schema_type == "object":
return "object", None
Expand Down
12 changes: 10 additions & 2 deletions tests/integration_tests/fixtures/source-test/source_test/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,23 @@
sample_record1_stream1 = {
"type": "RECORD",
"record": {
"data": {"Column1": "value1", "Column2": 1},
"data": {
"Column1": "value1",
"Column2": 1,
"sometimes_object": {"nested_column": "nested_value"},
},
"stream": "stream1",
"emitted_at": 1704067200,
},
}
sample_record2_stream1 = {
"type": "RECORD",
"record": {
"data": {"Column1": "value2", "Column2": 2},
"data": {
"Column1": "value2",
"Column2": 2,
"sometimes_object": "string_value",
},
"stream": "stream1",
"emitted_at": 1704067200,
},
Expand Down
12 changes: 10 additions & 2 deletions tests/integration_tests/test_source_test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,16 @@ def source_test(source_test_env) -> ab.Source:
def expected_test_stream_data() -> dict[str, list[dict[str, str | int]]]:
return {
"stream1": [
{"column1": "value1", "column2": 1},
{"column1": "value2", "column2": 2},
{
"column1": "value1",
"column2": 1,
"sometimes_object": '{"nested_column":"nested_value"}',
},
{
"column1": "value2",
"column2": 2,
"sometimes_object": '"string_value"',
},
],
"stream2": [
{
Expand Down
55 changes: 36 additions & 19 deletions tests/unit_tests/test_type_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from __future__ import annotations

import pytest
from airbyte.types import SQLTypeConversionError, SQLTypeConverter, _get_airbyte_type
from sqlalchemy import types
from airbyte.types import SQLTypeConverter, _get_airbyte_type


@pytest.mark.parametrize(
Expand Down Expand Up @@ -54,6 +54,7 @@
({"type": ["null", "array"], "items": {"type": "object"}}, types.JSON),
({"type": "object", "properties": {}}, types.JSON),
({"type": ["null", "object"], "properties": {}}, types.JSON),
({"type": ["null", "string", "object"], "properties": {}}, types.JSON),
# Malformed JSON schema seen in the wild:
({"type": "array", "items": {}}, types.JSON),
({"type": ["null", "array"], "items": {"items": {}}}, types.JSON),
Expand All @@ -66,22 +67,23 @@ def test_to_sql_type(json_schema_property_def, expected_sql_type):


@pytest.mark.parametrize(
"json_schema_property_def, expected_airbyte_type",
"json_schema_property_def, expected_airbyte_type, raises",
[
({"type": "string"}, "string"),
({"type": ["boolean", "null"]}, "boolean"),
({"type": ["null", "boolean"]}, "boolean"),
({"type": "string"}, "string"),
({"type": ["null", "string"]}, "string"),
({"type": "boolean"}, "boolean"),
({"type": "string", "format": "date"}, "date"),
({"type": "string"}, "string", None),
({"type": ["boolean", "null"]}, "boolean", None),
({"type": ["null", "boolean"]}, "boolean", None),
({"type": "string"}, "string", None),
({"type": ["null", "string"]}, "string", None),
({"type": "boolean"}, "boolean", None),
({"type": "string", "format": "date"}, "date", None),
(
{
"type": "string",
"format": "date-time",
"airbyte_type": "timestamp_without_timezone",
},
"timestamp_without_timezone",
None,
),
(
{
Expand All @@ -90,6 +92,7 @@ def test_to_sql_type(json_schema_property_def, expected_sql_type):
"airbyte_type": "timestamp_with_timezone",
},
"timestamp_with_timezone",
None,
),
(
{
Expand All @@ -98,26 +101,40 @@ def test_to_sql_type(json_schema_property_def, expected_sql_type):
"airbyte_type": "time_without_timezone",
},
"time_without_timezone",
None,
),
(
{"type": "string", "format": "time", "airbyte_type": "time_with_timezone"},
"time_with_timezone",
None,
),
({"type": "integer"}, "integer"),
({"type": "number", "airbyte_type": "integer"}, "integer"),
({"type": "number"}, "number"),
({"type": "integer"}, "integer", None),
({"type": "number", "airbyte_type": "integer"}, "integer", None),
({"type": "number"}, "number", None),
# Array type:
({"type": "array"}, "array"),
({"type": "array", "items": {"type": "object"}}, "array"),
({"type": ["null", "array"], "items": {"type": "object"}}, "array"),
({"type": "array"}, "array", None),
({"type": "array", "items": {"type": "object"}}, "array", None),
({"type": ["null", "array"], "items": {"type": "object"}}, "array", None),
# Object type:
({"type": "object"}, "object"),
({"type": "object"}, "object", None),
({"type": ["null", "object", "string"]}, None, SQLTypeConversionError),
({"type": ["not-a-type"]}, None, SQLTypeConversionError),
({"tyyyype": ["not-a-type"]}, None, SQLTypeConversionError),
# Malformed JSON schema seen in the wild:
({"type": "array", "items": {"items": {}}}, "array"),
({"type": ["null", "array"], "items": {"items": {}}}, "array"),
({"type": "array", "items": {"items": {}}}, "array", None),
({"type": ["null", "array"], "items": {"items": {}}}, "array", None),
],
)
def test_to_airbyte_type(json_schema_property_def, expected_airbyte_type):
def test_to_airbyte_type(
json_schema_property_def,
expected_airbyte_type: str,
raises: type[Exception] | None,
):
if raises:
with pytest.raises(raises):
_get_airbyte_type(json_schema_property_def)
return

airbyte_type, _ = _get_airbyte_type(json_schema_property_def)
assert airbyte_type == expected_airbyte_type

Expand Down
Loading