65 lines
2.7 KiB
Python
65 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Egyszerű teszt a ConfigService osztályhoz.
|
|
Futtatás: docker compose exec -T sf_api python3 /app/backend/test_config_service.py
|
|
"""
|
|
import asyncio
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app.services.config_service import ConfigService
|
|
from app.models.system.system import ParameterScope
|
|
|
|
async def test_config_service():
|
|
# Adatbázis kapcsolat létrehozása (használjuk a teszt adatbázist vagy a dev-et)
|
|
# A DATABASE_URL a .env fájlból jön, de itt hardcode-olhatunk egy teszt URL-t
|
|
database_url = os.getenv("DATABASE_URL", "postgresql+asyncpg://postgres:postgres@postgres:5432/service_finder")
|
|
engine = create_async_engine(database_url, echo=False)
|
|
AsyncSessionLocal = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
async with AsyncSessionLocal() as db:
|
|
print("=== ConfigService Teszt ===")
|
|
|
|
# 1. Teszt: nem létező kulcs, default értékkel
|
|
value = await ConfigService.get_int(db, "non_existent_key", 42)
|
|
print(f"1. get_int('non_existent_key', 42) = {value} (elvárt: 42)")
|
|
assert value == 42, f"Expected 42, got {value}"
|
|
|
|
# 2. Teszt: string lekérés
|
|
value = await ConfigService.get_str(db, "another_key", "hello")
|
|
print(f"2. get_str('another_key', 'hello') = {value} (elvárt: hello)")
|
|
assert value == "hello"
|
|
|
|
# 3. Teszt: boolean lekérés
|
|
value = await ConfigService.get_bool(db, "bool_key", True)
|
|
print(f"3. get_bool('bool_key', True) = {value} (elvárt: True)")
|
|
assert value == True
|
|
|
|
# 4. Teszt: float lekérés
|
|
value = await ConfigService.get_float(db, "float_key", 3.14)
|
|
print(f"4. get_float('float_key', 3.14) = {value} (elvárt: 3.14)")
|
|
assert value == 3.14
|
|
|
|
# 5. Teszt: JSON lekérés
|
|
value = await ConfigService.get_json(db, "json_key", {"foo": "bar"})
|
|
print(f"5. get_json('json_key', {{\"foo\": \"bar\"}}) = {value}")
|
|
assert value == {"foo": "bar"}
|
|
|
|
# 6. Teszt: általános get
|
|
value = await ConfigService.get(db, "generic_key", "default")
|
|
print(f"6. get('generic_key', 'default') = {value}")
|
|
assert value == "default"
|
|
|
|
# 7. Opcionális: beszúrhatunk egy teszt paramétert és lekérjük
|
|
# Ehhez szükség van a _insert_default metódusra, de most kihagyjuk
|
|
|
|
print("\n✅ Minden teszt sikeres!")
|
|
|
|
await db.commit()
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(test_config_service()) |