Skip to content

Commit 67c010f

Browse files
committed
improved tests
1 parent 8b39663 commit 67c010f

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

taskbadger/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def _update_task(task, status=None, **data_kwargs):
113113
else:
114114
task_data[key] = value
115115

116+
print(task_data)
116117
try:
117118
task.update(status=status, data=task_data or None)
118119
except Exception as e:

tests/test_cli_run.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,33 @@ def test_cli_long_run():
3636
def _should_update_task(last_update, update_frequency_seconds):
3737
return True
3838

39+
with mock.patch("taskbadger.process._should_update", new=_should_update_task):
40+
_test_cli_run(["echo test; sleep 0.11"], 0, args=["task_name"], update_call_count=3)
41+
42+
43+
def test_cli_capture_output():
44+
update_patch = _test_cli_run(["echo test"], 0, args=["task_name", "--capture-output"], update_call_count=2)
45+
46+
body = PatchedTaskRequest(status=UNSET, data=PatchedTaskRequestData.from_dict({"stdout": "test\n"}))
47+
update_patch.assert_any_call(
48+
client=Badger.current.settings.client,
49+
organization_slug="org",
50+
project_slug="project",
51+
id="test_id",
52+
json_body=body,
53+
)
54+
55+
56+
def test_cli_capture_output_append():
57+
def _should_update_task(last_update, update_frequency_seconds):
58+
return True
59+
3960
with mock.patch("taskbadger.process._should_update", new=_should_update_task):
4061
update_patch = _test_cli_run(
41-
["echo test; sleep 0.11"], 0, args=["task_name", "--capture-output"], update_call_count=3
62+
["echo test; sleep 0.11; echo 123"], 0, args=["task_name", "--capture-output"], update_call_count=3
4263
)
4364

44-
# make sure the output was captured
45-
body = PatchedTaskRequest(status=UNSET, data=PatchedTaskRequestData.from_dict({"stdout": "test\n"}))
65+
body = PatchedTaskRequest(status=UNSET, data=PatchedTaskRequestData.from_dict({"stdout": "test\n123\n"}))
4666
update_patch.assert_any_call(
4767
client=Badger.current.settings.client,
4868
organization_slug="org",
@@ -75,14 +95,22 @@ def test_cli_run_webhook():
7595

7696

7797
def _test_cli_run(command, return_code, args=None, action=None, update_call_count=1):
98+
update_mock = mock.MagicMock()
99+
100+
def _update(*args, **kwargs):
101+
update_mock(*args, **kwargs)
102+
103+
# handle updating task data
104+
data = kwargs["json_body"].data
105+
return Response(HTTPStatus.OK, b"", {}, task_for_test(data=data.additional_properties if data else None))
106+
78107
with (
79108
mock.patch("taskbadger.sdk.task_create.sync_detailed") as create,
80-
mock.patch("taskbadger.sdk.task_partial_update.sync_detailed") as update,
109+
mock.patch("taskbadger.sdk.task_partial_update.sync_detailed", new=_update) as update,
81110
):
82111
task = task_for_test()
83112
create.return_value = Response(HTTPStatus.OK, b"", {}, task)
84113

85-
update.return_value = Response(HTTPStatus.OK, b"", {}, task)
86114
args = args or []
87115
result = runner.invoke(app, ["run"] + args + ["--"] + command, catch_exceptions=False)
88116
print(result.output)
@@ -103,8 +131,8 @@ def _test_cli_run(command, return_code, args=None, action=None, update_call_coun
103131
status=StatusEnum.ERROR, data=PatchedTaskRequestData.from_dict({"return_code": return_code})
104132
)
105133

106-
assert update.call_count == update_call_count
107-
update.assert_called_with(
134+
assert update_mock.call_count == update_call_count
135+
update_mock.assert_called_with(
108136
client=settings.client, organization_slug="org", project_slug="project", id="test_id", json_body=body
109137
)
110-
return update
138+
return update_mock

0 commit comments

Comments
 (0)