Skip to content

Commit 1aa5553

Browse files
fix: Fixed convert chat type in method create_chat (#522)
* fix: fixed convert chat type * deleted Optional body from EventEdit --------- Co-authored-by: Mikhail Opryshko <mikhail.opryshko@ccsteam.ru>
1 parent 397ba94 commit 1aa5553

4 files changed

Lines changed: 64 additions & 29 deletions

File tree

pybotx/bot/bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ async def create_chat(
11921192
self._bot_accounts_storage,
11931193
)
11941194

1195-
payload = BotXAPICreateChatRequestPayload(
1195+
payload = BotXAPICreateChatRequestPayload.from_domain(
11961196
name=name,
11971197
chat_type=chat_type,
11981198
members=huids,

pybotx/client/chats_api/create_chat.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
from typing import Any, Dict, List, Literal, Optional, Set, Union
1+
from typing import List, Literal, Optional, Set, Union
22
from uuid import UUID
33

4-
from pydantic import (
5-
Field,
6-
ConfigDict,
7-
field_serializer,
8-
field_validator,
9-
model_validator,
10-
)
4+
from pydantic import Field, ConfigDict, field_serializer, field_validator
115

126
from pybotx.client.authorized_botx_method import AuthorizedBotXMethod
137
from pybotx.client.botx_method import response_exception_thrower
@@ -36,12 +30,11 @@ class BotXAPICreateChatRequestPayload(UnverifiedPayloadBaseModel):
3630
shared_history: Missing[bool]
3731
avatar: Optional[str] = None
3832

39-
@model_validator(mode="before")
40-
def _convert_chat_type(cls, values: Dict[str, Any]) -> Dict[str, Any]:
41-
ct = values.get("chat_type")
42-
if isinstance(ct, ChatTypes):
43-
values["chat_type"] = convert_chat_type_from_domain(ct)
44-
return values
33+
@classmethod
34+
def _convert_chat_type(cls, v: Union[APIChatTypes, ChatTypes]) -> APIChatTypes:
35+
if isinstance(v, ChatTypes):
36+
return convert_chat_type_from_domain(v)
37+
return v
4538

4639
@field_validator("avatar")
4740
def _validate_avatar(cls, v: Optional[str]) -> Optional[str]:
@@ -59,6 +52,26 @@ def _validate_avatar(cls, v: Optional[str]) -> Optional[str]:
5952
def _serialize_chat_type(self, v: APIChatTypes) -> str:
6053
return v.value.lower()
6154

55+
@classmethod
56+
def from_domain(
57+
cls,
58+
name: str,
59+
chat_type: Union[APIChatTypes, ChatTypes],
60+
members: List[UUID],
61+
shared_history: Missing[bool],
62+
description: Optional[str] = None,
63+
avatar: Optional[str] = None,
64+
) -> "BotXAPICreateChatRequestPayload":
65+
converted_chat_type = cls._convert_chat_type(chat_type)
66+
return cls(
67+
name=name,
68+
chat_type=converted_chat_type,
69+
members=members,
70+
shared_history=shared_history,
71+
description=description,
72+
avatar=avatar,
73+
)
74+
6275

6376
class BotXAPIChatIdResult(VerifiedPayloadBaseModel):
6477
model_config = ConfigDict(frozen=True)

pybotx/models/system_events/event_edit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class EventEdit(BotCommandBase):
3737
entities: Entities from updated message.
3838
"""
3939

40-
body: Optional[str]
40+
body: str
4141
sync_id: UUID
4242
chat_id: UUID
4343
huid: UUID

tests/client/chats_api/test_create_chat.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
HandlerCollector,
1515
lifespan_wrapper,
1616
)
17+
from pybotx.missing import Undefined
1718

1819
pytestmark = [
1920
pytest.mark.asyncio,
@@ -204,20 +205,41 @@ def test__create_chat_payload__convert_chat_type_validator() -> None:
204205
from pybotx.client.chats_api.create_chat import BotXAPICreateChatRequestPayload
205206
from pybotx.models.enums import ChatTypes, APIChatTypes
206207

207-
# Test with ChatTypes enum
208-
values = {"chat_type": ChatTypes.GROUP_CHAT}
209-
result = BotXAPICreateChatRequestPayload._convert_chat_type(values) # type: ignore[operator]
210-
assert result["chat_type"] == APIChatTypes.GROUP_CHAT
211-
212-
# Test with non-ChatTypes value (should remain unchanged)
213-
values = {"chat_type": APIChatTypes.CHAT} # type: ignore[dict-item]
214-
result = BotXAPICreateChatRequestPayload._convert_chat_type(values) # type: ignore[operator]
215-
assert result["chat_type"] == APIChatTypes.CHAT
208+
# Arrange
209+
name = "Test Chat"
210+
description = "Test Description"
211+
chat_type = ChatTypes.PERSONAL_CHAT
212+
members = [UUID("2fc83441-366a-49ba-81fc-6c39f065bb58")]
213+
shared_history = Undefined
214+
avatar = None
215+
216+
# Act
217+
payload = BotXAPICreateChatRequestPayload.from_domain(
218+
name=name,
219+
chat_type=chat_type,
220+
members=members,
221+
shared_history=shared_history,
222+
description=description,
223+
avatar=avatar,
224+
)
216225

217-
# Test with missing chat_type key
218-
values = {"name": "test"} # type: ignore[dict-item]
219-
result = BotXAPICreateChatRequestPayload._convert_chat_type(values) # type: ignore[operator]
220-
assert result == {"name": "test"}
226+
# Assert
227+
assert payload.name == name
228+
assert payload.description == description
229+
assert payload.chat_type == APIChatTypes.CHAT
230+
assert payload.members == members
231+
232+
# Test with APIChatTypes
233+
api_chat_type = APIChatTypes.CHANNEL
234+
payload = BotXAPICreateChatRequestPayload.from_domain(
235+
name=name,
236+
chat_type=api_chat_type,
237+
members=members,
238+
shared_history=shared_history,
239+
description=description,
240+
avatar=avatar,
241+
)
242+
assert payload.chat_type == api_chat_type
221243

222244

223245
async def test__create_chat__with_valid_avatar_succeed(

0 commit comments

Comments
 (0)