Set up mongo connection in pytest & fix authentication url

pull/123/head
stijndcl 2022-07-25 21:08:06 +02:00
parent 52b452c85a
commit 6bebd109bb
2 changed files with 17 additions and 4 deletions

View File

@ -7,14 +7,14 @@ from sqlalchemy.orm import sessionmaker
import settings import settings
encoded_password = quote_plus(settings.POSTGRES_PASS) encoded_postgres_password = quote_plus(settings.POSTGRES_PASS)
# PostgreSQL engine # PostgreSQL engine
postgres_engine = create_async_engine( postgres_engine = create_async_engine(
URL.create( URL.create(
drivername="postgresql+asyncpg", drivername="postgresql+asyncpg",
username=settings.POSTGRES_USER, username=settings.POSTGRES_USER,
password=encoded_password, password=encoded_postgres_password,
host=settings.POSTGRES_HOST, host=settings.POSTGRES_HOST,
port=settings.POSTGRES_PORT, port=settings.POSTGRES_PORT,
database=settings.POSTGRES_DB, database=settings.POSTGRES_DB,
@ -28,4 +28,7 @@ DBSession = sessionmaker(
) )
# MongoDB client # MongoDB client
mongo_client = motor.motor_asyncio.AsyncIOMotorClient(settings.MONGO_HOST, settings.MONGO_PORT) encoded_mongo_username = quote_plus(settings.MONGO_USER)
encoded_mongo_password = quote_plus(settings.MONGO_PASS)
mongo_url = f"mongodb://{encoded_mongo_username}:{encoded_mongo_password}@{settings.MONGO_HOST}:{settings.MONGO_PORT}/"
mongo_client = motor.motor_asyncio.AsyncIOMotorClient(mongo_url)

View File

@ -2,10 +2,12 @@ import asyncio
from typing import AsyncGenerator, Generator from typing import AsyncGenerator, Generator
from unittest.mock import MagicMock from unittest.mock import MagicMock
import motor.motor_asyncio
import pytest import pytest
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from database.engine import postgres_engine import settings
from database.engine import mongo_client, postgres_engine
from database.migrations import ensure_latest_migration, migrate from database.migrations import ensure_latest_migration, migrate
from didier import Didier from didier import Didier
@ -54,6 +56,14 @@ async def postgres(tables) -> AsyncGenerator[AsyncSession, None]:
await connection.close() await connection.close()
@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)
@pytest.fixture @pytest.fixture
def mock_client() -> Didier: def mock_client() -> Didier:
"""Fixture to get a mock Didier instance """Fixture to get a mock Didier instance