Skip to content

Commit

Permalink
refactor: fixing ruff, mypy errors, improving fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
aorumbayev committed Aug 14, 2023
1 parent c5d6383 commit 208f21c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 26 deletions.
24 changes: 16 additions & 8 deletions tests/goal/test_goal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _mocked_goal_mount_path(cwd: Path, monkeypatch: pytest.MonkeyPatch) -> None:


@pytest.fixture()
def _setup_input_files(cwd: Path, request) -> None:
def _setup_input_files(cwd: Path, request: pytest.FixtureRequest) -> None:
file_names = request.param
for file_name in file_names:
(cwd / file_name).write_text(
Expand Down Expand Up @@ -147,13 +147,13 @@ def test_goal_start_without_docker_engine_running(proc_mock: ProcMock) -> None:
verify(result.output)


@pytest.mark.usefixtures("proc_mock", "cwd", "_mocked_goal_mount_path", "_setup_input_files")
@pytest.mark.usefixtures("proc_mock", "_mocked_goal_mount_path", "_setup_input_files")
@pytest.mark.parametrize("_setup_input_files", [["approval.teal"]], indirect=True)
def test_goal_simple_args_with_input_file(
proc_mock: ProcMock,
cwd: Path,
) -> None:
(cwd / "/root/goal_mount/approval.teal").exists()
assert (cwd / "approval.teal").exists()
expected_arguments = [
"docker",
"exec",
Expand All @@ -168,7 +168,7 @@ def test_goal_simple_args_with_input_file(
]

proc_mock.set_output(expected_arguments, output=["File compiled"])
result = invoke("goal clerk group approval.teal")
result = invoke("goal clerk group approval.teal", cwd=cwd)

assert proc_mock.called[1].command[9] == "/root/goal_mount/approval.teal"
assert result.exit_code == 0
Expand All @@ -191,7 +191,12 @@ def test_goal_simple_args_with_output_file(proc_mock: ProcMock, cwd: Path) -> No
"/root/goal_mount/balance_record.json",
]

proc_mock.set_output(expected_arguments, output=["File compiled"], side_effect=dump_json_file(cwd))
proc_mock.set_output(
expected_arguments,
output=["File compiled"],
side_effect=dump_json_file,
side_effect_args={"cwd": cwd},
)
result = invoke("goal account dump -o balance_record.json")

assert proc_mock.called[1].command[10] == "/root/goal_mount/balance_record.json"
Expand Down Expand Up @@ -223,7 +228,9 @@ def test_goal_simple_args_with_input_output_files(
"/root/goal_mount/approval.compiled",
]

proc_mock.set_output(expected_arguments, output=["File compiled"], side_effect=dump_file(cwd))
proc_mock.set_output(
expected_arguments, output=["File compiled"], side_effect=dump_file, side_effect_args={"cwd": cwd}
)

result = invoke("goal clerk compile approval.teal -o approval.compiled", cwd=cwd)

Expand Down Expand Up @@ -258,7 +265,9 @@ def test_goal_simple_args_with_multiple_input_output_files(
"/root/goal_mount/approval.compiled",
]

proc_mock.set_output(expected_arguments, output=["File compiled"], side_effect=dump_file(cwd))
proc_mock.set_output(
expected_arguments, output=["File compiled"], side_effect=dump_file, side_effect_args={"cwd": cwd}
)
result = invoke("goal clerk compile approval1.teal approval2.teal -o approval.compiled", cwd=cwd)

assert proc_mock.called[1].command[9] == "/root/goal_mount/approval1.teal"
Expand All @@ -273,7 +282,6 @@ def test_goal_simple_args_with_multiple_input_output_files(

@pytest.mark.usefixtures("proc_mock", "cwd", "_mocked_goal_mount_path")
def test_goal_simple_args_with_file_error(
proc_mock: ProcMock,
cwd: Path,
) -> None:
assert not (cwd / "approval.teal").exists()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DEBUG: Running 'docker version' in '{current_working_directory}'
DEBUG: docker: STDOUT
DEBUG: docker: STDERR
ERROR: approval.teal does not exist.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ services:
- type: bind
source: ./algod_config.json
target: /etc/algorand/config.json
- type: volume
source: /Users/negarabbasi/algokit_local
target: /Users/negarabbasi/goal
- ./goal_mount:/root/goal_mount

indexer:
container_name: algokit_indexer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ services:
- type: bind
source: ./algod_config.json
target: /etc/algorand/config.json
- type: volume
source: /Users/negarabbasi/algokit_local
target: /Users/negarabbasi/goal
- ./goal_mount:/root/goal_mount

indexer:
container_name: algokit_indexer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ services:
- type: bind
source: ./algod_config.json
target: /etc/algorand/config.json
- type: volume
source: /Users/negarabbasi/algokit_local
target: /Users/negarabbasi/goal
- ./goal_mount:/root/goal_mount

indexer:
container_name: algokit_indexer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ services:
- type: bind
source: ./algod_config.json
target: /etc/algorand/config.json
- type: volume
source: /Users/negarabbasi/algokit_local
target: /Users/negarabbasi/goal
- ./goal_mount:/root/goal_mount

indexer:
container_name: algokit_indexer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ services:
- type: bind
source: ./algod_config.json
target: /etc/algorand/config.json
- type: volume
source: /Users/negarabbasi/algokit_local
target: /Users/negarabbasi/goal
- ./goal_mount:/root/goal_mount

indexer:
container_name: algokit_indexer
Expand Down
14 changes: 11 additions & 3 deletions tests/utils/proc_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class CommandMockData:
exit_code: int = 0
output_lines: list[str] = dataclasses.field(default_factory=lambda: ["STDOUT", "STDERR"])
side_effect: Any | None = None
side_effect_args: dict[str, Any] | None = None


@dataclasses.dataclass(kw_only=True)
Expand Down Expand Up @@ -90,7 +91,11 @@ def should_bad_exit_on(self, cmd: list[str] | str, exit_code: int = -1, output:
self._add_mock_data(cmd, mock_data)

def set_output(
self, cmd: list[str] | str, output: list[str], side_effect: Callable[[], None] | None = None
self,
cmd: list[str] | str,
output: list[str],
side_effect: Callable[[Any], None] | None = None,
side_effect_args: dict[str, Any] | None = None,
) -> None:
"""
Set the output of a command, and optionally a side effect to be called when the command is run.
Expand All @@ -103,7 +108,9 @@ def set_output(
side_effect: A callable to be called when the command is run
"""

self._add_mock_data(cmd, CommandMockData(output_lines=output, side_effect=side_effect))
self._add_mock_data(
cmd, CommandMockData(output_lines=output, side_effect=side_effect, side_effect_args=side_effect_args)
)

def popen(self, cmd: list[str], env: dict[str, str] | None = None, *_args: Any, **_kwargs: Any) -> PopenMock:
self.called.append(PopenArgs(command=cmd, env=env))
Expand All @@ -126,7 +133,8 @@ def popen(self, cmd: list[str], env: dict[str, str] | None = None, *_args: Any,
output = "\n".join(mock_data.output_lines)

if mock_data.side_effect:
mock_data.side_effect()
side_effect_args = mock_data.side_effect_args or {}
mock_data.side_effect(**side_effect_args)

return PopenMock(output, exit_code)

Expand Down

0 comments on commit 208f21c

Please sign in to comment.