Skip to content

Commit 8713d8e

Browse files
committed
- some fix
1 parent 0f3901f commit 8713d8e

3 files changed

Lines changed: 22 additions & 62 deletions

File tree

pybotx/bot/bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ async def create_chat(
13001300
self._bot_accounts_storage,
13011301
)
13021302

1303-
payload = BotXAPICreateChatRequestPayload.from_domain(
1303+
payload = BotXAPICreateChatRequestPayload(
13041304
name=name,
13051305
chat_type=chat_type,
13061306
members=huids,

pybotx/client/chats_api/create_chat.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from typing import Literal, Union
1+
from typing import Any, Literal
22
from uuid import UUID
33

44
from pydantic import (
55
Field,
66
ConfigDict,
77
field_serializer,
88
field_validator,
9+
model_validator,
910
)
1011

1112
from pybotx.client.authorized_botx_method import AuthorizedBotXMethod
@@ -35,31 +36,12 @@ class BotXAPICreateChatRequestPayload(UnverifiedPayloadBaseModel):
3536
shared_history: Missing[bool]
3637
avatar: str | None = None
3738

38-
@classmethod
39-
def from_domain(
40-
cls,
41-
name: str,
42-
chat_type: Union[APIChatTypes, ChatTypes],
43-
members: list[UUID],
44-
shared_history: Missing[bool],
45-
description: str | None = None,
46-
avatar: str | None = None,
47-
) -> "BotXAPICreateChatRequestPayload":
48-
converted_chat_type = cls._convert_chat_type(chat_type)
49-
return cls(
50-
name=name,
51-
chat_type=converted_chat_type,
52-
members=members,
53-
shared_history=shared_history,
54-
description=description,
55-
avatar=avatar,
56-
)
57-
58-
@classmethod
59-
def _convert_chat_type(cls, v: Union[APIChatTypes, ChatTypes]) -> APIChatTypes:
60-
if isinstance(v, ChatTypes):
61-
return convert_chat_type_from_domain(v)
62-
return v
39+
@model_validator(mode="before")
40+
def _convert_chat_type(cls, values: dict[str, Any]) -> dict[str, Any]:
41+
chat_type = values.get("chat_type")
42+
if isinstance(chat_type, ChatTypes):
43+
values["chat_type"] = convert_chat_type_from_domain(chat_type)
44+
return values
6345

6446
@field_validator("avatar")
6547
def _validate_avatar(cls, v: str | None) -> str | None:

tests/client/chats_api/test_create_chat.py

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from respx.router import MockRouter
88

99
from pybotx import ChatCreationError, ChatCreationProhibitedError, ChatTypes
10-
from pybotx.missing import Undefined
1110
from tests.testkit import BotXRequest, error_payload, mock_botx, ok_payload
1211

1312
pytestmark = [
@@ -216,41 +215,20 @@ def test__create_chat_payload__convert_chat_type_validator() -> None:
216215
from pybotx.client.chats_api.create_chat import BotXAPICreateChatRequestPayload
217216
from pybotx.models.enums import ChatTypes, APIChatTypes
218217

219-
# Arrange
220-
name = "Test Chat"
221-
description = "Test Description"
222-
chat_type = ChatTypes.PERSONAL_CHAT
223-
members = [UUID("2fc83441-366a-49ba-81fc-6c39f065bb58")]
224-
shared_history = Undefined
225-
avatar = None
226-
227-
# Act
228-
payload = BotXAPICreateChatRequestPayload.from_domain(
229-
name=name,
230-
chat_type=chat_type,
231-
members=members,
232-
shared_history=shared_history,
233-
description=description,
234-
avatar=avatar,
235-
)
218+
# Test with ChatTypes enum
219+
values = {"chat_type": ChatTypes.GROUP_CHAT}
220+
result = BotXAPICreateChatRequestPayload._convert_chat_type(values) # type: ignore[operator]
221+
assert result["chat_type"] == APIChatTypes.GROUP_CHAT
236222

237-
# Assert
238-
assert payload.name == name
239-
assert payload.description == description
240-
assert payload.chat_type == APIChatTypes.CHAT
241-
assert payload.members == members
242-
243-
# Test with APIChatTypes
244-
api_chat_type = APIChatTypes.CHANNEL
245-
payload = BotXAPICreateChatRequestPayload.from_domain(
246-
name=name,
247-
chat_type=api_chat_type,
248-
members=members,
249-
shared_history=shared_history,
250-
description=description,
251-
avatar=avatar,
252-
)
253-
assert payload.chat_type == api_chat_type
223+
# Test with APIChatTypes value (should remain unchanged)
224+
values = {"chat_type": APIChatTypes.CHAT} # type: ignore[dict-item]
225+
result = BotXAPICreateChatRequestPayload._convert_chat_type(values) # type: ignore[operator]
226+
assert result["chat_type"] == APIChatTypes.CHAT
227+
228+
# Test with missing chat_type key
229+
values = {"name": "test"} # type: ignore[dict-item]
230+
result = BotXAPICreateChatRequestPayload._convert_chat_type(values) # type: ignore[operator]
231+
assert result == {"name": "test"}
254232

255233

256234
def test__create_chat_payload__serialize_chat_type_from_domain() -> None:

0 commit comments

Comments
 (0)