""" Tests pour AppSettingRepository. """ import pytest from sqlalchemy.ext.asyncio import AsyncSession pytestmark = pytest.mark.unit class TestAppSettingRepository: """Tests pour AppSettingRepository.""" @pytest.mark.asyncio async def test_set_value_creates_new(self, db_session: AsyncSession): """Création d'un nouveau paramètre.""" from app.crud.app_setting import AppSettingRepository repo = AppSettingRepository(db_session) setting = await repo.set_value("test_key", "test_value") await db_session.commit() assert setting.key == "test_key" assert setting.value == "test_value" @pytest.mark.asyncio async def test_set_value_updates_existing(self, db_session: AsyncSession): """Mise à jour d'un paramètre existant.""" from app.crud.app_setting import AppSettingRepository repo = AppSettingRepository(db_session) await repo.set_value("update_key", "old_value") await db_session.commit() updated = await repo.set_value("update_key", "new_value") await db_session.commit() assert updated.value == "new_value" @pytest.mark.asyncio async def test_get_existing_setting(self, db_session: AsyncSession): """Récupération d'un paramètre existant.""" from app.crud.app_setting import AppSettingRepository repo = AppSettingRepository(db_session) await repo.set_value("get_key", "get_value") await db_session.commit() setting = await repo.get("get_key") assert setting is not None assert setting.value == "get_value" @pytest.mark.asyncio async def test_get_nonexistent_setting(self, db_session: AsyncSession): """Récupération d'un paramètre inexistant.""" from app.crud.app_setting import AppSettingRepository repo = AppSettingRepository(db_session) setting = await repo.get("nonexistent_key") assert setting is None @pytest.mark.asyncio async def test_get_value_returns_value(self, db_session: AsyncSession): """get_value retourne la valeur.""" from app.crud.app_setting import AppSettingRepository repo = AppSettingRepository(db_session) await repo.set_value("value_key", "the_value") await db_session.commit() value = await repo.get_value("value_key") assert value == "the_value" @pytest.mark.asyncio async def test_get_value_returns_default_when_missing(self, db_session: AsyncSession): """get_value retourne la valeur par défaut si clé manquante.""" from app.crud.app_setting import AppSettingRepository repo = AppSettingRepository(db_session) value = await repo.get_value("missing_key", default="default_value") assert value == "default_value" @pytest.mark.asyncio async def test_get_value_returns_default_when_null(self, db_session: AsyncSession): """get_value retourne la valeur par défaut si valeur est None.""" from app.crud.app_setting import AppSettingRepository repo = AppSettingRepository(db_session) await repo.set_value("null_key", None) await db_session.commit() value = await repo.get_value("null_key", default="fallback") assert value == "fallback" @pytest.mark.asyncio async def test_set_alias(self, db_session: AsyncSession): """Test de l'alias set().""" from app.crud.app_setting import AppSettingRepository repo = AppSettingRepository(db_session) setting = await repo.set("alias_key", "alias_value") await db_session.commit() assert setting.key == "alias_key" assert setting.value == "alias_value"