From d511e44b1a2b0e70236831926d86a10d589e9235 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 16 Nov 2022 04:16:01 +0300 Subject: qc: Allow whole numbers or numbers where the decimals are all zeroes --- quality_control/average.py | 4 ++-- quality_control/standard_error.py | 4 ++-- tests/qc/test_cells.py | 6 +++--- tests/qc/test_cells_average.py | 17 +++++++++++++++++ tests/qc/test_cells_standard_error.py | 20 ++++++++++++++++++++ 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 tests/qc/test_cells_average.py create mode 100644 tests/qc/test_cells_standard_error.py diff --git a/quality_control/average.py b/quality_control/average.py index 2b098db..ad732d0 100644 --- a/quality_control/average.py +++ b/quality_control/average.py @@ -9,8 +9,8 @@ def invalid_value(line_number: int, column_number: int, val: str) -> Union[ """Return an `InvalidValue` object if `val` is not a valid "averages" value.""" return cell_error( - r"^[0-9]+\.[0-9]{3}$", val, line=line_number, column=column_number, - value=val, message=( + r"^([0-9]+\.[0-9]{3}|[0-9]+\.?0*)$", val, line=line_number, + column=column_number, value=val, message=( f"Invalid value '{val}'. " "Expected string representing a number with exactly three " "decimal places.")) diff --git a/quality_control/standard_error.py b/quality_control/standard_error.py index 7e059ad..90beb8a 100644 --- a/quality_control/standard_error.py +++ b/quality_control/standard_error.py @@ -13,8 +13,8 @@ def invalid_value( `None`. """ return cell_error( - r"^[0-9]+\.[0-9]{6,}$", val, line=line_number, column=column_number, - value=val, message=( + r"^([0-9]+\.[0-9]{6,}|[0-9]+\.?0*)$", val, line=line_number, + column=column_number, value=val, message=( f"Invalid value '{val}'. " "Expected string representing a number with at least six " "decimal places.")) diff --git a/tests/qc/test_cells.py b/tests/qc/test_cells.py index 5ff5813..1c6c3f6 100644 --- a/tests/qc/test_cells.py +++ b/tests/qc/test_cells.py @@ -10,7 +10,7 @@ from quality_control.average import invalid_value as avg_invalid_value from quality_control.standard_error import invalid_value as se_invalid_value @given(num_str=st.from_regex( - r"^(?!(([0-9]+\.([0-9]{3}|[0-9]{6,}))|0+\.?0*)).*", fullmatch=True)) + r"^(?!(([0-9]+\.([0-9]{3}|[0-9]{6,}))|[0-9]+\.?0*)).*", fullmatch=True)) def test_cell_value_errors_with_invalid_inputs2(num_str): """ GIVEN: `num_str` is an arbitrary string that is an invalid input, @@ -30,7 +30,7 @@ def test_cell_value_errors_with_invalid_inputs2(num_str): @given(num_str=st.from_regex( r"^[0-9]+\.([0-9]{1,2}|[0-9]{4,}$)", fullmatch=True).filter( - lambda param: not re.match(r"0\.0+", param))) + lambda param: not re.match(r"^[0-9]+\.0+$", param))) def test_cell_average_value_errors_if_not_three_decimal_places2(num_str): """ GIVEN: `num_str` is a string representing a number with less than or more @@ -57,7 +57,7 @@ def test_cell_average_value_pass_if_three_decimal_places(num_str): assert avg_invalid_value(line, col, num_str) is None @given(num_str=st.from_regex(r"^[0-9]+\.([0-9]{0,5}$)", fullmatch=True).filter( - lambda param: not re.match(r"0\.0+", param))) + lambda param: not re.match(r"^[0-9]+\.?0*$", param))) def test_cell_standard_error_value_errors_if_less_than_six_decimal_places2(num_str): """ GIVEN: `num_str` is a string representing a number with less than six diff --git a/tests/qc/test_cells_average.py b/tests/qc/test_cells_average.py new file mode 100644 index 0000000..c2b382a --- /dev/null +++ b/tests/qc/test_cells_average.py @@ -0,0 +1,17 @@ +import re +from random import randint +from hypothesis import given +from hypothesis import strategies as st + +from quality_control.average import invalid_value as avg_invalid_value + +@given(num_str=st.from_regex(r"^[0-9]+$", fullmatch=True)) +def test_cell_average_value_pass_if_no_decimal_places(num_str): + """ + GIVEN: `num_str` is a string representing a number with no decimal places, + e.g. 2, 39, 8420 + WHEN: `num_str` is provided as an argument to `avg_invalid_value` function, + THEN: `avg_invalid_value` returns `None` + """ + line, col = randint(0, 100), randint(0, 20) + assert avg_invalid_value(line, col, num_str) is None diff --git a/tests/qc/test_cells_standard_error.py b/tests/qc/test_cells_standard_error.py new file mode 100644 index 0000000..aa25aa8 --- /dev/null +++ b/tests/qc/test_cells_standard_error.py @@ -0,0 +1,20 @@ +import re +from random import randint + + +from hypothesis import given +from hypothesis import strategies as st + +from quality_control.standard_error import invalid_value + +@given(num_str=st.from_regex(r"^[0-9]+\.?0*", fullmatch=True)) +def test_cell_standard_error_value_errors_if_less_than_six_decimal_places2(num_str): + """ + GIVEN: `num_str` is a string representing a whole number (e.g 5, 33, 5453 + etc) or a number with all zeroes for the decimals (e.g 0.0, 19.00000, + 45842.00 etc) + WHEN: `num_str` is provided as an argument to `se_invalid_value` function, + THEN: `se_invalid_value` returns a `None`. + """ + line, col = randint(0, 100), randint(0, 20) + assert invalid_value(line, col, num_str) is None -- cgit v1.2.3