65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
import os
|
|
import sys
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
sys.modules["psycopg2"] = MagicMock()
|
|
sys.modules["psycopg2.pool"] = MagicMock()
|
|
sys.modules["psycopg2.extras"] = MagicMock()
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
|
|
import pytest
|
|
|
|
import main
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_setting_returns_stored_value():
|
|
with patch.object(main, "db_query", new=AsyncMock(return_value={"value": "Custom GmbH"})) as mock_query:
|
|
value = await main.get_setting("company_name")
|
|
|
|
assert value == "Custom GmbH"
|
|
args, kwargs = mock_query.call_args
|
|
assert "SELECT value FROM settings" in args[0]
|
|
assert args[1] == ("company_name",)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_setting_falls_back_to_default_when_missing():
|
|
with patch.object(main, "db_query", new=AsyncMock(return_value=None)):
|
|
value = await main.get_setting("assistant_name")
|
|
|
|
assert value == "JARVIS"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_all_settings_merges_defaults_with_stored_overrides():
|
|
rows = [{"key": "assistant_name", "value": "FRIDAY"}]
|
|
with patch.object(main, "db_query", new=AsyncMock(return_value=rows)):
|
|
settings = await main.get_all_settings()
|
|
|
|
assert settings == {
|
|
"assistant_name": "FRIDAY",
|
|
"company_name": "MBO-Tech-IT",
|
|
"contact_email": "kontakt@mbo-tech-it.de",
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_all_settings_returns_defaults_when_table_empty():
|
|
with patch.object(main, "db_query", new=AsyncMock(return_value=[])):
|
|
settings = await main.get_all_settings()
|
|
|
|
assert settings == main.SETTINGS_DEFAULTS
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_set_setting_upserts_value():
|
|
with patch.object(main, "db_query", new=AsyncMock(return_value=None)) as mock_query:
|
|
await main.set_setting("company_name", "Neue Firma GmbH")
|
|
|
|
args, kwargs = mock_query.call_args
|
|
assert "INSERT INTO settings" in args[0]
|
|
assert "ON CONFLICT (key) DO UPDATE" in args[0]
|
|
assert args[1] == ("company_name", "Neue Firma GmbH")
|