mirror of
https://github.com/ipnet-mesh/meshcore-hub.git
synced 2026-06-22 02:55:58 +02:00
test(api): cover --workers CLI branch and factory metrics path
Raise diff coverage above target by exercising the previously untested lines: - test_cli.py: invoke the `api` command with uvicorn.run mocked to assert the default path passes the app object (no workers/factory) and that --workers / API_WORKERS launches the env factory by import string with the requested worker count and factory=True. - test_app_factory.py: add METRICS_ENABLED true/false cases that toggle the /metrics route, covering the env-bool parsing branch. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -65,3 +65,19 @@ def test_factory_redis_enabled_accepts_truthy_values(clean_env):
|
||||
clean_env.setenv("REDIS_ENABLED", "1")
|
||||
app = create_app_from_env()
|
||||
assert app.state.redis_enabled is True
|
||||
|
||||
|
||||
def test_factory_metrics_enabled_via_env(clean_env):
|
||||
"""METRICS_ENABLED=true mounts the /metrics endpoint."""
|
||||
clean_env.setenv("METRICS_ENABLED", "true")
|
||||
app = create_app_from_env()
|
||||
paths = {getattr(route, "path", None) for route in app.routes}
|
||||
assert "/metrics" in paths
|
||||
|
||||
|
||||
def test_factory_metrics_disabled_via_env(clean_env):
|
||||
"""METRICS_ENABLED=false omits the /metrics endpoint."""
|
||||
clean_env.setenv("METRICS_ENABLED", "false")
|
||||
app = create_app_from_env()
|
||||
paths = {getattr(route, "path", None) for route in app.routes}
|
||||
assert "/metrics" not in paths
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
"""Tests for the API CLI command (server launch wiring)."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from click.testing import CliRunner
|
||||
|
||||
from meshcore_hub.api.cli import api
|
||||
|
||||
|
||||
def test_api_default_runs_single_process():
|
||||
"""With the default worker count, the app object is passed directly and no
|
||||
worker/factory options are used."""
|
||||
runner = CliRunner()
|
||||
with patch("uvicorn.run") as mock_run:
|
||||
result = runner.invoke(api, [], catch_exceptions=False)
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert mock_run.call_count == 1
|
||||
args, kwargs = mock_run.call_args
|
||||
# Single-process path passes the built app object, not an import string.
|
||||
assert not isinstance(args[0], str)
|
||||
assert "workers" not in kwargs
|
||||
|
||||
|
||||
def test_api_workers_uses_env_factory_import_string():
|
||||
"""workers > 1 launches uvicorn against the env-driven factory by import
|
||||
string with the requested worker count."""
|
||||
runner = CliRunner()
|
||||
with patch("uvicorn.run") as mock_run:
|
||||
result = runner.invoke(api, ["--workers", "3"], catch_exceptions=False)
|
||||
|
||||
assert result.exit_code == 0
|
||||
args, kwargs = mock_run.call_args
|
||||
assert args[0] == "meshcore_hub.api.app:create_app_from_env"
|
||||
assert kwargs["workers"] == 3
|
||||
assert kwargs["factory"] is True
|
||||
|
||||
|
||||
def test_api_workers_from_env_var():
|
||||
"""API_WORKERS env var drives the worker count (the Docker path)."""
|
||||
runner = CliRunner()
|
||||
with patch("uvicorn.run") as mock_run:
|
||||
result = runner.invoke(
|
||||
api, [], env={"API_WORKERS": "2"}, catch_exceptions=False
|
||||
)
|
||||
|
||||
assert result.exit_code == 0
|
||||
_, kwargs = mock_run.call_args
|
||||
assert kwargs["workers"] == 2
|
||||
Reference in New Issue
Block a user