mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
refactor(backend): implement default value handling in serialization
The `json_serialize` method has been added to the `TelegramMethod` and `TelegramObject` classes to replace `Default` placeholders with actual values from the bot's defaults during JSON serialization. This change ensures that non-standard objects are handled correctly, maintaining backward compatibility for built-in pydantic json serialization. This modification is beneficial as it centralizes the handling of default values when serializing objects to JSON, making the code more maintainable and robust against future changes in serialization logic.
This commit is contained in:
parent
7653895dd2
commit
6f4452f4e0
3 changed files with 71 additions and 7 deletions
|
|
@ -1,9 +1,11 @@
|
|||
from typing import Any, Dict
|
||||
from unittest.mock import sentinel
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.methods import GetMe, TelegramMethod
|
||||
from aiogram.types import TelegramObject, User
|
||||
from aiogram.client.default import Default
|
||||
from aiogram.methods import GetMe, SendMessage, TelegramMethod
|
||||
from aiogram.types import LinkPreviewOptions, TelegramObject, User
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
|
||||
|
|
@ -26,6 +28,25 @@ class TestTelegramMethodRemoveUnset:
|
|||
assert obj.remove_unset("") == ""
|
||||
|
||||
|
||||
class TestTelegramMethodJsonSerialize:
|
||||
@pytest.mark.parametrize(
|
||||
"obj",
|
||||
[
|
||||
SendMessage(
|
||||
chat_id=1,
|
||||
text="test",
|
||||
),
|
||||
LinkPreviewOptions(),
|
||||
],
|
||||
)
|
||||
def test_json_serialize(self, obj):
|
||||
def has_defaults(dump: Dict[str, Any]) -> bool:
|
||||
return any(isinstance(value, Default) for value in dump.values())
|
||||
|
||||
assert has_defaults(obj.model_dump())
|
||||
assert not has_defaults(obj.model_dump(mode="json"))
|
||||
|
||||
|
||||
class TestTelegramMethodCall:
|
||||
async def test_async_emit_unsuccessful(self, bot: MockedBot):
|
||||
with pytest.raises(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue