Skip to content

Commit 77260b8

Browse files
committed
Change NowcastWorker mock to pytest fixture
Test suite maintenance. re: issue #81
1 parent 6db322f commit 77260b8

26 files changed

+387
-548
lines changed

nowcast/workers/download_wwatch3_results.py

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def main():
5050
help="Date of the run to download results files from.",
5151
)
5252
worker.run(download_wwatch3_results, success, failure)
53+
return worker
5354

5455

5556
def success(parsed_args):

nowcast/workers/launch_remote_worker.py

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def main():
4242
help="Quoted string of arguments to launch the remote worker with",
4343
)
4444
worker.run(launch_remote_worker, success, failure)
45+
return worker
4546

4647

4748
def success(parsed_args):

nowcast/workers/make_feeds.py

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def main():
5858
help="Date of the run to create feeds for.",
5959
)
6060
worker.run(make_feeds, success, failure)
61+
return worker
6162

6263

6364
def success(parsed_args):

nowcast/workers/make_runoff_file.py

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def main():
4848
help="Date of the run to produce runoff file for.",
4949
)
5050
worker.run(make_runoff_file, success, failure)
51+
return worker
5152

5253

5354
def success(parsed_args):

nowcast/workers/make_turbidity_file.py

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def main():
4949
help="Date of the run to produce turbidity file for.",
5050
)
5151
worker.run(make_turbidity_file, success, failure)
52+
return worker
5253

5354

5455
def success(parsed_args):

nowcast/workers/make_ww3_current_file.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# SPDX-License-Identifier: Apache-2.0
1717

1818

19-
"""Salish Sea WaveWatch3 forecast worker that produces the hourly
19+
"""SalishSeaCast WaveWatch3 forecast worker that produces the hourly
2020
ocean currents forcing file for a prelim-forecast or forecast run
2121
"""
2222
import logging
@@ -60,6 +60,7 @@ def main():
6060
help="Start date of run to create the currents file for.",
6161
)
6262
worker.run(make_ww3_current_file, success, failure)
63+
return worker
6364

6465

6566
def success(parsed_args):

nowcast/workers/make_ww3_wind_file.py

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def main():
5353
help="Start date of run to create the wind file for.",
5454
)
5555
worker.run(make_ww3_wind_file, success, failure)
56+
return worker
5657

5758

5859
def success(parsed_args):

nowcast/workers/run_NEMO.py

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def main():
6060
help="Date to execute the run for.",
6161
)
6262
worker.run(run_NEMO, success, failure)
63+
return worker
6364

6465

6566
def success(parsed_args):

nowcast/workers/run_NEMO_agrif.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
"""SalishSeaCast worker that repares the YAML run description file and
15+
"""SalishSeaCast worker that prepares the YAML run description file and
1616
bash run script for a NEMO SMELT AGRIF run on an HPC cluster that uses the
1717
TORQUE/MOAB scheduler, and queues the run.
1818
"""
@@ -55,6 +55,7 @@ def main():
5555
help="Date to execute the run for.",
5656
)
5757
worker.run(run_NEMO_agrif, success, failure)
58+
return worker
5859

5960

6061
def success(parsed_args):

nowcast/workers/run_NEMO_hindcast.py

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def main():
6464
""",
6565
)
6666
worker.run(run_NEMO_hindcast, success, failure)
67+
return worker
6768

6869

6970
def success(parsed_args):

nowcast/workers/watch_NEMO.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
"""SalishSeaCast worker that monitors and reports on the
16-
progress of a run on the ONC cloud computing facility or salish.
15+
"""SalishSeaCast worker that monitors and reports on the progress of a run on the cloud
16+
computing facility or salish.
1717
"""
1818
import logging
1919
import os
@@ -52,6 +52,7 @@ def main():
5252
""",
5353
)
5454
worker.run(watch_NEMO, success, failure)
55+
return worker
5556

5657

5758
def success(parsed_args):

nowcast/workers/watch_NEMO_agrif.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
"""SalishSeaCast worker that monitors and reports on the progress of a
16-
NEMO AGRIF run on an HPC cluster that uses the TORQUE/MOAB scheduler.
15+
"""SalishSeaCast worker that monitors and reports on the progress of a NEMO AGRIF run
16+
on an HPC cluster that uses the TORQUE/MOAB scheduler.
1717
"""
1818
import logging
1919
import os
@@ -42,6 +42,7 @@ def main():
4242
worker.cli.add_argument("host_name", help="Name of the host to monitor the run on")
4343
worker.cli.add_argument("job_id", help="Job identifier of the job to monitor")
4444
worker.run(watch_NEMO_agrif, success, failure)
45+
return worker
4546

4647

4748
def success(parsed_args):

nowcast/workers/watch_NEMO_hindcast.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
"""SalishSeaCast worker that monitors and reports on the progress of a
16-
NEMO hindcast run on an HPC cluster that uses the SLURM scheduler.
15+
"""SalishSeaCast worker that monitors and reports on the progress of a NEMO hindcast run
16+
on an HPC cluster that uses the SLURM scheduler.
1717
"""
1818
import logging
1919
import os
@@ -42,6 +42,7 @@ def main():
4242
worker.cli.add_argument("host_name", help="Name of the host to monitor the run on")
4343
worker.cli.add_argument("--run-id", help="Run id to watch; e.g. 01dec14hindcast")
4444
worker.run(watch_NEMO_hindcast, success, failure)
45+
return worker
4546

4647

4748
def success(parsed_args):

tests/workers/test_download_wwatch3_results.py

+34-45
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from unittest.mock import Mock, patch
2121

2222
import arrow
23+
import nemo_nowcast
2324
import pytest
2425

2526
from nowcast.workers import download_wwatch3_results
@@ -31,56 +32,44 @@ def config(base_config):
3132
return base_config
3233

3334

34-
@patch("nowcast.workers.download_wwatch3_results.NowcastWorker", spec=True)
35+
@pytest.fixture
36+
def mock_worker(mock_nowcast_worker, monkeypatch):
37+
monkeypatch.setattr(download_wwatch3_results, "NowcastWorker", mock_nowcast_worker)
38+
39+
3540
class TestMain:
3641
"""Unit tests for main() function."""
3742

38-
def test_instantiate_worker(self, m_worker):
39-
m_worker().cli = Mock(name="cli")
40-
download_wwatch3_results.main()
41-
args, kwargs = m_worker.call_args
42-
assert args == ("download_wwatch3_results",)
43-
assert list(kwargs.keys()) == ["description"]
44-
45-
def test_init_cli(self, m_worker):
46-
m_worker().cli = Mock(name="cli")
47-
download_wwatch3_results.main()
48-
m_worker().init_cli.assert_called_once_with()
49-
50-
def test_add_host_name_arg(self, m_worker):
51-
m_worker().cli = Mock(name="cli")
52-
download_wwatch3_results.main()
53-
args, kwargs = m_worker().cli.add_argument.call_args_list[0]
54-
assert args == ("host_name",)
55-
assert "help" in kwargs
56-
57-
def test_add_run_type_arg(self, m_worker):
58-
m_worker().cli = Mock(name="cli")
59-
download_wwatch3_results.main()
60-
args, kwargs = m_worker().cli.add_argument.call_args_list[1]
61-
assert args == ("run_type",)
62-
expected = {"nowcast", "forecast", "forecast2"}
63-
assert kwargs["choices"] == expected
64-
assert "help" in kwargs
65-
66-
def test_add_run_date_arg(self, m_worker):
67-
m_worker().cli = Mock(name="cli")
68-
download_wwatch3_results.main()
69-
args, kwargs = m_worker().cli.add_date_option.call_args_list[0]
70-
assert args == ("--run-date",)
71-
assert kwargs["default"] == arrow.now().floor("day")
72-
assert "help" in kwargs
73-
74-
def test_run_worker(self, m_worker):
75-
m_worker().cli = Mock(name="cli")
76-
download_wwatch3_results.main()
77-
args, kwargs = m_worker().run.call_args
78-
assert args == (
79-
download_wwatch3_results.download_wwatch3_results,
80-
download_wwatch3_results.success,
81-
download_wwatch3_results.failure,
43+
def test_instantiate_worker(self, mock_worker):
44+
worker = download_wwatch3_results.main()
45+
assert worker.name == "download_wwatch3_results"
46+
assert worker.description.startswith(
47+
"SalishSeaCast system worker that downloads the results files\nfrom a WaveWatch3 run"
8248
)
8349

50+
def test_add_host_name_arg(self, mock_worker):
51+
worker = download_wwatch3_results.main()
52+
assert worker.cli.parser._actions[3].dest == "host_name"
53+
assert worker.cli.parser._actions[3].help
54+
55+
def test_add_run_type_arg(self, mock_worker):
56+
worker = download_wwatch3_results.main()
57+
assert worker.cli.parser._actions[4].dest == "run_type"
58+
assert worker.cli.parser._actions[4].choices == {
59+
"nowcast",
60+
"forecast",
61+
"forecast2",
62+
}
63+
assert worker.cli.parser._actions[4].help
64+
65+
def test_add_run_date_option(self, mock_worker):
66+
worker = download_wwatch3_results.main()
67+
assert worker.cli.parser._actions[5].dest == "run_date"
68+
expected = nemo_nowcast.cli.CommandLineInterface.arrow_date
69+
assert worker.cli.parser._actions[5].type == expected
70+
assert worker.cli.parser._actions[5].default == arrow.now().floor("day")
71+
assert worker.cli.parser._actions[5].help
72+
8473

8574
@pytest.mark.parametrize(
8675
"run_type, host_name",

tests/workers/test_launch_remote_worker.py

+26-43
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
"""Unit tests for SalishSeaCast launch_remote_worker worker."""
2020
from types import SimpleNamespace
21-
from unittest.mock import Mock, patch
21+
from unittest.mock import patch
2222

2323
import pytest
2424

@@ -31,53 +31,36 @@ def config(base_config):
3131
return base_config
3232

3333

34-
@patch("nowcast.workers.launch_remote_worker.NowcastWorker", spec=True)
34+
@pytest.fixture
35+
def mock_worker(mock_nowcast_worker, monkeypatch):
36+
monkeypatch.setattr(launch_remote_worker, "NowcastWorker", mock_nowcast_worker)
37+
38+
3539
class TestMain:
3640
"""Unit tests for main() function."""
3741

38-
def test_instantiate_worker(self, m_worker):
39-
m_worker().cli = Mock(name="cli")
40-
launch_remote_worker.main()
41-
args, kwargs = m_worker.call_args
42-
assert args == ("launch_remote_worker",)
43-
assert list(kwargs.keys()) == ["description"]
44-
45-
def test_init_cli(self, m_worker):
46-
m_worker().cli = Mock(name="cli")
47-
launch_remote_worker.main()
48-
m_worker().init_cli.assert_called_once_with()
49-
50-
def test_add_host_name_arg(self, m_worker):
51-
m_worker().cli = Mock(name="cli")
52-
launch_remote_worker.main()
53-
args, kwargs = m_worker().cli.add_argument.call_args_list[0]
54-
assert args == ("host_name",)
55-
assert "help" in kwargs
56-
57-
def test_add_remote_worker_arg(self, m_worker):
58-
m_worker().cli = Mock(name="cli")
59-
launch_remote_worker.main()
60-
args, kwargs = m_worker().cli.add_argument.call_args_list[1]
61-
assert args == ("remote_worker",)
62-
assert "help" in kwargs
63-
64-
def test_add_worker_args_arg(self, m_worker):
65-
m_worker().cli = Mock(name="cli")
66-
launch_remote_worker.main()
67-
args, kwargs = m_worker().cli.add_argument.call_args_list[2]
68-
assert args == ("worker_args",)
69-
assert "help" in kwargs
70-
71-
def test_run_worker(self, m_worker):
72-
m_worker().cli = Mock(name="cli")
73-
launch_remote_worker.main()
74-
args, kwargs = m_worker().run.call_args
75-
assert args == (
76-
launch_remote_worker.launch_remote_worker,
77-
launch_remote_worker.success,
78-
launch_remote_worker.failure,
42+
def test_init_cli(self, mock_worker):
43+
worker = launch_remote_worker.main()
44+
assert worker.name == "launch_remote_worker"
45+
assert worker.description.startswith(
46+
"SalishSeaCast worker that launches a specified worker on a remote host."
7947
)
8048

49+
def test_add_host_name_arg(self, mock_worker):
50+
worker = launch_remote_worker.main()
51+
assert worker.cli.parser._actions[3].dest == "host_name"
52+
assert worker.cli.parser._actions[3].help
53+
54+
def test_add_remote_worker_arg(self, mock_worker):
55+
worker = launch_remote_worker.main()
56+
assert worker.cli.parser._actions[4].dest == "remote_worker"
57+
assert worker.cli.parser._actions[4].help
58+
59+
def test_add_worker_args_arg(self, mock_worker):
60+
worker = launch_remote_worker.main()
61+
assert worker.cli.parser._actions[5].dest == "worker_args"
62+
assert worker.cli.parser._actions[5].help
63+
8164

8265
class TestConfig:
8366
"""Unit tests for production YAML config file elements related to worker."""

0 commit comments

Comments
 (0)