mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Add tests for MongoStorage
Signed-off-by: selfkilla666 <selfkilla666@yahoo.com>
This commit is contained in:
parent
92064417f5
commit
d6059a84b0
1 changed files with 74 additions and 0 deletions
74
tests/test_fsm/storage/test_mongo.py
Normal file
74
tests/test_fsm/storage/test_mongo.py
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import pytest
|
||||
|
||||
try:
|
||||
from motor.motor_asyncio import AsyncIOMotorClient
|
||||
except ImportError:
|
||||
raise ModuleNotFoundError(
|
||||
"You do not have the `motor` module installed to work with MongoDB. Install it with `pip install motor`"
|
||||
)
|
||||
|
||||
from aiogram.fsm.state import State
|
||||
from aiogram.fsm.storage.base import StorageKey
|
||||
from aiogram.fsm.storage.mongo import MongoStorage
|
||||
from aiogram.fsm.storage.mongo import AIOGRAM_DATABASE_NAME
|
||||
|
||||
|
||||
MONGO_TEST_URI: str = "mongodb+srv://bot:25sFG3vj7H2T4j5R@mongostorage-aiogram-te.pugxfxq.mongodb.net/?retryWrites=true&w=majority"
|
||||
TEST_DATABASE_NAME: str = AIOGRAM_DATABASE_NAME
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mongo_storage():
|
||||
client = AsyncIOMotorClient(MONGO_TEST_URI)
|
||||
yield MongoStorage(client, database_name=TEST_DATABASE_NAME)
|
||||
client.close()
|
||||
|
||||
|
||||
class TestMongoStorage:
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_and_get_state(self, mongo_storage):
|
||||
chat_id = 123
|
||||
user_id = 456
|
||||
bot_id = 789
|
||||
key = StorageKey(chat_id=chat_id, user_id=user_id, bot_id=bot_id)
|
||||
|
||||
# Set state
|
||||
await mongo_storage.set_state(key, State("some_state"))
|
||||
|
||||
# Get state
|
||||
result = await mongo_storage.get_state(key)
|
||||
|
||||
assert result == State("some_state")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_and_get_data(self, mongo_storage):
|
||||
chat_id = 123
|
||||
user_id = 456
|
||||
bot_id = 789
|
||||
key = StorageKey(chat_id=chat_id, user_id=user_id, bot_id=bot_id)
|
||||
|
||||
# Set data
|
||||
await mongo_storage.set_data(key, {"key1": "value1", "key2": "value2"})
|
||||
|
||||
# Get data
|
||||
result = await mongo_storage.get_data(key)
|
||||
|
||||
assert result == {"key1": "value1", "key2": "value2"}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_data(self, mongo_storage):
|
||||
chat_id = 123
|
||||
user_id = 456
|
||||
bot_id = 789
|
||||
key = StorageKey(chat_id=chat_id, user_id=user_id, bot_id=bot_id)
|
||||
|
||||
# Set initial data
|
||||
await mongo_storage.set_data(key, {"key1": "value1"})
|
||||
|
||||
# Update data
|
||||
await mongo_storage.update_data(key, {"key2": "value2"})
|
||||
|
||||
# Get updated data
|
||||
result = await mongo_storage.get_data(key)
|
||||
|
||||
assert result == {"key1": "value1", "key2": "value2"}
|
||||
Loading…
Add table
Add a link
Reference in a new issue