"Add get_mounted_bot function and improve model comparison in tests"

In this commit, a new function `get_mounted_bot` was added to `context_controller.py` that returns the bot mounted in context. This function was needed to bypass the limitation in pydantic BaseModel's properties, which neither support computed fields nor serialization/validation.

Various tests were also updated to compare models using `model_dump_json()` method rather than comparing the models directly. This change provides more accurate comparisons by considering default values in the models.

Further, the dispatcher was adjusted to enforce update re-mounting if the mounted bot differs from the current update. This allows shortcuts to be used in the bot's current instance and ensures the correct propagation of the context to all the nested objects and attributes of Updates.
This commit is contained in:
Alex Root Junior 2023-07-16 22:46:45 +03:00
parent 31c11c31e0
commit 74e00a30b1
No known key found for this signature in database
GPG key ID: 074C1D455EBEA4AC
4 changed files with 31 additions and 7 deletions

View file

@ -460,7 +460,9 @@ class TestDispatcher:
@observer()
async def my_handler(event: Any, **kwargs: Any):
assert event == getattr(update, event_type)
assert event.model_dump(exclude_defaults=True) == getattr(
update, event_type
).model_dump(exclude_defaults=True)
if has_chat:
assert kwargs["event_chat"]
if has_user:
@ -469,7 +471,9 @@ class TestDispatcher:
result = await router.feed_update(bot, update, test="PASS")
assert isinstance(result, dict)
assert result["event_update"] == update
assert result["event_update"].model_dump(exclude_defaults=True) == update.model_dump(
exclude_defaults=True
)
assert result["event_router"] == router
assert result["test"] == "PASS"
@ -532,7 +536,9 @@ class TestDispatcher:
)
result = await dp.feed_update(bot, update, test="PASS")
assert isinstance(result, dict)
assert result["event_update"] == update
assert result["event_update"].model_dump(exclude_defaults=True) == update.model_dump(
exclude_defaults=True
)
assert result["event_router"] == router1
assert result["test"] == "PASS"