aboutsummaryrefslogtreecommitdiff
path: root/tests/qc/test_cells.py
blob: 46aeb64dcd04d46e25ca82ae52fbf1a392c9487f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""Test that values in cells within a line fulfill the required criteria"""

import pytest
from random import randint
from hypothesis import given
from hypothesis import strategies as st

from quality_control.errors import InvalidValue
from quality_control.errors import InvalidCellValue
from quality_control.average import (
    valid_value as avg_valid_value,
    invalid_value as avg_invalid_value)
from quality_control.standard_error import (
    valid_value as se_valid_value,
    invalid_value as se_invalid_value)

@given(num_str=st.from_regex(
    r"^(?!([0-9]+\.([0-9]{3}|[0-9]{6,}))).*", fullmatch=True))
def test_cell_value_errors_with_invalid_inputs(num_str):
    """Check that an error is raised for a cell with an invalid value."""
    with pytest.raises(InvalidCellValue):
        avg_valid_value(num_str)
    with pytest.raises(InvalidCellValue):
        se_valid_value(num_str)

@given(num_str=st.from_regex(
    r"^[0-9]+\.([0-9]{1,2}|[0-9]{4,}$)", fullmatch=True))
def test_cell_average_value_errors_if_not_three_decimal_places(num_str):
    """Check that an error is raised if the average value does not have 3 decimal places"""
    with pytest.raises(InvalidCellValue):
        avg_valid_value(num_str)

@given(num_str=st.from_regex(r"^[0-9]+\.[0-9]{3}$", fullmatch=True))
def test_cell_average_value_pass_if_three_decimal_places(num_str):
    """Check that there is no error if the average value has 3 decimal places."""
    processed = avg_valid_value(num_str)
    assert (
        isinstance(processed, float) and
        processed == float(num_str))

@given(num_str=st.from_regex(r"^[0-9]+\.([0-9]{0,5}$)", fullmatch=True))
def test_cell_standard_error_value_errors_if_less_than_six_decimal_places(num_str):
    """
    Check that an error is raised if the standard error value does not have 6
    decimal places
    """
    with pytest.raises(InvalidCellValue):
        se_valid_value(num_str)

@given(num_str=st.from_regex(r"^[0-9]+\.[0-9]{6,}$", fullmatch=True))
def test_cell_standard_error_value_pass_if_six_or_more_decimal_places(num_str):
    """
    Check that there is no error if the standard error value has 3 decimal
    places.
    """
    processed = se_valid_value(num_str)
    assert (
        isinstance(processed, float) and
        processed == float(num_str))

## ================================================================================

@given(num_str=st.from_regex(
    r"^(?!([0-9]+\.([0-9]{3}|[0-9]{6,}))).*", fullmatch=True))
def test_cell_value_errors_with_invalid_inputs2(num_str):
    """
    GIVEN: `num_str` is an arbitrary string that is an invalid input,
    WHEN: `num_str` is provided as an argument to `*_invalid_value` functions,
    THEN: The `*_invalid_value` functions return a
      `quality_control.errors.InvalidValue` object which holds the error
      information.
    """
    assert avg_invalid_value(0, 0, num_str) == InvalidValue(
        0, 0, num_str, (
            f"Invalid value '{num_str}'. Expected string representing a number "
            "with exactly three decimal places."))
    assert se_invalid_value(0, 0, num_str) == InvalidValue(
        0, 0, num_str, (
            f"Invalid value '{num_str}'. Expected string representing a number "
            "with at least six decimal places."))

@given(num_str=st.from_regex(
    r"^[0-9]+\.([0-9]{1,2}|[0-9]{4,}$)", fullmatch=True))
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
      than three decimal places, e.g. 2.92, 39.483732
    WHEN: `num_str` is provided as an argument to `avg_invalid_value` function,
    THEN: `avg_invalid_value` returns a `quality_control.errors.InvalidValue`
      object with the information about the placement of the invalid value.
    """
    line, col = randint(0, 100), randint(0, 20)
    assert avg_invalid_value(line, col, num_str) == InvalidValue(
        line, col, num_str, (
            f"Invalid value '{num_str}'. Expected string representing a number "
            "with exactly three decimal places."))

@given(num_str=st.from_regex(r"^[0-9]+\.[0-9]{3}$", fullmatch=True))
def test_cell_average_value_pass_if_three_decimal_places(num_str):
    """
    GIVEN: `num_str` is a string representing a number with exactly three
      decimal places, e.g. 2.924, 39.483
    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

@given(num_str=st.from_regex(r"^[0-9]+\.([0-9]{0,5}$)", 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 number with less than six
      decimal places, e.g. 2.9, 39.4837
    WHEN: `num_str` is provided as an argument to `se_invalid_value` function,
    THEN: `se_invalid_value` returns a `quality_control.errors.InvalidValue`
      object with the information about the placement of the invalid value.
    """
    line, col = randint(0, 100), randint(0, 20)
    assert se_invalid_value(line, col, num_str) == InvalidValue(
        line, col, num_str, (
            f"Invalid value '{num_str}'. Expected string representing a number "
            "with at least six decimal places."))


@given(num_str=st.from_regex(r"^[0-9]+\.[0-9]{6,}$", fullmatch=True))
def test_cell_standard_error_value_pass_if_six_or_more_decimal_places(num_str):
    """
    GIVEN: `num_str` is a string representing a number with six or more
      decimal places, e.g. 2.938434, 39.4837343
    WHEN: `num_str` is provided as an argument to `se_invalid_value` function,
    THEN: `se_invalid_value` returns `None`
    """
    line, col = randint(0, 100), randint(0, 20)
    assert se_invalid_value(line, col, num_str) is None