Skip to content

Commit

Permalink
Add tests for newly added '-json-metadata' flag. Update other tests t…
Browse files Browse the repository at this point in the history
…o be compatible with new 'json_metadata' param.
  • Loading branch information
valosekj committed Feb 21, 2024
1 parent 9541303 commit fe8a8c1
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions tests/test_create_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_create_json(tmp_path):
nifti_file.touch()

# Call the function with modified=True
update_json(str(nifti_file), "Test Rater")
update_json(str(nifti_file), "Test Rater", json_metadata=None)

# Check that the JSON file was created and contains the expected metadata
expected_metadata = {'SpatialReference': 'orig',
Expand All @@ -35,6 +35,38 @@ def test_create_json(tmp_path):
assert metadata == expected_metadata


def test_create_json_custom_metadata(tmp_path):
"""
Test that the function update_json() creates a JSON file with the expected metadata and includes custom metadata
provided by the user as a JSON file
"""
# Create a temporary file for testing
fname_label = "sub-001_ses-01_T1w_seg-manual.nii.gz"
nifti_file = tmp_path / fname_label
nifti_file.touch()

custom_metada = {'Name': 'sct_deepseg_sc',
'Author': "SCT v6.2",
'Date': "2024-02-21"}

# Call the function with modified=True
update_json(str(nifti_file), "Test Rater", json_metadata=custom_metada)

# Check that the JSON file was created and contains the expected metadata
expected_metadata = {'SpatialReference': 'orig',
'GeneratedBy': [{'Name': 'sct_deepseg_sc',
'Author': "SCT v6.2",
'Date': "2024-02-21"},
{'Name': 'Manual',
'Author': "Test Rater",
'Date': time.strftime('%Y-%m-%d %H:%M:%S')}]}
json_file = tmp_path / fname_label.replace(".nii.gz", ".json")
assert json_file.exists()
with open(str(json_file), "r") as f:
metadata = json.load(f)
assert metadata == expected_metadata


def test_update_json(tmp_path):
"""
Test that the function update_json() updates (appends to) the JSON file with the expected metadata.
Expand All @@ -52,7 +84,7 @@ def test_update_json(tmp_path):
'Date': "2023-01-01 00:00:00"}]}, f)

# Call the function with modified=True
update_json(str(nifti_file), "Test Rater 2")
update_json(str(nifti_file), "Test Rater 2", json_metadata=None)

# Check that the JSON file was created and contains the expected metadata
expected_metadata = {'SpatialReference': 'orig',
Expand Down

0 comments on commit fe8a8c1

Please sign in to comment.