Cover base and memory storage

This commit is contained in:
Alex Root Junior 2021-05-13 01:54:07 +03:00
parent becbcecaf1
commit 03ccebd8be
3 changed files with 43 additions and 5 deletions

View file

@ -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(

View 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"}