Skip to content

Commit

Permalink
Add handicap table tests and refactor into separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
jatkinson1000 committed Sep 3, 2023
1 parent 2d81c8f commit df7cfa2
Show file tree
Hide file tree
Showing 3 changed files with 297 additions and 118 deletions.
18 changes: 9 additions & 9 deletions archeryutils/handicaps/handicap_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,11 @@ def print_handicap_table(
hc_dat: hc_eq.HcParams,
arrow_d: Optional[float] = None,
round_scores_up: bool = True,
clean_gaps: Optional[bool] = True,
printout: Optional[bool] = True,
clean_gaps: bool = True,
printout: bool = True,
filename: Optional[str] = None,
csvfile: Optional[str] = None,
int_prec: Optional[bool] = False,
int_prec: bool = False,
) -> None:
"""
Generate a handicap table to screen and/or file.
Expand Down Expand Up @@ -479,7 +479,7 @@ def print_handicap_table(
def check_print_table_inputs(
hcs: Union[float, NDArray[np.float_]],
round_list: list[rounds.Round],
clean_gaps: Optional[bool] = True,
clean_gaps: bool = True,
) -> NDArray[np.float_]:
"""
Sanitise and format inputs to handicap printing code.
Expand Down Expand Up @@ -521,8 +521,8 @@ def check_print_table_inputs(

def clean_repeated(
table: NDArray[Union[np.float_, np.int_]],
int_prec: Optional[bool] = False,
hc_sys: Optional[str] = "AGB",
int_prec: bool = False,
hc_sys: str = "AGB",
) -> NDArray[Union[np.float_, np.int_]]:
"""
Keep only the first instance of a score in the handicap tables.
Expand Down Expand Up @@ -598,7 +598,7 @@ def table_as_str(
round_list: List[rounds.Round],
hcs: NDArray[Union[np.float_, np.int_]],
table: NDArray[Union[np.float_, np.int_]],
int_prec: Optional[bool] = False,
int_prec: bool = False,
) -> str:
"""
Convert the handicap table to a string.
Expand Down Expand Up @@ -638,8 +638,8 @@ def table_as_str(

def format_row(
row: NDArray[Union[np.float_, np.int_]],
hc_dp: Optional[int] = 0,
int_prec: Optional[bool] = False,
hc_dp: int = 0,
int_prec: bool = False,
) -> str:
"""
Fornat appearance of handicap table row to look nice.
Expand Down
283 changes: 283 additions & 0 deletions archeryutils/handicaps/tests/test_handicap_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
"""Tests for handicap table printing"""
# Due to defining some rounds to use in testing duplicate code may trigger.
# => disable for handicap tests
# pylint: disable=duplicate-code

from typing import Union
import numpy as np
from numpy.typing import NDArray
import pytest

import archeryutils.handicaps.handicap_equations as hc_eq
import archeryutils.handicaps.handicap_functions as hc_func
from archeryutils.rounds import Round, Pass


hc_params = hc_eq.HcParams()

# Define rounds used in these functions
york = Round(
"York",
[
Pass(72, 1.22, "5_zone", 100, "yard", False),
Pass(48, 1.22, "5_zone", 80, "yard", False),
Pass(24, 1.22, "5_zone", 60, "yard", False),
],
)
hereford = Round(
"Hereford",
[
Pass(72, 1.22, "5_zone", 80, "yard", False),
Pass(48, 1.22, "5_zone", 60, "yard", False),
Pass(24, 1.22, "5_zone", 50, "yard", False),
],
)
metric122_30 = Round(
"Metric 122-30",
[
Pass(36, 1.22, "10_zone", 30, "metre", False),
Pass(36, 1.22, "10_zone", 30, "metre", False),
],
)


class TestHandicapTable:
"""
Class to test the handicap table functionalities of handicap_functions.
Methods
-------
References
----------
"""

@pytest.mark.parametrize(
"input_arr,hc_dp,int_prec,expected",
[
(
np.array([1, 20.0, 23.0]),
0,
True,
" 1 20 23",
),
(
np.array([1, 20.0, 23.0]),
2,
True,
" 1.00 20 23",
),
(
np.array([1, 20.0, 23.0]),
3,
False,
" 1.000 20.00000000 23.00000000",
),
],
)
def test_format_row(
self,
input_arr: NDArray[Union[np.int_, np.float_]],
hc_dp: int,
int_prec: bool,
expected: str,
) -> None:
"""
Check that format_row returns expected results for float and int.
"""
assert hc_func.format_row(input_arr, hc_dp, int_prec) == expected

@pytest.mark.parametrize(
"hcs,table,int_prec,expected",
[
(
# Check int_prec true
np.array([1, 2, 3]),
np.array([[1, 20.0, 23.0], [2, 20.0, 23.0], [3, 20.0, 23.0]]),
True,
" Handicap York Hereford\n"
+ " 1 20 23\n"
+ " 2 20 23\n"
+ " 3 20 23",
),
(
# Check int_prec false
np.array([1, 2, 3]),
np.array([[1, 20.0, 23.0], [2, 20.0, 23.0], [3, 20.0, 23.0]]),
False,
" Handicap York Hereford\n"
+ " 1 20.00000000 23.00000000\n"
+ " 2 20.00000000 23.00000000\n"
+ " 3 20.00000000 23.00000000",
),
(
# Check handicap float integers are converted to ints
np.array([1.0, 2.0, 3.0]),
np.array([[1.0, 20.0, 23.0], [2.0, 20.0, 23.0], [3.0, 20.0, 23.0]]),
True,
" Handicap York Hereford\n"
+ " 1 20 23\n"
+ " 2 20 23\n"
+ " 3 20 23",
),
(
# Check handicap dp are allocated OK
np.array([1.20, 2.0, 3.0]),
np.array([[1.20, 20.0, 23.0], [2.0, 20.0, 23.0], [3.0, 20.0, 23.0]]),
True,
" Handicap York Hereford\n"
+ " 1.2 20 23\n"
+ " 2.0 20 23\n"
+ " 3.0 20 23",
),
],
)
def test_table_as_str(
self,
hcs: NDArray[Union[np.int_, np.float_]],
table: NDArray[Union[np.int_, np.float_]],
int_prec: bool,
expected: str,
) -> None:
"""
Check that format_row returns expected results for float and int.
"""
print(hc_func.table_as_str([york, hereford], hcs, table, int_prec))
print(expected)
assert hc_func.table_as_str([york, hereford], hcs, table, int_prec) == expected

@pytest.mark.parametrize(
"input_str,expected",
[
("Compound", "C"),
("Recurve Triple Portsmouth", "R Tr Ports"),
("Recurve Triple Portsmouth", "R Tr Ports"),
("Short Gents Worcester", "St G Worc"),
],
)
def test_abbreviate(
self,
input_str: str,
expected: str,
) -> None:
"""
Check that abbreviate returns expected results.
"""
assert hc_func.abbreviate(input_str) == expected

@pytest.mark.parametrize(
"hcs,clean_gaps,expected",
[
(
# 'Correct' inputs
np.array([1.0, 2.0, 3.0]),
False,
np.array([1.0, 2.0, 3.0]),
),
(
# List inputs
[1.0, 2.0, 3.0],
False,
np.array([1.0, 2.0, 3.0]),
),
(
# Clean gaps True
np.array([1.0, 2.0, 3.0]),
True,
np.array([0.0, 1.0, 2.0, 3.0, 4.0]),
),
(
# Clean gaps True
np.array([1.75, 2.0, 2.5]),
True,
np.array([1.5, 1.75, 2.0, 2.5, 3.0]),
),
(
# Single float
1.0,
False,
np.array([1.0]),
),
(
# Single float clean gaps True
1.5,
True,
np.array([0.5, 1.5, 2.5]),
),
],
)
def test_check_print_table_inputs(
self,
hcs: Union[float, NDArray[np.float_]],
clean_gaps: bool,
expected: Union[float, NDArray[np.float_]],
) -> None:
"""
Check that inputs processed appropriately.
"""
np.testing.assert_allclose(
hc_func.check_print_table_inputs(hcs, [york, metric122_30], clean_gaps),
expected,
)

def test_check_print_table_inputs_invalid_rounds(
self,
) -> None:
"""
Check that empty rounds list triggers error.
"""
with pytest.raises(
ValueError,
match=("No rounds provided for handicap table."),
):
hc_func.check_print_table_inputs(1.0, [], True)

@pytest.mark.parametrize(
"input_table,int_prec,sys,expected",
[
(
np.array([[0, 11, 12, 13], [1, 10, 12, 12]]),
True,
"AGB",
np.array([[0, 11, -9999, 13], [1, 10, 12, 12]]),
),
(
np.array([[0, 13], [5, 12], [10, 12], [15, 11]]),
True,
"AGB",
np.array([[0, 13], [5, -9999], [10, 12], [15, 11]]),
),
(
np.array([[4, 13], [3, 12], [2, 12], [1, 11]]),
True,
"AA",
np.array([[4, 13], [3, 12], [2, -9999], [1, 11]]),
),
(
np.array([[0.0, 11.0, 12.0, 13.0], [1.0, 10.0, 12.0, 12.0]]),
False,
"AGB",
np.array([[0.0, 11.0, np.nan, 13.0], [1.0, 10.0, 12.0, 12.0]]),
),
(
np.array([[0.0, 11.5, 12.5, 13.5], [1.0, 11.5, 12.0, 13.5]]),
False,
"AGB",
np.array([[0.0, np.nan, 12.5, np.nan], [1.0, 11.5, 12.0, 13.5]]),
),
],
)
def test_clean_repeated(
self,
input_table: NDArray[Union[np.int_, np.float_]],
int_prec: bool,
sys: str,
expected: NDArray[Union[np.int_, np.float_]],
) -> None:
"""
Check that abbreviate returns expected results.
"""
print(hc_func.clean_repeated(input_table, int_prec, sys))
np.testing.assert_allclose(
hc_func.clean_repeated(input_table, int_prec, sys), expected
)
Loading

0 comments on commit df7cfa2

Please sign in to comment.