-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_async_axon.py
More file actions
113 lines (90 loc) · 4.05 KB
/
Copy pathtest_async_axon.py
File metadata and controls
113 lines (90 loc) · 4.05 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
"""Comprehensive tests for async Axon class."""
from __future__ import annotations
from unittest.mock import AsyncMock
import pytest
from tests.sdk.conftest import MockAxonView, MockPublishResultView, MockSqlBatchResultView, MockSqlQueryResultView
from runloop_api_client.sdk import AsyncAxon
from runloop_api_client.types.axons.sql_statement_params import SqlStatementParams
class TestAsyncAxon:
"""Tests for AsyncAxon class."""
def test_init(self, mock_async_client: AsyncMock) -> None:
"""Test AsyncAxon initialization."""
axon = AsyncAxon(mock_async_client, "axn_123")
assert axon.id == "axn_123"
def test_repr(self, mock_async_client: AsyncMock) -> None:
"""Test AsyncAxon string representation."""
axon = AsyncAxon(mock_async_client, "axn_123")
assert repr(axon) == "<AsyncAxon id='axn_123'>"
@pytest.mark.asyncio
async def test_get_info(self, mock_async_client: AsyncMock, axon_view: MockAxonView) -> None:
"""Test get_info method."""
mock_async_client.axons.retrieve = AsyncMock(return_value=axon_view)
axon = AsyncAxon(mock_async_client, "axn_123")
result = await axon.get_info(
extra_headers={"X-Custom": "value"},
timeout=30.0,
)
assert result == axon_view
mock_async_client.axons.retrieve.assert_awaited_once_with(
"axn_123",
extra_headers={"X-Custom": "value"},
timeout=30.0,
)
@pytest.mark.asyncio
async def test_publish(self, mock_async_client: AsyncMock) -> None:
"""Test publish method."""
mock_result = MockPublishResultView()
mock_async_client.axons.publish = AsyncMock(return_value=mock_result)
axon = AsyncAxon(mock_async_client, "axn_123")
result = await axon.publish(
event_type="test",
origin="USER_EVENT",
payload="{}",
source="sdk",
)
assert result == mock_result
mock_async_client.axons.publish.assert_awaited_once_with(
"axn_123",
event_type="test",
origin="USER_EVENT",
payload="{}",
source="sdk",
)
@pytest.mark.asyncio
async def test_subscribe_sse(self, mock_async_client: AsyncMock) -> None:
"""Test subscribe_sse method."""
mock_stream = AsyncMock()
mock_async_client.axons.subscribe_sse = AsyncMock(return_value=mock_stream)
axon = AsyncAxon(mock_async_client, "axn_123")
result = await axon.subscribe_sse()
assert result == mock_stream
mock_async_client.axons.subscribe_sse.assert_awaited_once_with("axn_123")
@pytest.mark.asyncio
async def test_sql_query(self, mock_async_client: AsyncMock) -> None:
"""Test sql.query method delegates to client.axons.sql.query."""
mock_result = MockSqlQueryResultView()
mock_async_client.axons.sql.query = AsyncMock(return_value=mock_result)
axon = AsyncAxon(mock_async_client, "axn_123")
result = await axon.sql.query(sql="SELECT * FROM test WHERE id = ?", params=[1])
assert result == mock_result
mock_async_client.axons.sql.query.assert_awaited_once_with(
"axn_123",
sql="SELECT * FROM test WHERE id = ?",
params=[1],
)
@pytest.mark.asyncio
async def test_sql_batch(self, mock_async_client: AsyncMock) -> None:
"""Test sql.batch method delegates to client.axons.sql.batch."""
mock_result = MockSqlBatchResultView()
mock_async_client.axons.sql.batch = AsyncMock(return_value=mock_result)
statements: list[SqlStatementParams] = [
{"sql": "CREATE TABLE t (id INTEGER PRIMARY KEY)"},
{"sql": "INSERT INTO t (id) VALUES (?)", "params": [1]},
]
axon = AsyncAxon(mock_async_client, "axn_123")
result = await axon.sql.batch(statements=statements)
assert result == mock_result
mock_async_client.axons.sql.batch.assert_awaited_once_with(
"axn_123",
statements=statements,
)