Skip to content

Commit 4b5fb2c

Browse files
committedJan 8, 2025·
test: add basic AccountRunner test
This test validates that a Command's lifecycle methods are invoked by the runner: pre_hook_with_context, execute, collect_results, and post_hook. It also validates that the older pre_hook is still called if the newer pre_hook_with_context does not exist.
1 parent afdd406 commit 4b5fb2c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
 

‎tests/unit/test_runner.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# Copyright 2025 FMR LLC <opensource@fidelity.com>
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
# pylint: disable=redefined-outer-name,missing-docstring
8+
9+
import pytest
10+
from awsrun.runner import AccountRunner, Command
11+
from awsrun.session import SessionProvider
12+
13+
14+
@pytest.fixture
15+
def runner(mocker):
16+
session_provider = mocker.MagicMock(spec=SessionProvider)
17+
return AccountRunner(session_provider)
18+
19+
20+
def test_lifecycle_methods(mocker, runner):
21+
command = mocker.MagicMock(spec=Command)
22+
runner.run(command, ["a", "b"])
23+
command.pre_hook_with_context.assert_called()
24+
assert command.execute.call_count == 2
25+
command.collect_results.assert_called()
26+
command.post_hook.assert_called()
27+
28+
29+
def test_lifecycle_methods_with_no_accts_to_process(mocker, runner):
30+
command = mocker.MagicMock(spec=Command)
31+
runner.run(command, [])
32+
command.pre_hook_with_context.assert_called()
33+
command.execute.assert_not_called()
34+
command.collect_results.assert_not_called()
35+
command.post_hook.assert_called()
36+
37+
38+
def test_pre_hook_with_context(mocker, runner):
39+
command = Command()
40+
command.pre_hook_with_context = mocker.MagicMock()
41+
runner.run(command, [], context="Hello")
42+
command.pre_hook_with_context.assert_called_with("Hello")
43+
44+
45+
# If no pre_hook_with_context is explicitly defined in a Command, then the
46+
# default implementation should invoke the older pre_hook method for
47+
# backwards compatibility.
48+
def test_backwards_compat_lifecycle_pre_hook(mocker, runner):
49+
command = Command()
50+
command.pre_hook = mocker.MagicMock()
51+
runner.run(command, [])
52+
command.pre_hook.assert_called()

0 commit comments

Comments
 (0)
Please sign in to comment.