-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_task_app.py
More file actions
100 lines (75 loc) · 2.83 KB
/
test_task_app.py
File metadata and controls
100 lines (75 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from __future__ import annotations
import json
from datetime import datetime, timezone
from typing import TYPE_CHECKING
import pytest
from typer.testing import CliRunner
from infrahub_sdk.ctl.task import app
runner = CliRunner()
pytestmark = pytest.mark.httpx_mock(can_send_already_matched_responses=True)
if TYPE_CHECKING:
from pytest_httpx import HTTPXMock
def _task_response() -> dict:
now = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc).isoformat()
return {
"data": {
"InfrahubTask": {
"edges": [
{
"node": {
"id": "task-1",
"title": "Sync repositories",
"state": "RUNNING",
"progress": 0.5,
"workflow": "RepositorySync",
"branch": "main",
"created_at": now,
"updated_at": now,
"logs": {"edges": []},
"related_nodes": [],
}
}
],
"count": 1,
}
}
}
def _empty_task_response() -> dict:
return {"data": {"InfrahubTask": {"edges": [], "count": 0}}}
def test_task_list_command(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
method="POST",
url="http://mock/graphql/main",
json=_task_response(),
match_headers={"X-Infrahub-Tracker": "query-tasks-page1"},
)
result = runner.invoke(app=app, args=["list"])
assert result.exit_code == 0
assert "Infrahub Tasks" in result.stdout
assert "Sync repositories" in result.stdout
def test_task_list_json_output(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
method="POST",
url="http://mock/graphql/main",
json=_task_response(),
match_headers={"X-Infrahub-Tracker": "query-tasks-page1"},
)
result = runner.invoke(app=app, args=["list", "--json"])
assert result.exit_code == 0
payload = json.loads(result.stdout)
assert payload[0]["state"] == "RUNNING"
assert payload[0]["title"] == "Sync repositories"
def test_task_list_with_state_filter(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
method="POST",
url="http://mock/graphql/main",
json=_empty_task_response(),
match_headers={"X-Infrahub-Tracker": "query-tasks-page1"},
)
result = runner.invoke(app=app, args=["list", "--state", "running"])
assert result.exit_code == 0
assert "No tasks found" in result.stdout
def test_task_list_invalid_state() -> None:
result = runner.invoke(app=app, args=["list", "--state", "invalid"])
assert result.exit_code != 0
assert "Unsupported state" in result.stdout