From 1b3d8ad5d1fa072539c9a184243c06e371e190d0 Mon Sep 17 00:00:00 2001 From: Bill Riehl Date: Thu, 14 Nov 2024 11:59:06 -0500 Subject: [PATCH] test that data type in manifest.json is a string --- .../individual_parsers.py | 8 +++++ .../test_individual_parsers.py | 29 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/staging_service/import_specifications/individual_parsers.py b/staging_service/import_specifications/individual_parsers.py index 389d0729..2e0d837a 100644 --- a/staging_service/import_specifications/individual_parsers.py +++ b/staging_service/import_specifications/individual_parsers.py @@ -449,5 +449,13 @@ def _parse_single_manifest_resource(resource: dict[str, Any], spcsrc: Specificat ) ) datatype = instructions[_DTS_INSTRUCTIONS_DATATYPE_KEY] + if not isinstance(datatype, str): + raise _ParseException( + Error( + ErrorType.PARSE_FAIL, + "Data type must be a string", + spcsrc + ) + ) parameters = frozendict(instructions[_DTS_INSTRUCTIONS_PARAMETERS_KEY]) return datatype, parameters diff --git a/tests/import_specifications/test_individual_parsers.py b/tests/import_specifications/test_individual_parsers.py index be3d0d43..0349a35a 100644 --- a/tests/import_specifications/test_individual_parsers.py +++ b/tests/import_specifications/test_individual_parsers.py @@ -889,7 +889,7 @@ def test_dts_manifest_file_is_directory(temp_dir: Generator[Path, None, None]): [], 1, "nope", None ] @mark.parametrize("bad_instruction", malformed_dict) -def test_dts_manifest_malformed_instructions(write_dts_manifest: Callable[[dict | list], Path], bad_instruction): +def test_dts_manifest_malformed_instructions(write_dts_manifest: Callable[[dict | list], Path], bad_instruction: list|int|str|None): manifest_file = write_dts_manifest({ "resources": [{ "id": "some_id", @@ -909,7 +909,7 @@ def test_dts_manifest_malformed_instructions(write_dts_manifest: Callable[[dict ) @mark.parametrize("bad_parameters", malformed_dict) -def test_dts_manifest_malformed_parameters(write_dts_manifest: Callable[[dict | list], Path], bad_parameters): +def test_dts_manifest_malformed_parameters(write_dts_manifest: Callable[[dict | list], Path], bad_parameters: list|int|str|None): manifest_file = write_dts_manifest({ "resources": [{ "id": "some_id", @@ -937,7 +937,7 @@ def test_dts_manifest_malformed_parameters(write_dts_manifest: Callable[[dict | ["data_type", "parameters"] ] @mark.parametrize("missing_keys", missing_key_cases) -def test_dts_manifest_missing_instruction_keys(write_dts_manifest: Callable[[dict | list], Path], missing_keys): +def test_dts_manifest_missing_instruction_keys(write_dts_manifest: Callable[[dict | list], Path], missing_keys: list[str]): instructions = { "data_type": "some_type", "parameters": { @@ -974,3 +974,26 @@ def test_dts_manifest_empty(write_dts_manifest: Callable[[dict | list], Path]): SpecificationSource(manifest_file) )] ) + +@mark.parametrize("non_str", [{"a": "b"}, ["a", "b"], 1, None]) +def test_dts_manifest_fail_data_type_not_str(write_dts_manifest: Callable[[dict | list], Path], non_str: dict|list|int|None): + manifest_file = write_dts_manifest({ + "resources": [{ + "id": "foo", + "name": "bar", + "path": "baz", + "format": "some_format", + "instructions": { + "data_type": non_str, + "parameters": {} + } + }] + }) + _dts_manifest_parse_fail( + manifest_file, + [Error( + ErrorType.PARSE_FAIL, + "Data type must be a string", + SpecificationSource(manifest_file) + )] + )