1
1
import pytest
2
- from unittest .mock import patch
2
+ from unittest .mock import patch , Mock
3
3
from pathlib import Path
4
4
5
5
# TODO: place ROSE DATA into a function (or an input argument) so it doesn't need to get called when importing the module
12
12
srcpath = os .path .join (os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))),'src' ) #To delete when src is a package
13
13
sys .path .insert (0 ,srcpath ) #To delete when src is a package
14
14
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
16
16
17
17
del sys .path [0 ] #To delete when src is a package
18
18
19
+ # Test get_start_time
19
20
def test_get_start_time ():
20
21
time = "199205041155"
21
22
assert get_start_time (time ) == "19920504T1155Z"
22
23
24
+ # Test replace_input_file_with_tmp_input_file
23
25
def test_replace_input_file_with_tmp_input_file ():
24
26
tmppath = Path ('example/of/.tmp/file.tmp' )
25
27
newpath = Path ('example/of/.tmp/file' )
@@ -35,6 +37,7 @@ def test_replace_input_file_with_tmp_input_file_fail():
35
37
with pytest .raises (ValueError ):
36
38
replace_input_file_with_tmp_input_file (tmppath )
37
39
40
+ # Test parse_arguments
38
41
@patch ('sys.argv' , ['program_name' , '--mask' , 'mask_path' , '--file' , 'file_path' , '--start' , '202408121230' ])
39
42
def test_parse_arguments_success ():
40
43
args = parse_arguments ()
@@ -63,6 +66,7 @@ def test_parse_arguments_missing_start():
63
66
with pytest .raises (SystemExit ):
64
67
parse_arguments ()
65
68
69
+ # Test set_replace_function
66
70
@patch ('replace_landsurface_with_ERA5land_IC.swap_land_era5land' )
67
71
def test_set_replace_function_era5land (mock_era5land ):
68
72
result = set_replace_function ("era5land" )
@@ -75,4 +79,57 @@ def test_set_replace_function_barra(mock_barra):
75
79
76
80
def test_set_replace_function_unknown ():
77
81
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