aboutsummaryrefslogtreecommitdiff
path: root/tests/qc/test_cells.py
blob: d4ef91196646a51925e87972f49ac491b7ea1b15 (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
"""Test that values in cells within a line fulfill the required criteria"""

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

from quality_control.errors import InvalidCellValue
from quality_control.average import valid_value as avg_valid_value
from quality_control.standard_error import valid_value as se_valid_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))