Skip to content

Commit

Permalink
test(cli): add tests for CLI api group (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-awala authored May 12, 2024
1 parent 10821bb commit 3961938
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tests/cli/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from __future__ import annotations

from unittest import mock

import pytest
from click.testing import CliRunner

TEST_HOST = "localhost"
TEST_PORT = 8000
TEST_WORKERS = 8
TEST_LOG_LEVEL = "critical"
TEST_LIMIT_CONCURRENCY = 10000


@pytest.mark.parametrize(
"host, port, workers, log_level, limit_concurrency",
[
pytest.param(None, None, None, None, None, id="default conf"),
pytest.param(TEST_HOST, TEST_PORT, TEST_WORKERS, None, None, id="common conf"),
pytest.param(
TEST_HOST, TEST_PORT, TEST_WORKERS, TEST_LOG_LEVEL, TEST_LIMIT_CONCURRENCY, id="override all conf"
),
],
)
@mock.patch("uvicorn.run")
def test_list(mock_uvicorn_run, host, port, workers, log_level, limit_concurrency):
from spark_on_k8s.api.configuration import APIConfiguration
from spark_on_k8s.cli.api import start

args = []
if host is not None:
args.extend(["--host", host])
expected_host = host
else:
expected_host = APIConfiguration.SPARK_ON_K8S_API_HOST
if port is not None:
args.extend(["--port", port])
expected_port = port
else:
expected_port = APIConfiguration.SPARK_ON_K8S_API_PORT
if workers is not None:
args.extend(["--workers", workers])
expected_workers = workers
else:
expected_workers = APIConfiguration.SPARK_ON_K8S_API_WORKERS
if log_level is not None:
args.extend(["--log-level", log_level])
expected_log_level = log_level
else:
expected_log_level = APIConfiguration.SPARK_ON_K8S_API_LOG_LEVEL
if limit_concurrency is not None:
args.extend(["--limit-concurrency", limit_concurrency])
expected_limit_concurrency = limit_concurrency
else:
expected_limit_concurrency = APIConfiguration.SPARK_ON_K8S_API_LIMIT_CONCURRENCY
result = CliRunner().invoke(start, args)
assert result.exit_code == 0
mock_uvicorn_run.assert_called_with(
"spark_on_k8s.api.main:app",
host=expected_host,
port=expected_port,
workers=expected_workers,
log_level=expected_log_level,
limit_concurrency=expected_limit_concurrency,
)

0 comments on commit 3961938

Please sign in to comment.