From 6bebd109bbd5930adc560a3aa6c5d87eacb82b6b Mon Sep 17 00:00:00 2001 From: stijndcl Date: Mon, 25 Jul 2022 21:08:06 +0200 Subject: [PATCH] Set up mongo connection in pytest & fix authentication url --- database/engine.py | 9 ++++++--- tests/conftest.py | 12 +++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/database/engine.py b/database/engine.py index e73bc27..e98c8df 100644 --- a/database/engine.py +++ b/database/engine.py @@ -7,14 +7,14 @@ from sqlalchemy.orm import sessionmaker import settings -encoded_password = quote_plus(settings.POSTGRES_PASS) +encoded_postgres_password = quote_plus(settings.POSTGRES_PASS) # PostgreSQL engine postgres_engine = create_async_engine( URL.create( drivername="postgresql+asyncpg", username=settings.POSTGRES_USER, - password=encoded_password, + password=encoded_postgres_password, host=settings.POSTGRES_HOST, port=settings.POSTGRES_PORT, database=settings.POSTGRES_DB, @@ -28,4 +28,7 @@ DBSession = sessionmaker( ) # 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) diff --git a/tests/conftest.py b/tests/conftest.py index c218524..55919fd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,10 +2,12 @@ import asyncio from typing import AsyncGenerator, Generator from unittest.mock import MagicMock +import motor.motor_asyncio import pytest 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 didier import Didier @@ -54,6 +56,14 @@ async def postgres(tables) -> AsyncGenerator[AsyncSession, None]: 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 def mock_client() -> Didier: """Fixture to get a mock Didier instance