-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_connector_config_schema_generator.py
More file actions
106 lines (85 loc) · 3.8 KB
/
test_connector_config_schema_generator.py
File metadata and controls
106 lines (85 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import unittest
from unittest.mock import MagicMock
from pyoaev.configuration.connector_config_schema_generator import (
ConnectorConfigSchemaGenerator,
)
class TestConnectorConfigSchemaGenerator(unittest.TestCase):
def test_dereference_schema_resolves_internal_refs(self):
schema = {
"$defs": {
"Item": {
"type": "object",
"properties": {"value": {"type": "string"}},
}
},
"type": "object",
"properties": {
"item": {"$ref": "#/$defs/Item"},
"items": {"type": "array", "items": [{"$ref": "#/$defs/Item"}]},
},
}
resolved = ConnectorConfigSchemaGenerator.dereference_schema(schema)
self.assertEqual(resolved["properties"]["item"]["type"], "object")
self.assertIn("value", resolved["properties"]["item"]["properties"])
self.assertEqual(
resolved["properties"]["items"]["items"][0]["properties"]["value"]["type"],
"string",
)
def test_dereference_schema_rejects_unsupported_refs(self):
with self.assertRaises(ValueError):
ConnectorConfigSchemaGenerator.dereference_schema(
{"$ref": "external://schema"}
)
def test_flatten_config_loader_schema_and_filter_schema(self):
root_schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "config.schema.json",
"additionalProperties": False,
"properties": {
"connector": {
"properties": {
"name": {"type": "string", "title": "Name"},
"id": {"type": "string"},
},
"required": ["name"],
}
},
}
flattened = ConnectorConfigSchemaGenerator.flatten_config_loader_schema(
root_schema
)
flattened["properties"]["CONNECTOR_ID"] = {"type": "string"}
flattened["required"].append("CONNECTOR_ID")
filtered = ConnectorConfigSchemaGenerator.filter_schema(flattened)
self.assertEqual(filtered["additionalProperties"], False)
self.assertIn("CONNECTOR_NAME", filtered["properties"])
self.assertNotIn("title", filtered["properties"]["CONNECTOR_NAME"])
self.assertIn("CONNECTOR_NAME", filtered["required"])
self.assertNotIn("CONNECTOR_ID", filtered["properties"])
self.assertNotIn("CONNECTOR_ID", filtered["required"])
def test_flatten_config_loader_schema_defaults_additional_properties_to_true(self):
root_schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "config.schema.json",
"properties": {"app": {"properties": {}, "required": []}},
}
flattened = ConnectorConfigSchemaGenerator.flatten_config_loader_schema(
root_schema
)
self.assertTrue(flattened["additionalProperties"])
def test_nullable_schema_returns_null_when_inner_schema_is_null(self):
generator = ConnectorConfigSchemaGenerator(by_alias=True)
generator.generate_inner = MagicMock(return_value={"type": "null"})
result = generator.nullable_schema(
{"type": "nullable", "schema": {"type": "str"}}
)
self.assertEqual(result, {"type": "null"})
def test_nullable_schema_returns_inner_schema_when_not_null(self):
generator = ConnectorConfigSchemaGenerator(by_alias=True)
generator.generate_inner = MagicMock(return_value={"type": "string"})
result = generator.nullable_schema(
{"type": "nullable", "schema": {"type": "str"}}
)
self.assertEqual(result, {"type": "string"})
if __name__ == "__main__":
unittest.main()