Skip to content

Add assert_equal to asserts #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/scwidgets/check/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ._asserts import (
assert_equal,
assert_numpy_allclose,
assert_numpy_floating_sub_dtype,
assert_numpy_sub_dtype,
Expand All @@ -14,6 +15,7 @@
"AssertResult",
"CheckRegistry",
"CheckableWidget",
"assert_equal",
"assert_shape",
"assert_numpy_allclose",
"assert_type",
Expand Down
45 changes: 45 additions & 0 deletions src/scwidgets/check/_asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,51 @@
AssertFunctionOutputT = Union[str, AssertResult]


def assert_equal(
output_parameters: Check.FunOutParamsT,
output_references: Check.FunOutParamsT,
parameters_to_check: Union[Iterable[int], str] = "all",
) -> AssertResult:
assert len(output_parameters) == len(
output_references
), "output_parameters and output_references have to have the same length"

parameter_indices: Iterable[int]
if isinstance(parameters_to_check, str):
if parameters_to_check == "all":
parameter_indices = range(len(output_parameters))
else:
raise ValueError(
f'Got parameters_to_check="{parameters_to_check}" but only "all" '
"is accepted as string"
)
elif isinstance(parameters_to_check, abc.Iterable):
parameter_indices = parameters_to_check # type: ignore[assignment]
else:
raise TypeError(
"Only str and Iterable are accepted for parameters_to_check, "
f"but got type {type(parameters_to_check)}."
)

failed_parameter_indices = []
failed_parameter_values = []
messages = []
for i in parameter_indices:
if not output_parameters[i] == output_references[i]:
message = (
f"Expected {output_references[i]} " f"but got {output_parameters[i]}."
)
failed_parameter_indices.append(i)
failed_parameter_values.append(output_parameters[i])
messages.append(message)
return AssertResult(
assert_name="assert_equal",
parameter_indices=failed_parameter_indices,
parameter_values=failed_parameter_values,
messages=messages,
)


def assert_shape(
output_parameters: Check.FunOutParamsT,
output_references: Check.FunOutParamsT,
Expand Down
13 changes: 13 additions & 0 deletions tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@
CheckableWidget,
CheckRegistry,
CheckResult,
assert_equal,
assert_numpy_allclose,
assert_numpy_floating_sub_dtype,
assert_shape,
assert_type,
)


def test_assert_equal():
output_parameters = (42,)
output_references = (42,)
result = assert_equal(output_parameters, output_references)
assert result.successful

output_parameters = (42,)
output_references = (41,)
result = assert_equal(output_parameters, output_references)
assert not result.successful


def test_assert_shape():
output_parameters = (np.array([1, 2, 3]),)
output_references = (np.array([1, 2, 3]),)
Expand Down
Loading