storage cleanup (#1144)

* storage cleanup

* storage cleanup
This commit is contained in:
RootShinobi 2023-04-08 18:01:11 +03:00 committed by GitHub
parent d8a977f357
commit dbaf6fabcb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 54 additions and 75 deletions

View file

@ -5,7 +5,7 @@ from tests.mocked_bot import MockedBot
@pytest.fixture(name="storage_key")
def create_storate_key(bot: MockedBot):
def create_storage_key(bot: MockedBot):
return StorageKey(chat_id=-42, user_id=42, bot_id=bot.id)
@ -20,9 +20,8 @@ def create_storate_key(bot: MockedBot):
class TestIsolations:
async def test_lock(
self,
bot: MockedBot,
isolation: BaseEventIsolation,
storage_key: StorageKey,
):
async with isolation.lock(bot=bot, key=storage_key):
async with isolation.lock(key=storage_key):
assert True, "You are kidding me?"

View file

@ -5,7 +5,7 @@ from tests.mocked_bot import MockedBot
@pytest.fixture(name="storage_key")
def create_storate_key(bot: MockedBot):
def create_storage_key(bot: MockedBot):
return StorageKey(chat_id=-42, user_id=42, bot_id=bot.id)
@ -15,33 +15,31 @@ def create_storate_key(bot: MockedBot):
)
class TestStorages:
async def test_set_state(self, bot: MockedBot, storage: BaseStorage, storage_key: StorageKey):
assert await storage.get_state(bot=bot, key=storage_key) is None
assert await storage.get_state(key=storage_key) is None
await storage.set_state(bot=bot, key=storage_key, state="state")
assert await storage.get_state(bot=bot, key=storage_key) == "state"
await storage.set_state(bot=bot, key=storage_key, state=None)
assert await storage.get_state(bot=bot, key=storage_key) is None
await storage.set_state(key=storage_key, state="state")
assert await storage.get_state(key=storage_key) == "state"
await storage.set_state(key=storage_key, state=None)
assert await storage.get_state(key=storage_key) is None
async def test_set_data(self, bot: MockedBot, storage: BaseStorage, storage_key: StorageKey):
assert await storage.get_data(bot=bot, key=storage_key) == {}
assert await storage.get_data(key=storage_key) == {}
await storage.set_data(bot=bot, key=storage_key, data={"foo": "bar"})
assert await storage.get_data(bot=bot, key=storage_key) == {"foo": "bar"}
await storage.set_data(bot=bot, key=storage_key, data={})
assert await storage.get_data(bot=bot, key=storage_key) == {}
await storage.set_data(key=storage_key, data={"foo": "bar"})
assert await storage.get_data(key=storage_key) == {"foo": "bar"}
await storage.set_data(key=storage_key, data={})
assert await storage.get_data(key=storage_key) == {}
async def test_update_data(
self, bot: MockedBot, storage: BaseStorage, storage_key: StorageKey
):
assert await storage.get_data(bot=bot, key=storage_key) == {}
assert await storage.update_data(bot=bot, key=storage_key, data={"foo": "bar"}) == {
"foo": "bar"
}
assert await storage.update_data(bot=bot, key=storage_key, data={"baz": "spam"}) == {
assert await storage.get_data(key=storage_key) == {}
assert await storage.update_data(key=storage_key, data={"foo": "bar"}) == {"foo": "bar"}
assert await storage.update_data(key=storage_key, data={"baz": "spam"}) == {
"foo": "bar",
"baz": "spam",
}
assert await storage.get_data(bot=bot, key=storage_key) == {
assert await storage.get_data(key=storage_key) == {
"foo": "bar",
"baz": "spam",
}

View file

@ -13,7 +13,7 @@ def state(bot: MockedBot):
ctx = storage.storage[key]
ctx.state = "test"
ctx.data = {"foo": "bar"}
return FSMContext(bot=bot, storage=storage, key=key)
return FSMContext(storage=storage, key=key)
class TestFSMContext:
@ -22,15 +22,9 @@ class TestFSMContext:
ctx = storage.storage[StorageKey(chat_id=-42, user_id=42, bot_id=bot.id)]
ctx.state = "test"
ctx.data = {"foo": "bar"}
state = FSMContext(
bot=bot, storage=storage, key=StorageKey(chat_id=-42, user_id=42, bot_id=bot.id)
)
state2 = FSMContext(
bot=bot, storage=storage, key=StorageKey(chat_id=42, user_id=42, bot_id=bot.id)
)
state3 = FSMContext(
bot=bot, storage=storage, key=StorageKey(chat_id=69, user_id=69, bot_id=bot.id)
)
state = FSMContext(storage=storage, key=StorageKey(chat_id=-42, user_id=42, bot_id=bot.id))
state2 = FSMContext(storage=storage, key=StorageKey(chat_id=42, user_id=42, bot_id=bot.id))
state3 = FSMContext(storage=storage, key=StorageKey(chat_id=69, user_id=69, bot_id=bot.id))
assert await state.get_state() == "test"
assert await state2.get_state() is None

View file

@ -178,9 +178,7 @@ class TestFSMI18nMiddleware:
async def test_middleware(self, i18n: I18n, bot: MockedBot, extra):
middleware = FSMI18nMiddleware(i18n=i18n)
storage = MemoryStorage()
state = FSMContext(
bot=bot, storage=storage, key=StorageKey(user_id=42, chat_id=42, bot_id=bot.id)
)
state = FSMContext(storage=storage, key=StorageKey(user_id=42, chat_id=42, bot_id=bot.id))
data = {
"event_from_user": User(id=42, is_bot=False, language_code="it", first_name="Test"),
"state": state,