From 1736b514ac118044093d22a57bc201948ac26a22 Mon Sep 17 00:00:00 2001 From: Tien Tong Date: Thu, 12 Dec 2024 16:31:23 -0500 Subject: [PATCH] add unit test for cubids bids-version --- cubids/cli.py | 16 ++++++------ cubids/cubids.py | 5 ++-- cubids/tests/test_bond.py | 52 ++++++++++++++++++++++++++++++++++++++- cubids/validator.py | 3 ++- docs/installation.rst | 2 +- pyproject.toml | 1 + 6 files changed, 66 insertions(+), 13 deletions(-) diff --git a/cubids/cli.py b/cubids/cli.py index d2f8a920..cf48cf9a 100644 --- a/cubids/cli.py +++ b/cubids/cli.py @@ -43,7 +43,7 @@ def _parse_validate(): type=PathExists, action="store", help=( - "the root of a BIDS dataset. It should contain " + "The root of a BIDS dataset. It should contain " "sub-X directories and dataset_description.json" ), ) @@ -119,7 +119,7 @@ def _parse_bids_version(): type=PathExists, action="store", help=( - "the root of a BIDS dataset. It should contain " + "The root of a BIDS dataset. It should contain " "sub-X directories and dataset_description.json" ), ) @@ -188,7 +188,7 @@ def _parse_group(): type=PathExists, action="store", help=( - "the root of a BIDS dataset. It should contain " + "The root of a BIDS dataset. It should contain " "sub-X directories and dataset_description.json" ), ) @@ -255,7 +255,7 @@ def _parse_apply(): type=PathExists, action="store", help=( - "the root of a BIDS dataset. It should contain " + "The root of a BIDS dataset. It should contain " "sub-X directories and dataset_description.json" ), ) @@ -351,7 +351,7 @@ def _parse_datalad_save(): type=PathExists, action="store", help=( - "the root of a BIDS dataset. It should contain " + "The root of a BIDS dataset. It should contain " "sub-X directories and dataset_description.json" ), ) @@ -393,7 +393,7 @@ def _parse_undo(): type=PathExists, action="store", help=( - "the root of a BIDS dataset. It should contain " + "The root of a BIDS dataset. It should contain " "sub-X directories and dataset_description.json" ), ) @@ -617,7 +617,7 @@ def _parse_remove_metadata_fields(): type=PathExists, action="store", help=( - "the root of a BIDS dataset. It should contain " + "The root of a BIDS dataset. It should contain " "sub-X directories and dataset_description.json" ), ) @@ -663,7 +663,7 @@ def _parse_print_metadata_fields(): type=PathExists, action="store", help=( - "the root of a BIDS dataset. It should contain " + "The root of a BIDS dataset. It should contain " "sub-X directories and dataset_description.json" ), ) diff --git a/cubids/cubids.py b/cubids/cubids.py index 27f632e3..81781596 100644 --- a/cubids/cubids.py +++ b/cubids/cubids.py @@ -1346,9 +1346,10 @@ def get_all_metadata_fields(self): metadata = json.loads(content) found_fields.update(metadata.keys()) except json.JSONDecodeError as e: - print(f"Error decoding JSON in {json_file}: {e}") + warnings.warn(f"Error decoding JSON in {json_file}: {e}") except Exception as e: - print(f"Unexpected error with file {json_file}: {e}") + warnings.warn(f"Unexpected error with file {json_file}: {e}") + return sorted(found_fields) def remove_metadata_fields(self, fields_to_remove): diff --git a/cubids/tests/test_bond.py b/cubids/tests/test_bond.py index 28211cc2..068ee4fd 100644 --- a/cubids/tests/test_bond.py +++ b/cubids/tests/test_bond.py @@ -9,6 +9,7 @@ import numpy as np import pandas as pd import pytest +from packaging.version import Version from cubids.cubids import CuBIDS from cubids.metadata_merge import merge_json_into_json, merge_without_overwrite @@ -22,7 +23,15 @@ file_hash, get_data, ) -from cubids.validator import build_validator_call, parse_validator_output, run_validator +from cubids.validator import ( + build_validator_call, + parse_validator_output, + run_validator, + get_bids_validator_version, + extract_summary_info, + update_dataset_description, + bids_validator_version, +) COMPLETE_KEY_GROUPS = [ "acquisition-HASC55AP_datatype-dwi_suffix-dwi", @@ -1028,6 +1037,47 @@ def test_validator(tmp_path): assert isinstance(parsed, pd.DataFrame) +def bids_validator_version(tmp_path): + """Test validator.bids_validator_version.""" + # Get the BIDS validator version + validator_version = get_bids_validator_version() + # Extract schemaVersion + summary_info = extract_summary_info(decoded) + + +def test_bids_version(tmp_path): + """Test workflows.bids_version.""" + data_root = get_data(tmp_path) + bids_dir = Path(data_root) / "complete" + + # Ensure the test directory exists + assert bids_dir.exists() + + # test the validator in valid dataset + call = build_validator_call(bids_dir) + ret = run_validator(call) + + assert ret.returncode == 0 + + decoded = ret.stdout.decode("UTF-8") + + # Get the BIDS validator version + validator_version = Version(get_bids_validator_version()["ValidatorVersion"]) + # Extract schemaVersion + schema_version = Version(extract_summary_info(decoded)["SchemaVersion"]) + + # Set baseline versions to compare against + min_validator_version = Version("2.0.0") + min_schema_version = Version("0.11.3") + + assert ( + validator_version >= min_validator_version + ), f"Validator version {validator_version} is less than minimum {min_validator_version}" + assert ( + schema_version >= min_schema_version + ), f"Schema version {schema_version} is less than minimum {min_schema_version}" + + def test_docker(): """Verify that docker is installed and the user has permission to run docker images. diff --git a/cubids/validator.py b/cubids/validator.py index a7225ba0..a4feeba5 100644 --- a/cubids/validator.py +++ b/cubids/validator.py @@ -7,6 +7,7 @@ import pathlib import re import subprocess +import warnings import pandas as pd @@ -249,7 +250,7 @@ def update_dataset_description(path, new_info): ) print("Changes saved with DataLad.") except subprocess.CalledProcessError as e: - print(f"Error running DataLad save: {e}") + warnings.warn(f"Error running DataLad save: {e}") def bids_validator_version(output, path, write=False): diff --git a/docs/installation.rst b/docs/installation.rst index b02e734e..b6ebda3b 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -51,7 +51,7 @@ We can accomplish this using the following command: $ conda install deno -The new schema ``bids-validator`` doesn't need to be installed +The new schema-based ``bids-validator`` doesn't need to be installed and will be implemented automatically when `cubids validate` is called diff --git a/pyproject.toml b/pyproject.toml index 6e3c151c..79d8e1f6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -86,6 +86,7 @@ cubids-copy-exemplars = "cubids.cli:_enter_copy_exemplars" cubids-undo = "cubids.cli:_enter_undo" bids-sidecar-merge = "cubids.cli:_enter_bids_sidecar_merge" cubids-validate = "cubids.cli:_enter_validate" +cubids-bids-version = "cubids.cli:_enter_bids_version" cubids-datalad-save = "cubids.cli:_enter_datalad_save" cubids-print-metadata-fields = "cubids.cli:_enter_print_metadata_fields" cubids-remove-metadata-fields = "cubids.cli:_enter_remove_metadata_fields"