mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Cover base and memory storage
This commit is contained in:
parent
becbcecaf1
commit
03ccebd8be
3 changed files with 43 additions and 5 deletions
|
|
@ -10,23 +10,27 @@ StateType = Optional[Union[str, State]]
|
||||||
class BaseStorage(ABC):
|
class BaseStorage(ABC):
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lock(self) -> AsyncGenerator[None, None]:
|
async def lock(self) -> AsyncGenerator[None, None]: # pragma: no cover
|
||||||
yield None
|
yield None
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def set_state(self, chat_id: int, user_id: int, state: StateType = None) -> None:
|
async def set_state(
|
||||||
|
self, chat_id: int, user_id: int, state: StateType = None
|
||||||
|
) -> None: # pragma: no cover
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def get_state(self, chat_id: int, user_id: int) -> Optional[str]:
|
async def get_state(self, chat_id: int, user_id: int) -> Optional[str]: # pragma: no cover
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def set_data(self, chat_id: int, user_id: int, data: Dict[str, Any]) -> None:
|
async def set_data(
|
||||||
|
self, chat_id: int, user_id: int, data: Dict[str, Any]
|
||||||
|
) -> None: # pragma: no cover
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def get_data(self, chat_id: int, user_id: int) -> Dict[str, Any]:
|
async def get_data(self, chat_id: int, user_id: int) -> Dict[str, Any]: # pragma: no cover
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def update_data(
|
async def update_data(
|
||||||
|
|
|
||||||
0
tests/test_dispatcher/fsm/storage/__init__.py
Normal file
0
tests/test_dispatcher/fsm/storage/__init__.py
Normal file
34
tests/test_dispatcher/fsm/storage/test_base.py
Normal file
34
tests/test_dispatcher/fsm/storage/test_base.py
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from aiogram.dispatcher.fsm.storage.memory import MemoryStorage, MemoryStorageRecord
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def storage():
|
||||||
|
return MemoryStorage()
|
||||||
|
|
||||||
|
|
||||||
|
class TestMemoryStorage:
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_set_state(self, storage: MemoryStorage):
|
||||||
|
assert await storage.get_state(chat_id=-42, user_id=42) is None
|
||||||
|
|
||||||
|
await storage.set_state(chat_id=-42, user_id=42, state="state")
|
||||||
|
assert await storage.get_state(chat_id=-42, user_id=42) == "state"
|
||||||
|
|
||||||
|
assert -42 in storage.storage
|
||||||
|
assert 42 in storage.storage[-42]
|
||||||
|
assert isinstance(storage.storage[-42][42], MemoryStorageRecord)
|
||||||
|
assert storage.storage[-42][42].state == "state"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_set_data(self, storage: MemoryStorage):
|
||||||
|
assert await storage.get_data(chat_id=-42, user_id=42) == {}
|
||||||
|
|
||||||
|
await storage.set_data(chat_id=-42, user_id=42, data={"foo": "bar"})
|
||||||
|
assert await storage.get_data(chat_id=-42, user_id=42) == {"foo": "bar"}
|
||||||
|
|
||||||
|
assert -42 in storage.storage
|
||||||
|
assert 42 in storage.storage[-42]
|
||||||
|
assert isinstance(storage.storage[-42][42], MemoryStorageRecord)
|
||||||
|
assert storage.storage[-42][42].data == {"foo": "bar"}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue