Skip to content

Commit da2e667

Browse files
committed
update tests
1 parent e4b915e commit da2e667

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

tests/unit/synapseclient/extensions/unit_test_create_json_schema.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@
66
import json
77
import logging
88
import os
9+
import tempfile
10+
from time import sleep
911
from typing import Any, Optional
1012
from unittest import mock
1113
from unittest.mock import Mock
1214

15+
import pandas as pd
1316
import pytest
1417
from jsonschema import Draft7Validator
1518
from jsonschema.exceptions import ValidationError
1619

1720
from synapseclient.extensions.curator.schema_generation import (
1821
AtomicColumnType,
1922
ColumnType,
23+
DataModelGraph,
2024
DataModelGraphExplorer,
25+
DataModelParser,
2126
GraphTraversalState,
2227
JSONSchema,
2328
JSONSchemaFormat,
@@ -408,6 +413,48 @@ def test_node_init(
408413
assert warning_message in test_nodes[node_name].logger.mock_calls[0][1][0]
409414

410415

416+
def test_invalid_regex_columntype_traversalnode(
417+
helpers,
418+
) -> None:
419+
"""
420+
Tests for TraversalNode class initialization.
421+
422+
Verifies that when TransversalNode objects are initialized with a patter specified and an incompatible column type, a ValueError is raised.
423+
"""
424+
node = "Check Regex Single"
425+
426+
path_to_data_model = helpers.get_schema_file_path("data_models/example.model.csv")
427+
428+
fullpath = helpers.get_schema_file_path(path_to_data_model)
429+
430+
# Instantiate DataModelParser
431+
data_model_parser = DataModelParser(path_to_data_model=fullpath, logger=Mock())
432+
433+
# Parse Model
434+
parsed_data_model = data_model_parser.parse_model()
435+
436+
# Change column type to imcompatible type
437+
parsed_data_model[node]["Relationships"]["ColumnType"] = "integer"
438+
439+
# Instantiate DataModelGraph
440+
data_model_grapher = DataModelGraph(
441+
parsed_data_model, data_model_labels="class_label", logger=Mock()
442+
)
443+
444+
# Generate graph
445+
graph_data_model = data_model_grapher.graph
446+
447+
# Instantiate DataModelGraphExplorer
448+
dmge = DataModelGraphExplorer(graph_data_model, logger=Mock())
449+
450+
# A value error should be raised when using pattern specification with non-string column type
451+
error_message = "Column type must be set to 'string' to use column pattern specification for regex validation."
452+
with pytest.raises(ValueError, match=error_message):
453+
node = TraversalNode(
454+
node.replace(" ", ""), "JSONSchemaComponent", dmge, logger=Mock()
455+
)
456+
457+
411458
@pytest.mark.parametrize(
412459
"node_name, expected_node_type, expected_max, expected_min",
413460
[

tests/unit/synapseclient/extensions/unit_test_data_model_graph_explorer.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ def test_get_node_column_type(
6969
assert dmge.get_node_column_type(node_label) == column_type
7070

7171

72+
@pytest.mark.parametrize(
73+
"node_label, pattern",
74+
[
75+
("CheckRegexSingle", "[a-b]"),
76+
("CheckRegexFormat", "^[a-b]"),
77+
("CheckRegexInteger", None),
78+
],
79+
)
80+
def test_get_node_pattern(
81+
dmge: DataModelGraphExplorer, node_label: str, pattern: str
82+
) -> None:
83+
assert dmge.get_node_column_pattern(node_label) == pattern
84+
85+
7286
@pytest.mark.parametrize(
7387
"node_label, column_type",
7488
[("String", None), ("Date", JSONSchemaFormat.DATE), ("URL", JSONSchemaFormat.URI)],

tests/unit/synapseclient/extensions/unit_test_data_model_parser.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import tempfile
2+
from time import sleep
13
from typing import Any, Union
24

35
import numpy as np
6+
import pandas as pd
47
import pytest
58

69
from synapseclient.extensions.curator.schema_generation import (
@@ -87,6 +90,23 @@ def test_parse_format(
8790
) -> None:
8891
assert csv_dmp.parse_format(attribute_dict) == expected_dict
8992

93+
@pytest.mark.parametrize(
94+
"attribute_dict, expected_dict",
95+
[
96+
({}, {}),
97+
({"Pattern": np.nan}, {}),
98+
({"Pattern": "^[a-b]"}, {"Pattern": "^[a-b]"}),
99+
({"Pattern": " [a-b] "}, {"Pattern": "[a-b]"}),
100+
],
101+
)
102+
def test_parse_regex_pattern(
103+
self,
104+
csv_dmp: DataModelCSVParser,
105+
attribute_dict: dict[str, Any],
106+
expected_dict: dict[str, str],
107+
) -> None:
108+
assert csv_dmp.parse_pattern(attribute_dict) == expected_dict
109+
90110
@pytest.mark.parametrize(
91111
"attribute_dict, relationship, expected_dict",
92112
[
@@ -132,7 +152,6 @@ def test_parse_minimum_maximum(
132152
== expected_dict
133153
)
134154

135-
136155
class TestDataModelJsonLdParser:
137156
def test_gather_jsonld_attributes_relationships(
138157
self,
@@ -143,8 +162,6 @@ def test_gather_jsonld_attributes_relationships(
143162
assert csv_dmp.parse_minimum_maximum(attribute_dict, "Minimum") == expected_dict
144163
assert csv_dmp.parse_minimum_maximum(attribute_dict, "Maximum") == expected_dict
145164

146-
147-
class TestDataModelJsonLdParser:
148165
def test_gather_jsonld_attributes_relationships(
149166
self,
150167
helpers,

tests/unit/synapseclient/extensions/unit_test_data_model_relationships.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def test_retrieve_rel_headers_dict(self, dmr: DataModelRelationships, edge: bool
7171
"id": "Source",
7272
"maximum": "Maximum",
7373
"minimum": "Minimum",
74+
"pattern": "Pattern",
7475
}
7576

7677
def test_get_relationship_value(self, dmr: DataModelRelationships) -> None:

0 commit comments

Comments
 (0)