Skip to content

Commit 94b56ea

Browse files
committed
add unit tests for download_logs and score_and_complete
1 parent de0a302 commit 94b56ea

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

tests/sdk/test_async_scenario_run.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
from types import SimpleNamespace
6+
from pathlib import Path
67
from unittest.mock import AsyncMock
78

89
from tests.sdk.conftest import MockDevboxView, MockScenarioRunView
@@ -76,7 +77,7 @@ async def test_await_scored(self, mock_async_client: AsyncMock, scenario_run_vie
7677
result = await run.await_scored()
7778

7879
assert result == scenario_run_view
79-
mock_async_client.scenarios.runs.await_scored.assert_awaited_once_with("run_123", polling_config=None)
80+
mock_async_client.scenarios.runs.await_scored.assert_awaited_once_with("run_123")
8081

8182
async def test_score_and_await(self, mock_async_client: AsyncMock, scenario_run_view: MockScenarioRunView) -> None:
8283
"""Test score_and_await method."""
@@ -87,7 +88,20 @@ async def test_score_and_await(self, mock_async_client: AsyncMock, scenario_run_
8788
result = await run.score_and_await()
8889

8990
assert result == scenario_run_view
90-
mock_async_client.scenarios.runs.score_and_await.assert_awaited_once_with("run_123", polling_config=None)
91+
mock_async_client.scenarios.runs.score_and_await.assert_awaited_once_with("run_123")
92+
93+
async def test_score_and_complete(
94+
self, mock_async_client: AsyncMock, scenario_run_view: MockScenarioRunView
95+
) -> None:
96+
"""Test score_and_complete method."""
97+
scenario_run_view.state = "completed"
98+
mock_async_client.scenarios.runs.score_and_complete = AsyncMock(return_value=scenario_run_view)
99+
100+
run = AsyncScenarioRun(mock_async_client, "run_123", "dev_123")
101+
result = await run.score_and_complete()
102+
103+
assert result == scenario_run_view
104+
mock_async_client.scenarios.runs.score_and_complete.assert_awaited_once_with("run_123")
91105

92106
async def test_complete(self, mock_async_client: AsyncMock, scenario_run_view: MockScenarioRunView) -> None:
93107
"""Test complete method."""
@@ -111,6 +125,19 @@ async def test_cancel(self, mock_async_client: AsyncMock, scenario_run_view: Moc
111125
assert result == scenario_run_view
112126
mock_async_client.scenarios.runs.cancel.assert_awaited_once_with("run_123")
113127

128+
async def test_download_logs(self, mock_async_client: AsyncMock, tmp_path: Path) -> None:
129+
"""Test download_logs method writes to file."""
130+
mock_response = AsyncMock()
131+
mock_response.write_to_file = AsyncMock()
132+
mock_async_client.scenarios.runs.download_logs = AsyncMock(return_value=mock_response)
133+
134+
run = AsyncScenarioRun(mock_async_client, "run_123", "dev_123")
135+
output_path = tmp_path / "logs.zip"
136+
await run.download_logs(output_path)
137+
138+
mock_async_client.scenarios.runs.download_logs.assert_awaited_once_with("run_123")
139+
mock_response.write_to_file.assert_awaited_once_with(output_path)
140+
114141
async def test_get_score_when_scored(self, mock_async_client: AsyncMock) -> None:
115142
"""Test get_score returns scoring result when scored."""
116143
scoring_result = SimpleNamespace(score=0.95, scoring_function_results=[])
@@ -121,6 +148,7 @@ async def test_get_score_when_scored(self, mock_async_client: AsyncMock) -> None
121148
result = await run.get_score()
122149

123150
assert result == scoring_result
151+
mock_async_client.scenarios.runs.retrieve.assert_awaited_once_with("run_123")
124152

125153
async def test_get_score_when_not_scored(self, mock_async_client: AsyncMock) -> None:
126154
"""Test get_score returns None when not scored."""
@@ -131,3 +159,4 @@ async def test_get_score_when_not_scored(self, mock_async_client: AsyncMock) ->
131159
result = await run.get_score()
132160

133161
assert result is None
162+
mock_async_client.scenarios.runs.retrieve.assert_awaited_once_with("run_123")

tests/sdk/test_scenario_run.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
from types import SimpleNamespace
6+
from pathlib import Path
67
from unittest.mock import Mock
78

89
from tests.sdk.conftest import MockDevboxView, MockScenarioRunView
@@ -73,7 +74,7 @@ def test_await_scored(self, mock_client: Mock, scenario_run_view: MockScenarioRu
7374
result = run.await_scored()
7475

7576
assert result == scenario_run_view
76-
mock_client.scenarios.runs.await_scored.assert_called_once_with("run_123", polling_config=None)
77+
mock_client.scenarios.runs.await_scored.assert_called_once_with("run_123")
7778

7879
def test_score_and_await(self, mock_client: Mock, scenario_run_view: MockScenarioRunView) -> None:
7980
"""Test score_and_await method."""
@@ -84,7 +85,18 @@ def test_score_and_await(self, mock_client: Mock, scenario_run_view: MockScenari
8485
result = run.score_and_await()
8586

8687
assert result == scenario_run_view
87-
mock_client.scenarios.runs.score_and_await.assert_called_once_with("run_123", polling_config=None)
88+
mock_client.scenarios.runs.score_and_await.assert_called_once_with("run_123")
89+
90+
def test_score_and_complete(self, mock_client: Mock, scenario_run_view: MockScenarioRunView) -> None:
91+
"""Test score_and_complete method."""
92+
scenario_run_view.state = "completed"
93+
mock_client.scenarios.runs.score_and_complete.return_value = scenario_run_view
94+
95+
run = ScenarioRun(mock_client, "run_123", "dev_123")
96+
result = run.score_and_complete()
97+
98+
assert result == scenario_run_view
99+
mock_client.scenarios.runs.score_and_complete.assert_called_once_with("run_123")
88100

89101
def test_complete(self, mock_client: Mock, scenario_run_view: MockScenarioRunView) -> None:
90102
"""Test complete method."""
@@ -108,6 +120,19 @@ def test_cancel(self, mock_client: Mock, scenario_run_view: MockScenarioRunView)
108120
assert result == scenario_run_view
109121
mock_client.scenarios.runs.cancel.assert_called_once_with("run_123")
110122

123+
def test_download_logs(self, mock_client: Mock, tmp_path: Path) -> None:
124+
"""Test download_logs method writes to file."""
125+
mock_response = Mock()
126+
mock_response.write_to_file = Mock()
127+
mock_client.scenarios.runs.download_logs.return_value = mock_response
128+
129+
run = ScenarioRun(mock_client, "run_123", "dev_123")
130+
output_path = tmp_path / "logs.zip"
131+
run.download_logs(output_path)
132+
133+
mock_client.scenarios.runs.download_logs.assert_called_once_with("run_123")
134+
mock_response.write_to_file.assert_called_once_with(output_path)
135+
111136
def test_get_score_when_scored(self, mock_client: Mock) -> None:
112137
"""Test get_score returns scoring result when scored."""
113138
scoring_result = SimpleNamespace(score=0.95, scoring_function_results=[])
@@ -118,6 +143,7 @@ def test_get_score_when_scored(self, mock_client: Mock) -> None:
118143
result = run.get_score()
119144

120145
assert result == scoring_result
146+
mock_client.scenarios.runs.retrieve.assert_called_once_with("run_123")
121147

122148
def test_get_score_when_not_scored(self, mock_client: Mock) -> None:
123149
"""Test get_score returns None when not scored."""
@@ -128,3 +154,4 @@ def test_get_score_when_not_scored(self, mock_client: Mock) -> None:
128154
result = run.get_score()
129155

130156
assert result is None
157+
mock_client.scenarios.runs.retrieve.assert_called_once_with("run_123")

0 commit comments

Comments
 (0)