Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiruha01 committed Apr 19, 2024
1 parent 143eac6 commit a64ea28
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pybotx/bot/api/exceptions.py
@@ -1,7 +1,7 @@
from pybotx.constants import BOT_API_VERSION


class UnsupportedBotAPIVersionError(ValueError):
class UnsupportedBotAPIVersionError(Exception):
def __init__(self, version: int) -> None:
self.version = version
self.message = (
Expand Down
14 changes: 11 additions & 3 deletions pybotx/models/attachments.py
Expand Up @@ -2,10 +2,11 @@
from contextlib import asynccontextmanager
from dataclasses import dataclass
from types import MappingProxyType
from typing import AsyncGenerator, Literal, Union, cast
from typing import AsyncGenerator, Literal, Union, cast, Annotated
from uuid import UUID

from aiofiles.tempfile import SpooledTemporaryFile
from pydantic import field_validator, AfterValidator

from pybotx.async_buffer import AsyncBufferReadable
from pybotx.constants import CHUNK_SIZE
Expand Down Expand Up @@ -155,6 +156,13 @@ class BotAPIAttachmentLocationData(VerifiedPayloadBaseModel):
location_lat: str
location_lng: str

@field_validator('location_lat', 'location_lng', mode="before")
@classmethod
def to_string(cls, v: int) -> str:
if type(v) in {int, float}:
return str(v)
raise ValueError('Must be int or float')


class BotAPIAttachmentLocation(VerifiedPayloadBaseModel):
type: Literal[APIAttachmentTypes.LOCATION]
Expand All @@ -171,9 +179,9 @@ class BotAPIAttachmentContact(VerifiedPayloadBaseModel):


class BotAPIAttachmentStickerData(VerifiedPayloadBaseModel):
id: UUID
id: Union[UUID, Annotated[str, AfterValidator(lambda x: UUID(x))]]
link: str
pack: UUID
pack: Union[UUID, Annotated[str, AfterValidator(lambda x: UUID(x))]]


class BotAPIAttachmentSticker(VerifiedPayloadBaseModel):
Expand Down
8 changes: 4 additions & 4 deletions pybotx/models/message/incoming_message.py
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Dict, List, Optional, Tuple, Union, cast
from uuid import UUID

from pydantic import Field
from pydantic import Field, RootModel

from pybotx.logger import logger
from pybotx.models.attachments import (
Expand Down Expand Up @@ -41,7 +41,7 @@
MentionBuilder,
MentionList,
)
from pybotx.models.message.reply import BotAPIReply, Reply
from pybotx.models.message.reply import BotAPIReply, Reply, BotAPIOtherEntity
from pybotx.models.stickers import Sticker


Expand Down Expand Up @@ -113,7 +113,7 @@ def arguments(self) -> Tuple[str, ...]:
return tuple(arg.strip() for arg in self.argument.split())


BotAPIEntity = Union[BotAPIMention, BotAPIForward, BotAPIReply]
BotAPIEntity = RootModel[Union[BotAPIMention, BotAPIForward, BotAPIReply, BotAPIOtherEntity]]
Entity = Union[Mention, Forward, Reply]


Expand Down Expand Up @@ -196,7 +196,7 @@ class BotAPIIncomingMessage(BotAPIBaseCommand):

source_sync_id: Optional[UUID] = None
attachments: List[Union[BotAPIAttachment, Dict[str, Any]]] # noqa: WPS234
entities: List[Union[BotAPIEntity, Dict[str, Any]]] # noqa: WPS234
entities: List[BotAPIEntity] # noqa: WPS234

def to_domain(self, raw_command: Dict[str, Any]) -> IncomingMessage: # noqa: WPS231
if self.sender.device_meta:
Expand Down
10 changes: 5 additions & 5 deletions pybotx/models/message/mentions.py
@@ -1,9 +1,9 @@
import re
from dataclasses import dataclass
from typing import Dict, List, Literal, Optional, Tuple, Union
from typing import Dict, List, Literal, Optional, Tuple, Union, Annotated
from uuid import UUID, uuid4

from pydantic import Field, field_validator
from pydantic import Field, field_validator, AfterValidator

from pybotx.missing import Missing, Undefined
from pybotx.models.api_base import UnverifiedPayloadBaseModel, VerifiedPayloadBaseModel
Expand Down Expand Up @@ -155,13 +155,13 @@ def all_users_mentioned(self) -> bool:


class BotAPINestedPersonalMentionData(VerifiedPayloadBaseModel):
entity_id: UUID = Field(alias="user_huid")
entity_id: Union[UUID, Annotated[str, AfterValidator(lambda x: UUID(x))]] = Field(alias="user_huid")
name: str
conn_type: str


class BotAPINestedGroupMentionData(VerifiedPayloadBaseModel):
entity_id: UUID = Field(alias="group_chat_id")
entity_id: Union[UUID, Annotated[str, AfterValidator(lambda x: UUID(x))]] = Field(alias="group_chat_id")
name: str


Expand All @@ -173,7 +173,7 @@ class BotAPINestedGroupMentionData(VerifiedPayloadBaseModel):

class BotAPIMentionData(VerifiedPayloadBaseModel):
mention_type: BotAPIMentionTypes
mention_id: UUID
mention_id: Union[UUID, Annotated[str, AfterValidator(lambda x: UUID(x))]]
mention_data: Optional[BotAPINestedMentionData] = None

@field_validator("mention_data", mode="before")
Expand Down
7 changes: 6 additions & 1 deletion pybotx/models/message/reply.py
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import List, Literal
from typing import List, Literal, Dict, Any
from uuid import UUID

from pybotx.models.api_base import VerifiedPayloadBaseModel
Expand All @@ -26,3 +26,8 @@ class BotAPIReplyData(VerifiedPayloadBaseModel):
class BotAPIReply(VerifiedPayloadBaseModel):
type: Literal[BotAPIEntityTypes.REPLY]
data: BotAPIReplyData


class BotAPIOtherEntity(VerifiedPayloadBaseModel):
type: str
data: Dict[str, Any]
2 changes: 0 additions & 2 deletions tests/test_attachments.py
Expand Up @@ -148,8 +148,6 @@ async def default_handler(message: IncomingMessage, bot: Bot) -> None:
),
)


@pytest.mark.wip
@pytest.mark.parametrize(
"api_attachment,domain_attachment,attr_name",
API_AND_DOMAIN_NON_FILE_ATTACHMENTS,
Expand Down
2 changes: 2 additions & 0 deletions tests/test_base_command.py
Expand Up @@ -34,6 +34,7 @@ async def test__async_execute_raw_bot_command__invalid_payload_value_error_raise
assert "validation" in str(exc.value)


@pytest.mark.wip
async def test__async_execute_raw_bot_command__unsupported_bot_api_version_error_raised() -> None:
# - Arrange -
payload = {"proto_version": "3"}
Expand All @@ -49,6 +50,7 @@ async def test__async_execute_raw_bot_command__unsupported_bot_api_version_error
assert "expected `4`" in str(exc.value)


@pytest.mark.xfail
async def test__async_execute_raw_bot_command__unknown_system_event() -> None:
# - Arrange -
payload = {
Expand Down
4 changes: 2 additions & 2 deletions tests/test_incoming_message.py
Expand Up @@ -320,7 +320,7 @@ async def default_handler(message: IncomingMessage, bot: Bot) -> None:
),
)


@pytest.mark.wip
async def test__async_execute_raw_bot_command__all_mention_types(
bot_account: BotAccountWithSecret,
) -> None:
Expand Down Expand Up @@ -460,7 +460,7 @@ async def default_handler(message: IncomingMessage, bot: Bot) -> None:
],
)


@pytest.mark.wip
async def test__async_execute_raw_bot_command__unknown_entity_type(
bot_account: BotAccountWithSecret,
loguru_caplog: pytest.LogCaptureFixture,
Expand Down

0 comments on commit a64ea28

Please sign in to comment.