From 108a562df1d7ae875a4c775fb1410cff4f868500 Mon Sep 17 00:00:00 2001 From: Adam Tyson Date: Mon, 22 Jul 2024 18:33:26 +0100 Subject: [PATCH] Update tests --- .../brainmapper/transform_widget.py | 2 +- .../test_brainmapper/test_transform_widget.py | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/brainglobe_utils/brainmapper/transform_widget.py b/brainglobe_utils/brainmapper/transform_widget.py index 51e5690..ba53060 100644 --- a/brainglobe_utils/brainmapper/transform_widget.py +++ b/brainglobe_utils/brainmapper/transform_widget.py @@ -282,7 +282,7 @@ def check_brainreg_directory(self): with open(self.paths.brainreg_metadata_file) as json_file: self.brainreg_metadata = json.load(json_file) - if not self.brainreg_metadata["atlas"]: + if "atlas" not in self.brainreg_metadata: self.display_brainreg_directory_warning() except FileNotFoundError: diff --git a/tests/tests/test_brainmapper/test_transform_widget.py b/tests/tests/test_brainmapper/test_transform_widget.py index f04a35a..3ab6460 100644 --- a/tests/tests/test_brainmapper/test_transform_widget.py +++ b/tests/tests/test_brainmapper/test_transform_widget.py @@ -1,3 +1,4 @@ +import json from pathlib import Path from typing import Any, Dict @@ -210,6 +211,73 @@ def test_transform_points_to_atlas_space( ) +@pytest.fixture +def random_json_path(tmp_path): + json_path = tmp_path / "random_json.json" + content = { + "name": "Pooh Bear", + "location": "100 acre wood", + "food": "Honey", + } + with open(json_path, "w") as f: + json.dump(content, f) + + return json_path + + +def test_check_brainreg_directory_correct_metadata( + mocker, transformation_widget_with_data +): + mock_method = mocker.patch.object( + transformation_widget_with_data, "display_brainreg_directory_warning" + ) + + transformation_widget_with_data.check_brainreg_directory() + mock_method.assert_not_called() + + +def test_check_brainreg_directory_random_data( + mocker, transformation_widget_with_data, random_json_path +): + mock_method = mocker.patch.object( + transformation_widget_with_data, "display_brainreg_directory_warning" + ) + transformation_widget_with_data.paths.brainreg_metadata_file = ( + random_json_path + ) + transformation_widget_with_data.check_brainreg_directory() + mock_method.assert_called_once() + + +def test_check_brainreg_directory_false_path( + mocker, transformation_widget_with_data +): + mock_method = mocker.patch.object( + transformation_widget_with_data, "display_brainreg_directory_warning" + ) + + transformation_widget_with_data.paths.brainreg_metadata_file = "/some/file" + transformation_widget_with_data.check_brainreg_directory() + mock_method.assert_called_once() + + +def test_display_brainreg_directory_warning_calls_display_info( + mocker, transformation_widget_with_napari_layers +): + mock_display_info = mocker.patch( + "brainglobe_utils.brainmapper.transform_widget.display_info" + ) + transformation_widget_with_napari_layers.display_brainreg_directory_warning() + + # Assert display_info was called once with the expected arguments + mock_display_info.assert_called_once_with( + transformation_widget_with_napari_layers, + "Not a brainreg directory", + "This directory does not appear to be a valid brainreg directory. " + "Please try loading another brainreg output directory.", + ) + + def test_analysis(transformation_widget_with_transformed_points): transformation_widget_with_transformed_points.analyse_points()