Skip to content

Commit 1872909

Browse files
author
Lingling Peng
committed
updated dmge to use get_validation_rule_based_fields_no_explicit_type
1 parent fe48b08 commit 1872909

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

tests/unit/synapseclient/extensions/conftest.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@ def helpers():
5858

5959
@pytest.fixture(name="dmge", scope="function")
6060
def DMGE(helpers: Helpers) -> DataModelGraphExplorer:
61-
"""Fixture to instantiate a DataModelGraphExplorer object."""
62-
dmge = helpers.get_data_model_graph_explorer(path="example.model.jsonld")
63-
return dmge
64-
65-
66-
@pytest.fixture(name="dmge_column_type", scope="function")
67-
def DMGE_column_type(helpers: Helpers) -> DataModelGraphExplorer:
6861
"""Fixture to instantiate a DataModelGraphExplorer object using the data model with column types"""
6962
dmge = helpers.get_data_model_graph_explorer(
7063
path="example.model.column_type_component.csv"

tests/unit/synapseclient/extensions/unit_test_create_json_schema.py

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def fixture_test_nodes(
125125

126126
@pytest.fixture(name="test_nodes_column_types")
127127
def fixture_test_nodes_column_types(
128-
dmge_column_type: DataModelGraphExplorer,
128+
dmge: DataModelGraphExplorer,
129129
):
130130
"""Yields dict of Nodes"""
131131
nodes = [
@@ -147,16 +147,14 @@ def fixture_test_nodes_column_types(
147147
"ListInRange",
148148
]
149149
nodes = {
150-
node: TraversalNode(
151-
node, "JSONSchemaComponent", dmge_column_type, logger=Mock()
152-
)
150+
node: TraversalNode(node, "JSONSchemaComponent", dmge, logger=Mock())
153151
for node in nodes
154152
}
155153
return nodes
156154

157155

158156
class TestJSONSchema:
159-
"""Tests for JSONSchema"""
157+
"""Tests for JSONSchema class - validates JSON schema object creation and manipulation."""
160158

161159
def test_init(self) -> None:
162160
"""Test the JSONSchema.init method"""
@@ -210,7 +208,12 @@ def test_add_to_all_of_list(self) -> None:
210208
assert schema.all_of == [{"if": {}, "then": {}}, {"if2": {}, "then2": {}}]
211209

212210
def test_update_property(self) -> None:
213-
"""Test the JSONSchema.update_property method"""
211+
"""
212+
Test JSONSchema.update_property method.
213+
214+
Verifies that properties can be added and updated in the schema's
215+
properties dictionary, maintaining existing properties while adding new ones.
216+
"""
214217
# GIVEN a JSONSchema instance
215218
schema = JSONSchema()
216219
# WHEN updating the properties dict
@@ -226,22 +229,22 @@ def test_update_property(self) -> None:
226229
@pytest.mark.parametrize(
227230
"node_name, expected_type, expected_is_array, expected_min, expected_max, expected_pattern, expected_format",
228231
[
229-
# Node with no validation rules - all constraint fields should be None/False
232+
# Node with no columnType - all constraint fields should be None/False
230233
("NoRules", None, False, None, None, None, None),
231-
# Node with "str" validation rule - type remains None, constraints not set in Node initialization
232-
("String", None, False, None, None, None, None),
233-
# Node with "list" validation rule - is_array is set to True, other fields remain None
234+
# Node with columnType "string" - type is set to STRING via columnType
235+
("String", JSONSchemaType.STRING, False, None, None, None, None),
236+
# Node with "list" validation rule - is_array is set to True, type remains None (no columnType)
234237
("List", None, True, None, None, None, None),
235-
# Node with both "list" and "str" validation rules - is_array is True, type remains None
236-
("ListString", None, True, None, None, None, None),
237-
# Node with "inRange 50 100" validation rule - minimum and maximum are extracted and set
238-
("InRange", None, False, 50, 100, None, None),
239-
# Node with "regex search [a-f]" validation rule - pattern is extracted and set
240-
("Regex", None, False, None, None, "[a-f]", None),
241-
# Node with "date" validation rule - format is set to JSONSchemaFormat.DATE
242-
("Date", None, False, None, None, None, JSONSchemaFormat.DATE),
243-
# Node with "url" validation rule - format is set to JSONSchemaFormat.URI
244-
("URL", None, False, None, None, None, JSONSchemaFormat.URI),
238+
# Node with both "list" validation rules and columnType "string" - is_array is True, type is STRING
239+
("ListString", JSONSchemaType.STRING, True, None, None, None, None),
240+
# Node with "inRange 50 100" validation rule and columnType "number" - min/max are set, type is NUMBER
241+
("InRange", JSONSchemaType.NUMBER, False, 50, 100, None, None),
242+
# Node with "regex search [a-f]" validation rule and columnType "string" - pattern is set, type is STRING
243+
("Regex", JSONSchemaType.STRING, False, None, None, "[a-f]", None),
244+
# Node with "date" validation rule and columnType "string" - format is set to DATE, type is STRING
245+
("Date", JSONSchemaType.STRING, False, None, None, None, JSONSchemaFormat.DATE),
246+
# Node with "url" validation rule and columnType "string" - format is set to URI, type is STRING
247+
("URL", JSONSchemaType.STRING, False, None, None, None, JSONSchemaFormat.URI),
245248
],
246249
ids=["None", "String", "List", "ListString", "InRange", "Regex", "Date", "URI"],
247250
)
@@ -255,7 +258,17 @@ def test_node_init(
255258
expected_format: Optional[JSONSchemaFormat],
256259
test_nodes: dict[str, TraversalNode],
257260
) -> None:
258-
"""Tests for Node class"""
261+
"""
262+
Tests for TraversalNode class initialization.
263+
264+
Verifies that TraversalNode objects are correctly initialized with:
265+
- Types derived from columnType attribute in the data model
266+
- Validation constraints extracted from validation rules (format, pattern, min/max, array flag)
267+
- Proper combination of columnType and validation rule parsing
268+
269+
The type property comes from the columnType field, while constraints
270+
come from parsing validation rules like "str", "inRange", "regex", etc.
271+
"""
259272
node = test_nodes[node_name]
260273
assert node.type == expected_type
261274
assert node.format == expected_format
@@ -687,7 +700,7 @@ def test_create_json_schema_with_no_column_type(
687700

688701

689702
def test_create_json_schema_with_column_type(
690-
dmge_column_type: DataModelGraphExplorer, test_directory: str
703+
dmge: DataModelGraphExplorer, test_directory: str
691704
) -> None:
692705
"""
693706
Tests for JSONSchemaGenerator.create_json_schema
@@ -699,7 +712,7 @@ def test_create_json_schema_with_column_type(
699712

700713
logger = logging.getLogger(__name__)
701714
create_json_schema(
702-
dmge=dmge_column_type,
715+
dmge=dmge,
703716
datatype=datatype,
704717
schema_name=f"{datatype}_validation",
705718
schema_path=test_path,
@@ -807,8 +820,10 @@ def test_write_data_model_with_schema_path(test_directory: str) -> None:
807820

808821
def test_write_data_model_with_name_and_jsonld_path(test_directory: str) -> None:
809822
"""
810-
Test for _write_data_model with a name and the data model path used to create it.
811-
The name of the file should be "<jsonld_path_prefix>.<name>.schema.json"
823+
Test _write_data_model with a name and JSONLD path.
824+
825+
When provided with a name and jsonld_path, the function should create
826+
a schema file with the format: "<jsonld_path_prefix>.<name>.schema.json"
812827
"""
813828
json_ld_path = os.path.join(test_directory, "fake_model.jsonld")
814829
logger = Mock()
@@ -826,8 +841,10 @@ def test_write_data_model_with_name_and_jsonld_path(test_directory: str) -> None
826841

827842
def test_write_data_model_exception() -> None:
828843
"""
829-
Test for _write_data_model where neither the path, the name, or JSONLD path are provided.
830-
This should return a ValueError
844+
Test _write_data_model error handling.
845+
846+
When neither schema_path nor (name + jsonld_path) are provided,
847+
the function should raise a ValueError.
831848
"""
832849
with pytest.raises(ValueError):
833850
_write_data_model(json_schema_dict={}, logger=Mock())
@@ -1032,8 +1049,8 @@ def test_set_conditional_dependencies(
10321049
JSONSchema(
10331050
properties={
10341051
"String": {
1035-
"not": {"type": "null"},
10361052
"description": "TBD",
1053+
"type": "string",
10371054
"title": "String",
10381055
}
10391056
},
@@ -1130,7 +1147,7 @@ def test_create_enum_array_property(
11301147
[None, [], [None], ["x"]],
11311148
["x"],
11321149
),
1133-
# # If item_type is given, it is set in the schema
1150+
# If item_type is given, it is set in the schema
11341151
(
11351152
"ListString",
11361153
{

0 commit comments

Comments
 (0)