Skip to content

Commit

Permalink
Cover base and memory storage
Browse files Browse the repository at this point in the history
  • Loading branch information
JrooTJunior committed May 12, 2021
1 parent becbcec commit 03ccebd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
14 changes: 9 additions & 5 deletions aiogram/dispatcher/fsm/storage/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@
class BaseStorage(ABC):
@abstractmethod
@asynccontextmanager
async def lock(self) -> AsyncGenerator[None, None]:
async def lock(self) -> AsyncGenerator[None, None]: # pragma: no cover
yield None

@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

@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

@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

@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

async def update_data(
Expand Down
Empty file.
34 changes: 34 additions & 0 deletions tests/test_dispatcher/fsm/storage/test_base.py
Original file line number Diff line number Diff line change
@@ -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"}

0 comments on commit 03ccebd

Please sign in to comment.