Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions pybotx/client/chats_api/create_chat.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import Any, Dict, List, Literal, Optional, Set, Union
from typing import List, Literal, Optional, Set, Union
from uuid import UUID

from pydantic import (
Field,
ConfigDict,
field_serializer,
field_validator,
model_validator,
)

from pybotx.client.authorized_botx_method import AuthorizedBotXMethod
Expand Down Expand Up @@ -36,12 +35,11 @@ class BotXAPICreateChatRequestPayload(UnverifiedPayloadBaseModel):
shared_history: Missing[bool]
avatar: Optional[str] = None

@model_validator(mode="before")
def _convert_chat_type(cls, values: Dict[str, Any]) -> Dict[str, Any]:
ct = values.get("chat_type")
if isinstance(ct, ChatTypes):
values["chat_type"] = convert_chat_type_from_domain(ct)
return values
@field_validator("chat_type", mode="before")
def _convert_chat_type(cls, v: Union[APIChatTypes, ChatTypes]) -> APIChatTypes:
if isinstance(v, ChatTypes):
return convert_chat_type_from_domain(v)
return v

@field_validator("avatar")
def _validate_avatar(cls, v: Optional[str]) -> Optional[str]:
Expand Down
10 changes: 4 additions & 6 deletions tests/client/chats_api/test_create_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,12 @@ def test__create_chat_payload__convert_chat_type_validator() -> None:
from pybotx.models.enums import ChatTypes, APIChatTypes

# Test with ChatTypes enum
values = {"chat_type": ChatTypes.GROUP_CHAT}
result = BotXAPICreateChatRequestPayload._convert_chat_type(values) # type: ignore[operator]
assert result["chat_type"] == APIChatTypes.GROUP_CHAT
result = BotXAPICreateChatRequestPayload._convert_chat_type(ChatTypes.GROUP_CHAT) # type: ignore[operator]
Copy link
Contributor

@vladimirgubarik vladimirgubarik Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не совсем понимаю, зачем тестировать проткетед метод модели.
Это создает хрупкие тесты. Если ты станешь валидировать как-то по другому(например в рут валидаторе), то придется переписывать и этот тест.
К тому же, если кто-то вдруг нечаянно уберет декоратор с этого метода модели - тест все равно будет проходить .
Лучше протестировать всю работу создания модели, так будет надежнее.

assert result == APIChatTypes.GROUP_CHAT

# Test with non-ChatTypes value (should remain unchanged)
values = {"chat_type": APIChatTypes.CHAT} # type: ignore[dict-item]
result = BotXAPICreateChatRequestPayload._convert_chat_type(values) # type: ignore[operator]
assert result["chat_type"] == APIChatTypes.CHAT
result = BotXAPICreateChatRequestPayload._convert_chat_type(APIChatTypes.CHAT) # type: ignore[operator]
assert result == APIChatTypes.CHAT

# Test with missing chat_type key
values = {"name": "test"} # type: ignore[dict-item]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это рудимент от предыдущей реализации валидатора

Expand Down