From 829504781f11fdfe9f0f940e78524a83d94de363 Mon Sep 17 00:00:00 2001 From: Ainur Karimov Date: Fri, 20 Dec 2024 08:58:00 +0300 Subject: [PATCH] argparse tests --- .../rtk/lungs_description_metrics.py | 2 +- .../lightning_datamodules/test_rtk.py | 9 +++-- tests/unit/rtk/test_hemorrhage_contrast.py | 34 +++++++++++++++++++ tests/unit/rtk/test_lung_description_rtk.py | 22 ++++++++++++ .../{test_metrics.py => test_rtk_metrics.py} | 0 5 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 tests/unit/rtk/test_hemorrhage_contrast.py create mode 100644 tests/unit/rtk/test_lung_description_rtk.py rename tests/unit/rtk/{test_metrics.py => test_rtk_metrics.py} (100%) diff --git a/innofw/utils/data_utils/rtk/lungs_description_metrics.py b/innofw/utils/data_utils/rtk/lungs_description_metrics.py index 18108ccb..441f8079 100644 --- a/innofw/utils/data_utils/rtk/lungs_description_metrics.py +++ b/innofw/utils/data_utils/rtk/lungs_description_metrics.py @@ -68,7 +68,7 @@ def main(): """Main module function""" parser = ArgumentParser( prog="innofw", - description="A tool to determine lung decision from a description", + description="Tool to calc lung decision metrcis", formatter_class=ArgumentDefaultsHelpFormatter, ) setup_parser(parser) diff --git a/tests/unit/datamodules/lightning_datamodules/test_rtk.py b/tests/unit/datamodules/lightning_datamodules/test_rtk.py index 62045787..0ef14112 100644 --- a/tests/unit/datamodules/lightning_datamodules/test_rtk.py +++ b/tests/unit/datamodules/lightning_datamodules/test_rtk.py @@ -15,10 +15,13 @@ ) from innofw.core.datasets.coco_rtk import DicomCocoDatasetRTK from innofw.utils.data_utils.preprocessing.CT_hemorrhage_contrast_rtk import ( - hemorrhage_contrast, transform as resize_transform + hemorrhage_contrast, + transform as resize_transform, +) +from innofw.utils.data_utils.preprocessing.CT_hemorrhage_contrast_metrics import ( + hemorrhage_contrast_metrics, ) from innofw.utils.data_utils.rtk.CT_hemorrhage_metrics import process_metrics -from innofw.utils.data_utils.preprocessing.CT_hemorrhage_contrast_metrics import hemorrhage_contrast_metrics rtk_complex = "https://api.blackhole.ai.innopolis.university/public-datasets/rtk/complex_infer.zip" rtk_segm = "https://api.blackhole.ai.innopolis.university/public-datasets/rtk/infer.zip" @@ -133,7 +136,7 @@ def test_segm_detection_pipeline_metrics(mock_show, tmp_path_factory, task): out_dir = tmp_path_factory.mktemp("out") for i in range(samples_number): - random_numpy = np.random.randint(0, 1, [256,256,1]) + random_numpy = np.random.randint(0, 1, [256, 256, 1]) np.save(os.path.join(out_dir, f"{i}.npy"), random_numpy) process_metrics(input_path=target_dir, output_folder=out_dir) diff --git a/tests/unit/rtk/test_hemorrhage_contrast.py b/tests/unit/rtk/test_hemorrhage_contrast.py new file mode 100644 index 00000000..f0415584 --- /dev/null +++ b/tests/unit/rtk/test_hemorrhage_contrast.py @@ -0,0 +1,34 @@ +from unittest.mock import patch +import argparse + +from innofw.utils.data_utils.preprocessing.CT_hemorrhage_contrast_rtk import ( + callback, + default_output_path, + setup_parser, +) + + +def test_setup_parser(): + parser = argparse.ArgumentParser() + setup_parser(parser) + args = parser.parse_args(["-i", "input.txt"]) + assert args.input == "input.txt" + assert args.output is None + + args = parser.parse_args(["-i", "input.txt", "-o", "output.txt"]) + assert args.input == "input.txt" + assert args.output == "output.txt" + + +def test_default_output_path(): + # Mock get_log_dir to return a predictable value + with patch("innofw.utils.getters.get_log_dir", return_value="mock_log_dir"): + output_path = default_output_path() + assert output_path == "mock_log_dir" + + +@patch("innofw.utils.data_utils.preprocessing.CT_hemorrhage_contrast_rtk.hemorrhage_contrast") +def test_callback_success(mock_contrast): + arguments = argparse.Namespace(input="input.txt", output="output.txt") + callback(arguments) + mock_contrast.assert_called_once_with("input.txt", "output.txt") diff --git a/tests/unit/rtk/test_lung_description_rtk.py b/tests/unit/rtk/test_lung_description_rtk.py new file mode 100644 index 00000000..68c05a1f --- /dev/null +++ b/tests/unit/rtk/test_lung_description_rtk.py @@ -0,0 +1,22 @@ +import argparse +from unittest.mock import patch + +from innofw.utils.data_utils.rtk.lungs_description_metrics import ( + callback, + setup_parser, +) + + +def test_setup_parser(): + parser = argparse.ArgumentParser() + setup_parser(parser) + args = parser.parse_args(["-i", "input.txt", "-o", "output.txt"]) + assert args.input == "input.txt" + assert args.output == "output.txt" + + +@patch("innofw.utils.data_utils.rtk.lungs_description_metrics.calculate_lungs_metrics") +def test_callback_success(mock_calc): + arguments = argparse.Namespace(input="input.txt", output="output.txt") + callback(arguments) + mock_calc.assert_called_once_with("input.txt", "output.txt") diff --git a/tests/unit/rtk/test_metrics.py b/tests/unit/rtk/test_rtk_metrics.py similarity index 100% rename from tests/unit/rtk/test_metrics.py rename to tests/unit/rtk/test_rtk_metrics.py