Skip to content

Commit

Permalink
feat: additional tests and type checks.
Browse files Browse the repository at this point in the history
Refs: 251
  • Loading branch information
pc532627 committed Dec 6, 2023
1 parent bf79924 commit daf49c2
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 14 deletions.
24 changes: 13 additions & 11 deletions coreax/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ def validate_in_range(
:param x: Variable we wish to verify lies in the specified range
:param object_name: Name of ``x`` to display if limits are broken
:param strict_inequalities: If true, checks are applied using strict inequalities,
otherwise they are not
:param strict_inequalities: If :data:`True`, checks are applied using strict
inequalities, otherwise they are not
:param lower_bound: Lower limit placed on ``x``, or :data:`None`
:param upper_bound: Upper limit placed on ``x``, or :data:`None`
:raises ValueError: Raised if ``x`` does not fall between ``lower_limit`` and
``upper_limit``
:raises TypeError: Raised if x cannot be compared to a value using >, >=, < or <=
:raises TypeError: Raised if x cannot be compared to a value using ``>``, ``>=``,
``<`` or ``<=``
"""
try:
if strict_inequalities:
Expand All @@ -61,9 +62,14 @@ def validate_in_range(
if upper_bound is not None and not x <= upper_bound:
raise ValueError(f"{object_name} must be {upper_bound} or lower.")
except TypeError:
raise TypeError(
f"{object_name} must have a valid comparison <, <=, > and >= implemented."
)
if strict_inequalities:
raise TypeError(
f"{object_name} must have a valid comparison < and > implemented."
)
else:
raise TypeError(
f"{object_name} must have a valid comparison <= and >= implemented."
)


def validate_is_instance(x: T, object_name: str, expected_type: type[T]) -> None:
Expand Down Expand Up @@ -97,8 +103,4 @@ def cast_as_type(x: Any, object_name: str, type_caster: Callable) -> Any:
error_text += e.message
else:
error_text += str(e)

if isinstance(e, TypeError):
raise TypeError(error_text)
else:
raise ValueError(error_text)
raise TypeError(error_text)
94 changes: 91 additions & 3 deletions tests/unit/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from coreax.validation import cast_as_type, validate_in_range, validate_is_instance


class TestInputValidation(unittest.TestCase):
class TestInputValidationRange(unittest.TestCase):
"""
Tests relating to validation of inputs provided by the user.
Tests relating to validation of inputs provided by the user lying in a given range.
"""

def test_validate_in_range_equal_lower_strict(self) -> None:
Expand Down Expand Up @@ -68,6 +68,38 @@ def test_validate_in_range_below_lower(self) -> None:
upper_bound=100.0,
)

def test_validate_in_range_equal_upper_strict(self) -> None:
"""
Test the function validate_in_range with the input matching the lower bound.
The inequality is strict here, so this should be flagged as invalid.
"""
self.assertRaises(
ValueError,
validate_in_range,
x=100.0,
object_name="var",
strict_inequalities=True,
lower_bound=0.0,
upper_bound=100.0,
)

def test_validate_in_range_equal_upper_not_strict(self) -> None:
"""
Test the function validate_in_range with the input matching the lower bound.
The inequality is not strict here, so this should not be flagged as invalid.
"""
self.assertIsNone(
validate_in_range(
x=100.0,
object_name="var",
strict_inequalities=False,
lower_bound=0.0,
upper_bound=100.0,
)
)

def test_validate_in_range_above_upper(self) -> None:
"""
Test the function validate_in_range with the input above the upper bound.
Expand Down Expand Up @@ -134,6 +166,56 @@ def test_validate_in_range_invalid_input(self) -> None:
upper_bound=100.0,
)

def test_validate_in_range_input_no_lower_bound(self) -> None:
"""
Test the function validate_in_range with the input between the two bounds.
The input is below the upper bound, so this should not be flagged as invalid.
"""
self.assertIsNone(
validate_in_range(
x=50.0,
object_name="var",
strict_inequalities=True,
upper_bound=100.0,
)
)

def test_validate_in_range_input_no_upper_bound(self) -> None:
"""
Test the function validate_in_range with the input between the two bounds.
The input is above the lower bound, so this should not be flagged as invalid.
"""
self.assertIsNone(
validate_in_range(
x=50.0,
object_name="var",
strict_inequalities=True,
lower_bound=0.0,
)
)

def test_validate_in_range_input_no_lower_or_upper_bound(self) -> None:
"""
Test the function validate_in_range with the input between the two bounds.
The input is below the upper bound, so this should not be flagged as invalid.
"""
self.assertIsNone(
validate_in_range(
x=50.0,
object_name="var",
strict_inequalities=True,
)
)


class TestInputValidationInstance(unittest.TestCase):
"""
Tests relating to validation of inputs provided by the user are a given type.
"""

def test_validate_is_instance_float_to_int(self) -> None:
"""
Test the function validate_is_instance comparing a float to an int.
Expand Down Expand Up @@ -194,6 +276,12 @@ def test_validate_is_instance_str_to_str(self) -> None:
validate_is_instance(x="500", object_name="var", expected_type=str)
)


class TestInputValidationConversion(unittest.TestCase):
"""
Tests relating to validation of inputs provided by the user convert to a given type.
"""

def test_cast_as_type_int_to_float(self) -> None:
"""
Test the function cast_as_type converting an int to a float.
Expand Down Expand Up @@ -237,7 +325,7 @@ def test_cast_as_type_str_to_float_invalid(self) -> None:
to cause the conversion to fail.
"""
self.assertRaises(
ValueError,
TypeError,
cast_as_type,
x="120.0ABC",
object_name="var",
Expand Down

0 comments on commit daf49c2

Please sign in to comment.