lalib/tests/fields/utils.py

76 lines
1.6 KiB
Python
Raw Normal View History

"""Utilities to test the `lalib.fields` sub-package."""
import decimal
import fractions
import os
import pytest
from lalib import config
from lalib import elements
from lalib import fields
ALL_FIELDS = (fields.Q, fields.R, fields.C, fields.GF2)
NON_10_FIELDS = (fields.Q, fields.R, fields.C)
ONES = (
1,
1.0,
fractions.Fraction(1, 1),
decimal.Decimal("1.0"),
elements.one,
True,
)
ZEROS = (
0,
0.0,
fractions.Fraction(0, 1),
decimal.Decimal("+0.0"),
decimal.Decimal("-0.0"),
elements.zero,
False,
)
ONES_N_ZEROS = ONES + ZEROS
NON_ONES_N_ZEROS = (
+42,
+42.0,
fractions.Fraction(+42, 1),
decimal.Decimal("+42.0"),
-42,
-42.0,
fractions.Fraction(-42, 1),
decimal.Decimal("-42.0"),
)
NUMBERS = ONES_N_ZEROS + NON_ONES_N_ZEROS
DEFAULT_THRESHOLD = config.THRESHOLD
WITHIN_THRESHOLD = config.THRESHOLD / 10
NOT_WITHIN_THRESHOLD = config.THRESHOLD * 10
N_RANDOM_DRAWS = os.environ.get("N_RANDOM_DRAWS") or 1
def is_field_element(field, value):
"""Utility method to avoid redundant logic in tests."""
element = field.cast(value)
assert element == value
assert field.validate(value)
def is_not_field_element(field, value):
"""Utility method to avoid redundant logic in tests."""
with pytest.raises(ValueError, match="not an element of the field"):
field.cast(value)
assert not field.validate(value)
assert not field.validate(value, silent=True)
with pytest.raises(ValueError, match="not an element of the field"):
field.validate(value, silent=False)