Skip to content

Commit 0b97f3b

Browse files
committed
add value error
1 parent bfd1711 commit 0b97f3b

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

synapseclient/extensions/curator/schema_generation.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5516,6 +5516,9 @@ def generate_jsonschema(
55165516
- A list of JSON schema dictionaries, each corresponding to a data type
55175517
- A list of file paths where the schemas were written
55185518
5519+
Raises:
5520+
ValueError: If a single output file is specified but multiple data types are requested.
5521+
55195522
Example: Using this function to generate JSON Schema files:
55205523
Generate schema for one datatype:
55215524
@@ -5560,15 +5563,19 @@ def generate_jsonschema(
55605563
path_to_data_model=data_model_source, logger=synapse_client.logger
55615564
)
55625565
parsed_data_model = data_model_parser.parse_model()
5563-
data_model_grapher = DataModelGraph(parsed_data_model)
5564-
graph_data_model = data_model_grapher.graph
5566+
data_model_graph = DataModelGraph(parsed_data_model)
5567+
graph_data_model = data_model_graph.graph
55655568
dmge = DataModelGraphExplorer(graph_data_model, logger=synapse_client.logger)
55665569

55675570
if output is not None and not output.endswith(".json"):
55685571
dirname = output
55695572
os.makedirs(dirname, exist_ok=True)
55705573
else:
55715574
dirname = "./"
5575+
if data_types is None or len(data_types) != 1:
5576+
raise ValueError(
5577+
"When specifying more than a single output file, don't provide a file path."
5578+
)
55725579

55735580
# Gets all data types if none are specified
55745581
if data_types is None or len(data_types) == 0:

tests/unit/synapseclient/extensions/unit_test_curator.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,3 +2333,36 @@ def test_generate_jsonschema_specify_file(self):
23332333
finally:
23342334
if os.path.isfile("./test.json"):
23352335
os.remove("./test.json")
2336+
2337+
def test_generate_jsonschema_exception_no_datatypes(self):
2338+
"""Test that an exception is raised when no datatypes are provided, and a JSON path is"""
2339+
try:
2340+
# GIVEN a CSV schema file
2341+
# WHEN I generate a schema with specific output file, and no datatype
2342+
# THEN a ValueError should be raised
2343+
with pytest.raises(ValueError):
2344+
generate_jsonschema(
2345+
data_model_source=self.test_schema_path,
2346+
output="./test.json",
2347+
synapse_client=self.syn,
2348+
)
2349+
finally:
2350+
if os.path.isfile("./test.json"):
2351+
os.remove("./test.json")
2352+
2353+
def test_generate_jsonschema_exception_multiple_datatypes(self):
2354+
"""Test that an exception is raised when multiple datatypes are provided, and a JSON path is"""
2355+
try:
2356+
# GIVEN a CSV schema file
2357+
# WHEN I generate a schema with specific output file, and no datatype
2358+
# THEN a ValueError should be raised
2359+
with pytest.raises(ValueError):
2360+
generate_jsonschema(
2361+
data_model_source=self.test_schema_path,
2362+
output="./test.json",
2363+
data_types=["Patient", "Biospecimen"],
2364+
synapse_client=self.syn,
2365+
)
2366+
finally:
2367+
if os.path.isfile("./test.json"):
2368+
os.remove("./test.json")

0 commit comments

Comments
 (0)