Skip to content

Commit

Permalink
Correct typer validator factory
Browse files Browse the repository at this point in the history
  • Loading branch information
JimMadge committed Nov 29, 2023
1 parent c040e47 commit ba05dd7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions data_safe_haven/functions/typer_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def typer_validator_factory(validator: Callable[[Any], Any]) -> Callable[[Any],
def typer_validator(x: Any) -> Any:
try:
validator(x)
return x
except ValueError as exc:
raise BadParameter(str(exc)) from exc

Expand Down
28 changes: 28 additions & 0 deletions tests_/functions/test_typer_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest
from typer import BadParameter

from data_safe_haven.functions.typer_validators import typer_validate_aad_guid


class TestTyperValidateAadGuid:
@pytest.mark.parametrize(
"guid",
[
"d5c5c439-1115-4cb6-ab50-b8e547b6c8dd",
"10de18e7-b238-6f1e-a4ad-772708929203",
]
)
def test_typer_validate_aad_guid(self, guid):
assert typer_validate_aad_guid(guid) == guid

@pytest.mark.parametrize(
"guid",
[
"10de18e7_b238_6f1e_a4ad_772708929203",
"not a guid",
]
)
def test_typer_validate_aad_guid_fail(self, guid):
with pytest.raises(BadParameter) as exc:
typer_validate_aad_guid(guid)
assert "Expected GUID" in exc
27 changes: 27 additions & 0 deletions tests_/functions/test_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest

from data_safe_haven.functions.validators import validate_aad_guid


class TestValidateAadGuid:
@pytest.mark.parametrize(
"guid",
[
"d5c5c439-1115-4cb6-ab50-b8e547b6c8dd",
"10de18e7-b238-6f1e-a4ad-772708929203",
]
)
def test_validate_aad_guid(self, guid):
assert validate_aad_guid(guid) == guid

@pytest.mark.parametrize(
"guid",
[
"10de18e7_b238_6f1e_a4ad_772708929203",
"not a guid",
]
)
def test_validate_aad_guid_fail(self, guid):
with pytest.raises(ValueError) as exc:
validate_aad_guid(guid)
assert "Expected GUID" in exc

0 comments on commit ba05dd7

Please sign in to comment.