Skip to content

Commit bd10418

Browse files
committed
Add test cases, move duplicating code to fixture, fix name of test
1 parent 7fe0eca commit bd10418

File tree

1 file changed

+67
-25
lines changed

1 file changed

+67
-25
lines changed

tests/client/chats_api/test_create_thread.py

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from http import HTTPStatus
2-
from typing import Any
2+
from typing import Any, Callable
33
from uuid import UUID
44

55
import httpx
66
import pytest
7+
from respx import Route
78
from respx.router import MockRouter
89

910
from pybotx import (
@@ -25,27 +26,44 @@
2526
ENDPOINT = "api/v3/botx/chats/create_thread"
2627

2728

28-
async def test__create_chat__succeed(
29+
@pytest.fixture
30+
def sync_id() -> str:
31+
return "21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3"
32+
33+
34+
@pytest.fixture
35+
def create_mocked_endpoint(
2936
respx_mock: MockRouter,
3037
host: str,
38+
sync_id: str,
39+
) -> Callable[[dict[str, Any], int], Route]:
40+
def mocked_endpoint(json_response: dict[str, Any], status_code: int) -> Route:
41+
return respx_mock.post(
42+
f"https://{host}/{ENDPOINT}",
43+
headers={
44+
"Authorization": "Bearer token",
45+
"Content-Type": "application/json",
46+
},
47+
json={"sync_id": sync_id},
48+
).mock(return_value=httpx.Response(status_code, json=json_response))
49+
50+
return mocked_endpoint
51+
52+
53+
async def test__create_thread__succeed(
54+
create_mocked_endpoint: Callable[[dict[str, Any], int], Route],
55+
sync_id: str,
3156
bot_id: UUID,
3257
bot_account: BotAccountWithSecret,
3358
) -> None:
3459
# - Arrange -
35-
sync_id = "21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3"
3660
thread_id = "2a8c0d1e-c4d1-4308-b024-6e1a9f4a4b6d"
37-
endpoint = respx_mock.post(
38-
f"https://{host}/{ENDPOINT}",
39-
headers={"Authorization": "Bearer token", "Content-Type": "application/json"},
40-
json={"sync_id": sync_id},
41-
).mock(
42-
return_value=httpx.Response(
43-
HTTPStatus.OK,
44-
json={
45-
"status": "ok",
46-
"result": {"thread_id": thread_id},
47-
},
48-
),
61+
endpoint = create_mocked_endpoint(
62+
{
63+
"status": "ok",
64+
"result": {"thread_id": thread_id},
65+
},
66+
HTTPStatus.OK,
4967
)
5068

5169
built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account])
@@ -185,25 +203,47 @@ async def test__create_chat__succeed(
185203
HTTPStatus.UNPROCESSABLE_ENTITY,
186204
ThreadCreationError,
187205
),
206+
(
207+
{
208+
"status": "error",
209+
"reason": None,
210+
"errors": [],
211+
"error_data": {},
212+
},
213+
HTTPStatus.FORBIDDEN,
214+
ThreadCreationProhibitedError,
215+
),
216+
(
217+
{
218+
"status": "error",
219+
"errors": [],
220+
"error_data": {},
221+
},
222+
HTTPStatus.FORBIDDEN,
223+
ThreadCreationProhibitedError,
224+
),
225+
(
226+
{
227+
"status": "error",
228+
"reason": "unexpected reason",
229+
"errors": [],
230+
},
231+
HTTPStatus.FORBIDDEN,
232+
ThreadCreationProhibitedError,
233+
),
188234
),
189235
)
190236
async def test__create_thread__botx_error_raised(
191-
respx_mock: MockRouter,
192-
host: str,
237+
create_mocked_endpoint: Callable[[dict[str, Any], int], Route],
238+
sync_id: str,
193239
bot_id: UUID,
194240
bot_account: BotAccountWithSecret,
195241
return_json: dict[str, Any],
196242
response_status: int,
197243
expected_exc_type: type[BaseException],
198244
) -> None:
199245
# - Arrange -
200-
sync_id = "21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3"
201-
endpoint = respx_mock.post(
202-
f"https://{host}/{ENDPOINT}",
203-
headers={"Authorization": "Bearer token", "Content-Type": "application/json"},
204-
json={"sync_id": sync_id},
205-
).mock(return_value=httpx.Response(response_status, json=return_json))
206-
246+
endpoint = create_mocked_endpoint(return_json, response_status)
207247
built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account])
208248

209249
# - Act -
@@ -216,4 +256,6 @@ async def test__create_thread__botx_error_raised(
216256

217257
# - Assert -
218258
assert endpoint.called
219-
assert return_json["reason"] in str(exc.value)
259+
260+
if return_json.get("reason"):
261+
assert return_json["reason"] in str(exc.value)

0 commit comments

Comments
 (0)