From d646e198520e4076f713ca841f45f4e07e2391eb Mon Sep 17 00:00:00 2001 From: Cyril Margorin Date: Wed, 1 Dec 2021 03:43:12 +0300 Subject: [PATCH] Fix fsmcontextproxy update (#755) * Create test case for invalid 'FSMContextProxy.update()' function * Fix invalid 'FSMContextProxy.update()' function * Verify that written data has been stored and read back correctly --- aiogram/dispatcher/storage.py | 2 ++ tests/test_dispatcher/test_fsm_context.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/test_dispatcher/test_fsm_context.py diff --git a/aiogram/dispatcher/storage.py b/aiogram/dispatcher/storage.py index 340b6352..63ce25b2 100644 --- a/aiogram/dispatcher/storage.py +++ b/aiogram/dispatcher/storage.py @@ -408,6 +408,8 @@ class FSMContextProxy: def update(self, data=None, **kwargs): self._check_closed() + if data is None: + data = {} self._data.update(data, **kwargs) def pop(self, key, default=None): diff --git a/tests/test_dispatcher/test_fsm_context.py b/tests/test_dispatcher/test_fsm_context.py new file mode 100644 index 00000000..3189e4e6 --- /dev/null +++ b/tests/test_dispatcher/test_fsm_context.py @@ -0,0 +1,14 @@ +import pytest +from aiogram.contrib.fsm_storage.memory import MemoryStorage +from aiogram.dispatcher import FSMContext + + +class TestFSMContext: + @pytest.mark.asyncio + async def test_update_data(self): + context = FSMContext(MemoryStorage(), chat=1, user=1) + async with context.proxy() as data: + data.update(key1="value1", key2="value2") + async with context.proxy() as data: + assert data['key1'] == "value1" + assert data['key2'] == "value2"