Skip to content

Commit 718a91c

Browse files
committed
Added test for 'hres_ic.main()' function.
100% test coverage for hres_ic.py
1 parent 52bb15b commit 718a91c

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

src/hres_ic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,5 @@ def main():
133133
print("No need to swap out IC")
134134

135135
if __name__ == '__main__':
136-
main()
136+
main() # pragma: no cover
137137

tests/test_hres.py

+60-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from unittest.mock import patch
2+
from unittest.mock import patch, Mock
33
from pathlib import Path
44

55
# TODO: place ROSE DATA into a function (or an input argument) so it doesn't need to get called when importing the module
@@ -12,14 +12,16 @@
1212
srcpath = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'src') #To delete when src is a package
1313
sys.path.insert(0,srcpath) #To delete when src is a package
1414

15-
from hres_ic import get_start_time, replace_input_file_with_tmp_input_file, parse_arguments, set_replace_function
15+
from hres_ic import get_start_time, replace_input_file_with_tmp_input_file, parse_arguments, set_replace_function, main
1616

1717
del sys.path[0] #To delete when src is a package
1818

19+
# Test get_start_time
1920
def test_get_start_time():
2021
time = "199205041155"
2122
assert get_start_time(time) == "19920504T1155Z"
2223

24+
# Test replace_input_file_with_tmp_input_file
2325
def test_replace_input_file_with_tmp_input_file():
2426
tmppath = Path('example/of/.tmp/file.tmp')
2527
newpath = Path('example/of/.tmp/file')
@@ -35,6 +37,7 @@ def test_replace_input_file_with_tmp_input_file_fail():
3537
with pytest.raises(ValueError):
3638
replace_input_file_with_tmp_input_file(tmppath)
3739

40+
# Test parse_arguments
3841
@patch('sys.argv', ['program_name', '--mask', 'mask_path', '--file', 'file_path', '--start', '202408121230'])
3942
def test_parse_arguments_success():
4043
args = parse_arguments()
@@ -63,6 +66,7 @@ def test_parse_arguments_missing_start():
6366
with pytest.raises(SystemExit):
6467
parse_arguments()
6568

69+
# Test set_replace_function
6670
@patch('replace_landsurface_with_ERA5land_IC.swap_land_era5land')
6771
def test_set_replace_function_era5land(mock_era5land):
6872
result = set_replace_function("era5land")
@@ -75,4 +79,57 @@ def test_set_replace_function_barra(mock_barra):
7579

7680
def test_set_replace_function_unknown():
7781
result = set_replace_function("unknown")
78-
assert result is None
82+
assert result is None
83+
84+
# Test main function
85+
@patch('hres_ic.parse_arguments')
86+
@patch('hres_ic.get_start_time')
87+
@patch('hres_ic.set_replace_function')
88+
@patch('hres_ic.replace_input_file_with_tmp_input_file')
89+
def test_main_with_replacement(mock_replace_input, mock_set_replace, mock_get_start, mock_parse_args):
90+
# Mock the arguments returned by parse_arguments
91+
mock_args = Mock()
92+
mock_args.mask = "mock_mask"
93+
mock_args.file = "mock_file"
94+
mock_args.start = "mock_start"
95+
mock_args.type = "era5land"
96+
mock_parse_args.return_value = mock_args
97+
98+
# Mock the return value of get_start_time
99+
mock_get_start.return_value = "mock_time"
100+
101+
# Mock the replacement function
102+
mock_replace_func = Mock()
103+
mock_set_replace.return_value = mock_replace_func
104+
105+
main()
106+
mock_parse_args.assert_called_once()
107+
mock_get_start.assert_called_once_with("mock_start")
108+
mock_set_replace.assert_called_once_with("era5land")
109+
mock_replace_func.assert_called_once_with("mock_mask", "mock_file", "mock_time")
110+
mock_replace_input.assert_called_once_with("mock_file")
111+
112+
@patch('hres_ic.parse_arguments')
113+
@patch('hres_ic.get_start_time')
114+
@patch('hres_ic.set_replace_function')
115+
@patch('hres_ic.replace_input_file_with_tmp_input_file')
116+
def test_main_without_replacement(mock_replace_input, mock_set_replace, mock_get_start, mock_parse_args):
117+
# Mock the arguments returned by parse_arguments
118+
mock_args = Mock()
119+
mock_args.mask = "mock_mask"
120+
mock_args.file = "mock_file"
121+
mock_args.start = "mock_start"
122+
mock_args.type = "unknown_type"
123+
mock_parse_args.return_value = mock_args
124+
125+
# Mock the return value of get_start_time
126+
mock_get_start.return_value = "mock_time"
127+
128+
# Mock the replacement function to return None
129+
mock_set_replace.return_value = None
130+
131+
main()
132+
mock_parse_args.assert_called_once()
133+
mock_get_start.assert_called_once_with("mock_start")
134+
mock_set_replace.assert_called_once_with("unknown_type")
135+
mock_replace_input.assert_not_called() # Should not be called since replace_function is None

0 commit comments

Comments
 (0)