-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_context.py
More file actions
175 lines (126 loc) · 5.3 KB
/
test_context.py
File metadata and controls
175 lines (126 loc) · 5.3 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import json
from pathlib import Path
from typing import Any
import pytest
from uipath.runtime.context import UiPathRuntimeContext
from uipath.runtime.errors import (
UiPathErrorCode,
UiPathRuntimeError,
)
from uipath.runtime.result import UiPathRuntimeResult, UiPathRuntimeStatus
class DummyLogsInterceptor:
"""Minimal interceptor used to avoid touching real logging in tests."""
def __init__(self, *args: Any, **kwargs: Any) -> None:
self.setup_called = False
self.teardown_called = False
def setup(self) -> None:
self.setup_called = True
def teardown(self) -> None:
self.teardown_called = True
@pytest.fixture(autouse=True)
def patch_logs_interceptor(monkeypatch: pytest.MonkeyPatch) -> None:
"""Patch UiPathRuntimeLogsInterceptor with a dummy so tests don't depend on logging."""
monkeypatch.setattr(
"uipath.runtime.context.UiPathRuntimeLogsInterceptor",
DummyLogsInterceptor,
)
def test_context_loads_json_input_file(tmp_path: Path) -> None:
input_data = {"foo": "bar", "answer": 42}
input_path = tmp_path / "input.json"
input_path.write_text(json.dumps(input_data))
ctx = UiPathRuntimeContext(input_file=str(input_path))
with ctx:
# input should be loaded from the JSON file
assert ctx.get_input() == input_data
# logs interceptor should have been set up
assert isinstance(ctx.logs_interceptor, DummyLogsInterceptor)
assert ctx.logs_interceptor.setup_called
# After leaving the context, interceptor should be torn down
assert ctx.logs_interceptor.teardown_called
def test_context_raises_for_invalid_json(tmp_path: Path) -> None:
bad_input_path = tmp_path / "input.json"
bad_input_path.write_text("{not: valid json") # invalid JSON
ctx = UiPathRuntimeContext(input_file=str(bad_input_path))
with pytest.raises(UiPathRuntimeError) as excinfo:
with ctx:
# Explicitly call get_input() which will raise
ctx.get_input()
err = excinfo.value.error_info
assert err.code == f"Python.{UiPathErrorCode.INPUT_INVALID_JSON.value}"
def test_output_file_written_on_successful_execution(tmp_path: Path) -> None:
output_path = tmp_path / "output.json"
ctx = UiPathRuntimeContext(
output_file=str(output_path),
)
with ctx:
# Simulate a successful runtime that produced some output
ctx.result = UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUCCESSFUL,
output={"foo": "bar"},
)
pass
assert output_path.exists()
written = json.loads(output_path.read_text())
assert written == {"foo": "bar"}
def test_result_file_written_on_success_contains_output(tmp_path: Path) -> None:
runtime_dir = tmp_path / "runtime"
ctx = UiPathRuntimeContext(
job_id="job-123", # triggers writing result file
runtime_dir=str(runtime_dir),
result_file="result.json",
)
with ctx:
ctx.result = UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUCCESSFUL,
output={"foo": "bar"},
)
pass
# Assert: result file is written whether successful or faulted
result_path = Path(ctx.result_file_path)
assert result_path.exists()
content = json.loads(result_path.read_text())
# Should contain output and no error
assert content["output"] == {"foo": "bar"}
assert "error" not in content or content["error"] is None
def test_result_file_written_on_fault_contains_error_contract(tmp_path: Path) -> None:
runtime_dir = tmp_path / "runtime"
ctx = UiPathRuntimeContext(
job_id="job-456", # triggers writing result file
runtime_dir=str(runtime_dir),
result_file="result.json",
)
# No pre-set result -> context will create a default UiPathRuntimeResult()
# Act: simulate a failing runtime
with pytest.raises(RuntimeError, match="Stream blew up"):
with ctx:
raise RuntimeError("Stream blew up")
# Assert: result file is written even when faulted
result_path = Path(ctx.result_file_path)
assert result_path.exists()
content = json.loads(result_path.read_text())
# We always have an output key, even if it's an empty dict
assert "output" in content
# Status should be FAULTED
assert "status" in content
assert content["status"] == UiPathRuntimeStatus.FAULTED.value
# Error contract should be present and structured
assert "error" in content
error = content["error"]
assert error["code"] == "ERROR_RuntimeError"
assert error["title"] == "Runtime error: RuntimeError"
assert "Stream blew up" in error["detail"]
def test_parse_input_string_returns_none_for_empty_string() -> None:
"""Test that empty input string returns None, not empty dict."""
ctx = UiPathRuntimeContext(input="")
result = ctx.get_input()
assert result is None
def test_parse_input_string_returns_none_for_whitespace_only() -> None:
"""Test that whitespace-only input string returns None, not empty dict."""
ctx = UiPathRuntimeContext(input=" ")
result = ctx.get_input()
assert result is None
def test_parse_input_string_returns_none_for_none() -> None:
"""Test that None input returns None."""
ctx = UiPathRuntimeContext(input=None)
result = ctx.get_input()
assert result is None