Skip to content

Commit c335668

Browse files
authored
Test JSONSchema resolver (#3223)
closes #2722 I don't love the current structure. There is a number of improvements that can be made: JsonSchemaResolver can be refactored to have the following signature: def __init__(schemas: Mapping[str, any], refs: Mapping[str, any]) # schemas are all json files in `schemas/`, refs are all json files in `schemas/shared/` Then we can thoroughly test the resolving behavior in JsonSchemaResolver independently from the logic for where we actually load schemas from. I was tempted to make this refactor now but I'd rather cover more ground with tests then revisit. I created an issue to track this: #3222
1 parent f942f7f commit c335668

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

airbyte-cdk/python/.coveragerc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[run]
2+
omit =
3+
# omit the models package
4+
airbyte_cdk/models/*

airbyte-cdk/python/unit_tests/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2020 Airbyte
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
24+
import json
25+
import os
26+
import shutil
27+
import sys
28+
from collections import Mapping
29+
from pathlib import Path
30+
31+
from airbyte_cdk.base_python.schema_helpers import ResourceSchemaLoader
32+
from pytest import fixture
33+
34+
MODULE = sys.modules[__name__]
35+
MODULE_NAME = MODULE.__name__.split(".")[0]
36+
SCHEMAS_ROOT = "/".join(os.path.abspath(MODULE.__file__).split("/")[:-1]) / Path("schemas")
37+
38+
39+
# TODO (sherif) refactor ResourceSchemaLoader to completely separate the functionality for reading data from the package. See https://github.com/airbytehq/airbyte/issues/3222
40+
# and the functionality for resolving schemas. See https://github.com/airbytehq/airbyte/issues/3222
41+
@fixture(autouse=True, scope="session")
42+
def create_and_teardown_schemas_dir():
43+
os.mkdir(SCHEMAS_ROOT)
44+
os.mkdir(SCHEMAS_ROOT / "shared")
45+
yield
46+
shutil.rmtree(SCHEMAS_ROOT)
47+
48+
49+
def create_schema(name: str, content: Mapping):
50+
with open(SCHEMAS_ROOT / f"{name}.json", "w") as f:
51+
f.write(json.dumps(content))
52+
53+
54+
class TestResourceSchemaLoader:
55+
# Test that a simple schema is loaded correctly
56+
@staticmethod
57+
def test_inline_schema_resolves():
58+
expected_schema = {
59+
"type": ["null", "object"],
60+
"properties": {
61+
"str": {"type": "string"},
62+
"int": {"type": "integer"},
63+
"obj": {"type": ["null", "object"], "properties": {"k1": {"type": "string"}}},
64+
},
65+
}
66+
67+
create_schema("simple_schema", expected_schema)
68+
resolver = ResourceSchemaLoader(MODULE_NAME)
69+
actual_schema = resolver.get_schema("simple_schema")
70+
assert actual_schema == expected_schema
71+
72+
@staticmethod
73+
def test_shared_schemas_resolves():
74+
expected_schema = {
75+
"type": ["null", "object"],
76+
"properties": {
77+
"str": {"type": "string"},
78+
"int": {"type": "integer"},
79+
"obj": {"type": ["null", "object"], "properties": {"k1": {"type": "string"}}},
80+
},
81+
}
82+
83+
partial_schema = {
84+
"type": ["null", "object"],
85+
"properties": {"str": {"type": "string"}, "int": {"type": "integer"}, "obj": {"$ref": "shared_schema.json"}},
86+
}
87+
88+
referenced_schema = {"type": ["null", "object"], "properties": {"k1": {"type": "string"}}}
89+
90+
create_schema("complex_schema", partial_schema)
91+
create_schema("shared/shared_schema", referenced_schema)
92+
93+
resolver = ResourceSchemaLoader(MODULE_NAME)
94+
95+
actual_schema = resolver.get_schema("complex_schema")
96+
assert actual_schema == expected_schema

0 commit comments

Comments
 (0)