#!/usr/bin/env python3 """ Gyors teszt a hierarchikus paraméterekhez. Futtatás: docker exec sf_api python /app/test_hierarchical.py """ import asyncio import os import sys sys.path.insert(0, '/app') from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession from sqlalchemy.orm import sessionmaker from sqlalchemy import text from app.services.system_service import system_service DATABASE_URL = os.getenv("DATABASE_URL", "postgresql+asyncpg://postgres:postgres@shared-postgres:5432/service_finder") async def test(): engine = create_async_engine(DATABASE_URL, echo=False) async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) async with async_session() as db: # Töröljük a teszt paramétereket await db.execute(text("DELETE FROM system.system_parameters WHERE key = 'test.hierarchical'")) await db.commit() # Beszúrjuk a teszt adatokat await db.execute(text(""" INSERT INTO system.system_parameters (key, value, scope_level, scope_id, category, is_active) VALUES ('test.hierarchical', '{"msg": "global"}', 'global', NULL, 'test', true), ('test.hierarchical', '{"msg": "country HU"}', 'country', 'HU', 'test', true), ('test.hierarchical', '{"msg": "region budapest"}', 'region', 'budapest', 'test', true), ('test.hierarchical', '{"msg": "user 123"}', 'user', '123', 'test', true) """)) await db.commit() # Tesztelés # 1. Global val = await system_service.get_scoped_parameter(db, 'test.hierarchical', default=None) print(f"Global: {val}") assert val['msg'] == 'global' # 2. Country HU val = await system_service.get_scoped_parameter(db, 'test.hierarchical', country_code='HU', default=None) print(f"Country HU: {val}") assert val['msg'] == 'country HU' # 3. Region budapest (country is HU) val = await system_service.get_scoped_parameter(db, 'test.hierarchical', region_id='budapest', country_code='HU', default=None) print(f"Region budapest: {val}") assert val['msg'] == 'region budapest' # 4. User 123 (with region and country) val = await system_service.get_scoped_parameter(db, 'test.hierarchical', user_id='123', region_id='budapest', country_code='HU', default=None) print(f"User 123: {val}") assert val['msg'] == 'user 123' # 5. Non-existent user, fallback to region val = await system_service.get_scoped_parameter(db, 'test.hierarchical', user_id='999', region_id='budapest', country_code='HU', default=None) print(f"Non-existent user -> region: {val}") assert val['msg'] == 'region budapest' # 6. Non-existent region, fallback to country val = await system_service.get_scoped_parameter(db, 'test.hierarchical', region_id='none', country_code='HU', default=None) print(f"Non-existent region -> country: {val}") assert val['msg'] == 'country HU' # 7. Non-existent country, fallback to global val = await system_service.get_scoped_parameter(db, 'test.hierarchical', country_code='US', default=None) print(f"Non-existent country -> global: {val}") assert val['msg'] == 'global' # Törlés await db.execute(text("DELETE FROM system.system_parameters WHERE key = 'test.hierarchical'")) await db.commit() print("✅ Minden teszt sikeres!") if __name__ == "__main__": asyncio.run(test())