Fail redis and mongo tests if incorrect URI provided + some storages tests refactoring (#1510)

* Smaller timeout for MongoStorage connection

By default serverSelectionTimeoutMS=30000. This is too much

* Correct ConnectionError for RedisStorage in tests

* Remove unused import in conftest.py

* Refactor skipping redis and mongo tests

* Fail redis and mongo tests if incorrect URI

If incorrect URIs provided to "--redis" and/or "--mongo" options
tests should fail with ERRORs instead of skipping.
Otherwise the next scenario is possible:
  1) developer breaks RedisStorage and/or MongoStorage code
  2) tests are run with incorrect redis and/or mongo URIs
     provided by "--redis" and "--mongo" options.
     For example, wrong port specified.
  3) tests pass because skipping doesn't fail tests run
  4) developer or reviewer doesn't notice
     that redis and/or mongo tests were skipped
  5) broken code gets in codebase

* Remove unused fixtures passing in storages tests

* Define create_storage_key fixture in conftest.py

* Linters formatting

* Changes description

* Revert "Smaller timeout for MongoStorage connection"

This reverts commit d88b7ec612.

* Smaller timeout for MongoStorage connection in tests

The default 30s timeout is too long

* Add test for MongoStorage for 100% coverage

* Linters formatting

* Move skipping redis/mongo tests in earlier fixtures

* Replace vars with constants in conftest.py

* Linters formatting
This commit is contained in:
Rishat-F 2024-06-17 00:55:59 +03:00 committed by GitHub
parent 7760ab1d0d
commit 1df3adaba1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 60 additions and 50 deletions

View file

@ -5,12 +5,6 @@ import pytest
from aiogram.fsm.storage.base import BaseEventIsolation, StorageKey
from aiogram.fsm.storage.redis import RedisEventIsolation, RedisStorage
from tests.mocked_bot import MockedBot
@pytest.fixture(name="storage_key")
def create_storage_key(bot: MockedBot):
return StorageKey(chat_id=-42, user_id=42, bot_id=bot.id)
@pytest.mark.parametrize(

View file

@ -1,17 +1,19 @@
import pytest
from pymongo.errors import PyMongoError
from aiogram.fsm.state import State
from aiogram.fsm.storage.mongo import MongoStorage, StorageKey
from tests.mocked_bot import MockedBot
from tests.conftest import CHAT_ID, USER_ID
PREFIX = "fsm"
CHAT_ID = -42
USER_ID = 42
@pytest.fixture(name="storage_key")
def create_storage_key(bot: MockedBot):
return StorageKey(chat_id=CHAT_ID, user_id=USER_ID, bot_id=bot.id)
async def test_get_storage_passing_only_url(mongo_server):
storage = MongoStorage.from_url(url=mongo_server)
try:
await storage._client.server_info()
except PyMongoError as e:
pytest.fail(str(e))
async def test_update_not_existing_data_with_empty_dictionary(

View file

@ -1,12 +1,6 @@
import pytest
from aiogram.fsm.storage.base import BaseStorage, StorageKey
from tests.mocked_bot import MockedBot
@pytest.fixture(name="storage_key")
def create_storage_key(bot: MockedBot):
return StorageKey(chat_id=-42, user_id=42, bot_id=bot.id)
@pytest.mark.parametrize(
@ -18,7 +12,7 @@ def create_storage_key(bot: MockedBot):
],
)
class TestStorages:
async def test_set_state(self, bot: MockedBot, storage: BaseStorage, storage_key: StorageKey):
async def test_set_state(self, storage: BaseStorage, storage_key: StorageKey):
assert await storage.get_state(key=storage_key) is None
await storage.set_state(key=storage_key, state="state")
@ -26,7 +20,7 @@ class TestStorages:
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):
async def test_set_data(self, storage: BaseStorage, storage_key: StorageKey):
assert await storage.get_data(key=storage_key) == {}
await storage.set_data(key=storage_key, data={"foo": "bar"})
@ -34,9 +28,7 @@ class TestStorages:
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
):
async def test_update_data(self, storage: BaseStorage, storage_key: StorageKey):
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={}) == {"foo": "bar"}