ソースを参照

test: strengthen multi service test harness

Jax Docker 1 ヶ月 前
コミット
dc88e6735b

+ 3 - 0
.gitlab-ci.yml

@@ -10,9 +10,12 @@ python-test:
     - pip install -e libs/core-shared
     - pip install -e libs/core-domain
     - pip install -e libs/core-db
+    - pip install -e libs/core-dsl
     - pip install -e libs/core-events
     - pip install -e services/agent-service
     - pip install -e services/knowledge-service
+    - pip install -e services/workflow-service
+    - pip install -e services/runtime-service
   script:
     - python -m compileall libs services scripts tests
     - pytest -q

+ 36 - 0
tests/conftest.py

@@ -1,5 +1,6 @@
 from __future__ import annotations
 
+from dataclasses import dataclass
 import sys
 from pathlib import Path
 from typing import Any
@@ -8,6 +9,32 @@ from typing import Any
 REPO_ROOT = Path(__file__).resolve().parents[1]
 
 
+@dataclass(frozen=True)
+class ServiceImportConfig:
+    service_name: str
+    libs: tuple[str, ...]
+
+
+SERVICE_IMPORT_CONFIGS: dict[str, ServiceImportConfig] = {
+    "agent-service": ServiceImportConfig(
+        service_name="agent-service",
+        libs=("core-domain", "core-shared", "core-db", "core-events"),
+    ),
+    "knowledge-service": ServiceImportConfig(
+        service_name="knowledge-service",
+        libs=("core-domain", "core-shared", "core-db"),
+    ),
+    "runtime-service": ServiceImportConfig(
+        service_name="runtime-service",
+        libs=("core-domain", "core-shared", "core-db", "core-events", "core-dsl"),
+    ),
+    "workflow-service": ServiceImportConfig(
+        service_name="workflow-service",
+        libs=("core-domain", "core-shared", "core-db", "core-dsl"),
+    ),
+}
+
+
 def prepare_service_import(
     service_name: str,
     *,
@@ -23,6 +50,15 @@ def prepare_service_import(
     _prepend_sys_path(REPO_ROOT / "services" / service_name)
 
 
+def prepare_known_service_import(service_name: str) -> None:
+    config = SERVICE_IMPORT_CONFIGS[service_name]
+    prepare_service_import(config.service_name, libs=config.libs)
+
+
+def build_sqlite_database_url(tmp_path: Path, filename: str) -> str:
+    return f"sqlite:///{tmp_path / filename}"
+
+
 def _prepend_sys_path(path: Path) -> None:
     path_text = str(path)
     if path_text in sys.path:

+ 7 - 6
tests/test_runtime_debugger.py

@@ -6,14 +6,15 @@ from pathlib import Path
 
 from sqlalchemy.orm import Session
 
-from tests.conftest import build_fastapi_test_client, prepare_service_import
+from tests.conftest import (
+    build_fastapi_test_client,
+    build_sqlite_database_url,
+    prepare_known_service_import,
+)
 
 
 def test_runtime_debugger_pause_step_and_breakpoint_continue(tmp_path: Path) -> None:
-    prepare_service_import(
-        "runtime-service",
-        libs=("core-domain", "core-shared", "core-db", "core-events", "core-dsl"),
-    )
+    prepare_known_service_import("runtime-service")
 
     from app.api.routes import get_runtime_application_service
     from app.application.services import RuntimeApplicationService
@@ -57,7 +58,7 @@ def test_runtime_debugger_pause_step_and_breakpoint_continue(tmp_path: Path) ->
                 },
             )
 
-    settings = RuntimeServiceSettings(database_url=f"sqlite:///{tmp_path / 'runtime.db'}")
+    settings = RuntimeServiceSettings(database_url=build_sqlite_database_url(tmp_path, "runtime.db"))
     session_factory = build_session_factory(settings)
     engine = session_factory.kw["bind"]
     Base.metadata.create_all(bind=engine)

+ 46 - 0
tests/test_test_harness.py

@@ -0,0 +1,46 @@
+from __future__ import annotations
+
+import sys
+import types
+from pathlib import Path
+
+from tests.conftest import (
+    REPO_ROOT,
+    SERVICE_IMPORT_CONFIGS,
+    build_sqlite_database_url,
+    prepare_known_service_import,
+)
+
+
+def test_prepare_known_service_import_resets_app_modules_and_orders_paths() -> None:
+    sys.modules["app"] = types.ModuleType("app")
+    sys.modules["app.fake"] = types.ModuleType("app.fake")
+
+    prepare_known_service_import("workflow-service")
+
+    assert "app" not in sys.modules
+    assert "app.fake" not in sys.modules
+    assert sys.path[0] == str(REPO_ROOT / "services" / "workflow-service")
+    assert str(REPO_ROOT / "libs" / "core-dsl" / "src") in sys.path
+
+
+def test_service_import_configs_cover_services_with_database_tests() -> None:
+    assert "runtime-service" in SERVICE_IMPORT_CONFIGS
+    assert "workflow-service" in SERVICE_IMPORT_CONFIGS
+    assert "core-db" in SERVICE_IMPORT_CONFIGS["runtime-service"].libs
+    assert "core-dsl" in SERVICE_IMPORT_CONFIGS["workflow-service"].libs
+
+
+def test_build_sqlite_database_url_uses_tmp_path(tmp_path: Path) -> None:
+    database_url = build_sqlite_database_url(tmp_path, "service.db")
+
+    assert database_url.startswith("sqlite:///")
+    assert str(tmp_path / "service.db") in database_url
+
+
+def test_gitlab_ci_installs_services_used_by_tests() -> None:
+    ci_text = (REPO_ROOT / ".gitlab-ci.yml").read_text(encoding="utf-8")
+
+    assert "pip install -e libs/core-dsl" in ci_text
+    assert "pip install -e services/workflow-service" in ci_text
+    assert "pip install -e services/runtime-service" in ci_text

+ 2 - 5
tests/test_workflow_designer_debugger.py

@@ -1,12 +1,9 @@
 from __future__ import annotations
 
-from tests.conftest import build_fastapi_test_client, prepare_service_import
+from tests.conftest import build_fastapi_test_client, prepare_known_service_import
 
 
-prepare_service_import(
-    "workflow-service",
-    libs=("core-domain", "core-shared", "core-db", "core-dsl"),
-)
+prepare_known_service_import("workflow-service")
 
 from app.bootstrap.app import create_app
 from app.api.routes import get_workflow_application_service