Skip to content

Commit

Permalink
PENG-2456 turn the *_force_cast* function into a inner function of th…
Browse files Browse the repository at this point in the history
…e *validate_job_metric_upload_input* function
  • Loading branch information
matheushent committed Dec 13, 2024
1 parent 925ae78 commit f9d37c9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 49 deletions.
17 changes: 4 additions & 13 deletions jobbergate-api/jobbergate_api/apps/job_submissions/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@
)


def _force_cast(object: Any, expected_type: Type[Any]) -> Any:
"""Forcefully cast a value to the expected type.
Args:
value (Any): The value to be cast.
expected_type (Type[Any]): The type to cast the value to.
Returns:
Any: The value cast to the expected type.
"""
return expected_type(object)


def validate_job_metric_upload_input(
data: Any, expected_types: tuple[Type[Any], ...]
) -> Iterable[tuple[Any]]:
Expand All @@ -42,6 +29,10 @@ def validate_job_metric_upload_input(
Returns:
Iterable[list[Any] | tuple[Any]]: The validated data.
"""

def _force_cast(object: Any, expected_type: Type[Any]) -> Any:
return expected_type(object)

if not isinstance(data, list):
raise ValueError("Decoded data must be a list.")
if not all(isinstance(x, (list, tuple)) for x in data):
Expand Down
36 changes: 0 additions & 36 deletions jobbergate-api/tests/apps/job_submissions/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
)
from jobbergate_api.apps.job_submissions.helpers import (
build_job_metric_aggregation_query,
_force_cast,
validate_job_metric_upload_input,
)

Expand Down Expand Up @@ -152,41 +151,6 @@ def test_validate_job_metric_upload_input_cast_success(self):
assert result == [(1, "test", 3.5)]


class TestForceCast:
"""
Test suite for the `_force_cast` function.
This test suite contains tests to verify the behavior of the `_force_cast` function,
which is responsible for casting values to specified types.
Classes:
TestForceCast: Contains test cases for the `_force_cast` function.
TestForceCast:
test_success: Tests that `_force_cast` successfully casts values to the expected types.
test_force_cast_failure: Tests that `_force_cast` raises appropriate errors when the cast fails.
"""

def test_success(self):
"""
Test that `_force_cast` successfully casts a value to the expected type.
"""
assert _force_cast("123", int) == 123
assert _force_cast("123.45", float) == 123.45
assert _force_cast(123, str) == "123"

def test_force_cast_failure(self):
"""
Test that `_force_cast` raises an error when the cast fails.
"""
with pytest.raises(ValueError):
_force_cast("not an int", int)
with pytest.raises(ValueError):
_force_cast("not a float", float)
with pytest.raises(TypeError):
_force_cast(123, list)


class TestBuildJobMetricAggregationQuery:
"""
Test suite for the `build_job_metric_aggregation_query` function.
Expand Down

0 comments on commit f9d37c9

Please sign in to comment.