Skip to content

Commit

Permalink
test(cli): add tests for CLI options utils (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-awala authored May 12, 2024
1 parent 3961938 commit 376b083
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
9 changes: 7 additions & 2 deletions spark_on_k8s/cli/options.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import click

from spark_on_k8s.utils.configuration import Configuration

if TYPE_CHECKING:
from click.core import Context, Parameter

namespace_option = click.Option(
("-n", "--namespace"),
type=str,
Expand All @@ -20,7 +25,7 @@


# Submit options
def validate_dictionary_option(ctx, param, value):
def validate_dictionary_option(ctx: Context, param: Parameter, value: list[str]) -> dict[str, str]:
dict_values = {}
for conf in value:
try:
Expand All @@ -31,7 +36,7 @@ def validate_dictionary_option(ctx, param, value):
return dict_values


def validate_list_option(ctx, param, value):
def validate_list_option(ctx: Context, param: Parameter, value: str) -> list[str]:
list_values = value.split(",") if value else []
return list_values

Expand Down
43 changes: 43 additions & 0 deletions tests/cli/test_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import annotations

import pytest
from click.core import Argument, BadParameter, Command, Context
from spark_on_k8s.cli.options import validate_dictionary_option, validate_list_option


@pytest.mark.parametrize(
"passed_value, expected_result",
[
pytest.param([], {}, id="empty value"),
pytest.param(["p1=v1"], {"p1": "v1"}, id="a single value"),
pytest.param(["p1=v1", "p2=v2"], {"p1": "v1", "p2": "v2"}, id="multiple values"),
pytest.param(["p1:v1"], BadParameter, id="a bad value"),
pytest.param(["p1=v1", "p2"], BadParameter, id="multiple value with a bad one"),
],
)
def test_validate_dictionary_option(
passed_value: list[str], expected_result: dict[str, str] | type[Exception]
):
ctx = Context(Command("fake-command"))
param = Argument(["--test-param"])
if isinstance(expected_result, type) and issubclass(expected_result, Exception):
with pytest.raises(expected_result):
validate_dictionary_option(ctx, param, passed_value)
else:
actual_result = validate_dictionary_option(ctx, param, passed_value)
assert expected_result == actual_result


@pytest.mark.parametrize(
"passed_value, expected_result",
[
pytest.param("", [], id="empty value"),
pytest.param("p1", ["p1"], id="a single value"),
pytest.param("p1,p2,p3", ["p1", "p2", "p3"], id="multiple values"),
],
)
def test_validate_list_option(passed_value: str, expected_result: list[str]):
ctx = Context(Command("fake-command"))
param = Argument(["--test-param"])
actual_result = validate_list_option(ctx, param, passed_value)
assert expected_result == actual_result

0 comments on commit 376b083

Please sign in to comment.