2022-06-22 02:05:04 +02:00
|
|
|
import asyncio
|
|
|
|
from typing import AsyncGenerator, Generator
|
2022-06-21 21:06:11 +02:00
|
|
|
from unittest.mock import MagicMock
|
2022-06-17 01:02:36 +02:00
|
|
|
|
2022-07-25 21:08:06 +02:00
|
|
|
import motor.motor_asyncio
|
2022-06-17 00:43:55 +02:00
|
|
|
import pytest
|
2022-06-17 01:02:36 +02:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
2022-07-25 21:08:06 +02:00
|
|
|
import settings
|
|
|
|
from database.engine import mongo_client, postgres_engine
|
2022-07-19 18:49:22 +02:00
|
|
|
from database.migrations import ensure_latest_migration, migrate
|
2022-06-21 21:06:11 +02:00
|
|
|
from didier import Didier
|
2022-06-17 00:43:55 +02:00
|
|
|
|
|
|
|
|
2022-07-19 18:49:22 +02:00
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
2022-06-22 02:05:04 +02:00
|
|
|
def event_loop() -> Generator:
|
2022-07-19 23:35:41 +02:00
|
|
|
"""Fixture to change the event loop
|
|
|
|
|
|
|
|
This fixes a lot of headaches during async tests
|
|
|
|
"""
|
2022-06-22 02:05:04 +02:00
|
|
|
loop = asyncio.get_event_loop_policy().new_event_loop()
|
|
|
|
yield loop
|
|
|
|
loop.close()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
2022-06-30 15:20:54 +02:00
|
|
|
async def tables():
|
2022-07-23 20:59:02 +02:00
|
|
|
"""Fixture to initialize a database before the tests, and then tear it down again
|
2022-07-19 18:49:22 +02:00
|
|
|
|
|
|
|
Checks that the migrations were successful by asserting that we are currently
|
|
|
|
on the latest migration
|
|
|
|
"""
|
|
|
|
await migrate(up=True)
|
|
|
|
await ensure_latest_migration()
|
|
|
|
yield
|
|
|
|
await migrate(up=False)
|
2022-06-17 01:02:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2022-07-25 19:12:27 +02:00
|
|
|
async def postgres(tables) -> AsyncGenerator[AsyncSession, None]:
|
2022-06-17 01:02:36 +02:00
|
|
|
"""Fixture to create a session for every test
|
2022-07-19 23:35:41 +02:00
|
|
|
|
2022-06-17 01:02:36 +02:00
|
|
|
Rollbacks the transaction afterwards so that the future tests start with a clean database
|
|
|
|
"""
|
2022-07-25 20:33:20 +02:00
|
|
|
connection = await postgres_engine.connect()
|
2022-06-17 01:02:36 +02:00
|
|
|
transaction = await connection.begin()
|
|
|
|
session = AsyncSession(bind=connection, expire_on_commit=False)
|
|
|
|
|
|
|
|
yield session
|
|
|
|
|
|
|
|
# Clean up session & rollback transactions
|
|
|
|
await session.close()
|
|
|
|
if transaction.is_valid:
|
|
|
|
await transaction.rollback()
|
|
|
|
|
|
|
|
await connection.close()
|
2022-06-21 21:06:11 +02:00
|
|
|
|
|
|
|
|
2022-07-25 21:08:06 +02:00
|
|
|
@pytest.fixture
|
|
|
|
async def mongodb() -> motor.motor_asyncio.AsyncIOMotorDatabase:
|
|
|
|
"""Fixture to get a MongoDB connection"""
|
|
|
|
database = mongo_client[settings.MONGO_DB]
|
|
|
|
yield database
|
|
|
|
mongo_client.drop_database(settings.MONGO_DB)
|
|
|
|
|
|
|
|
|
2022-06-21 21:06:11 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_client() -> Didier:
|
|
|
|
"""Fixture to get a mock Didier instance
|
2022-07-19 23:35:41 +02:00
|
|
|
|
2022-06-21 21:06:11 +02:00
|
|
|
The mock uses 0 as the id
|
|
|
|
"""
|
|
|
|
mock_client = MagicMock()
|
|
|
|
mock_user = MagicMock()
|
|
|
|
mock_user.id = 0
|
|
|
|
mock_client.user = mock_user
|
|
|
|
|
|
|
|
return mock_client
|