60 lines
2.8 KiB
Python
60 lines
2.8 KiB
Python
from decimal import Decimal
|
|
|
|
from app.domain.constraints import evaluate_constraint
|
|
from app.domain.enums import ConstraintOperator
|
|
from app.domain.models import Constraint
|
|
from tests.conftest import statement
|
|
|
|
|
|
def _eval(definitions, attribute, operator, value, stmt, confidence="0.8"):
|
|
return evaluate_constraint(
|
|
Constraint("c1", attribute, ConstraintOperator(operator), value, True),
|
|
stmt,
|
|
definitions[attribute],
|
|
Decimal(confidence),
|
|
)
|
|
|
|
|
|
def test_number_operators_pass_fail_unknown(definitions):
|
|
assert _eval(definitions, "ram_gb", ">=", 16, statement("ram_gb", 16)).status == "PASS"
|
|
assert _eval(definitions, "ram_gb", ">", 16, statement("ram_gb", 16)).status == "FAIL"
|
|
assert _eval(definitions, "ram_gb", "<", 16, statement("ram_gb", 8)).status == "PASS"
|
|
assert _eval(definitions, "ram_gb", "<=", 16, statement("ram_gb", 16)).status == "PASS"
|
|
assert _eval(definitions, "ram_gb", ">=", 16, None).status == "UNKNOWN"
|
|
|
|
|
|
def test_equality_and_in(definitions):
|
|
assert _eval(definitions, "color", "==", "black", statement("color", "black")).status == "PASS"
|
|
assert _eval(definitions, "color", "!=", "silver", statement("color", "black")).status == "PASS"
|
|
assert _eval(definitions, "has_lte", "==", True, statement("has_lte", True)).status == "PASS"
|
|
assert _eval(definitions, "ram_gb", "in", [16, 32], statement("ram_gb", 32)).status == "PASS"
|
|
assert _eval(definitions, "ram_gb", "in", [16], statement("ram_gb", 8)).status == "FAIL"
|
|
|
|
|
|
def test_contains_and_empty_list(definitions):
|
|
empty = statement("ports", [])
|
|
assert _eval(definitions, "ports", "exists", None, empty).status == "PASS"
|
|
assert _eval(definitions, "ports", "contains", "hdmi", empty).status == "FAIL"
|
|
present = statement("ports", ["hdmi", "usb-c"])
|
|
assert _eval(definitions, "ports", "contains", "hdmi", present).status == "PASS"
|
|
|
|
|
|
def test_exists_unknown_when_unusable(definitions):
|
|
assert _eval(definitions, "ram_gb", "exists", None, None).status == "UNKNOWN"
|
|
unknown = statement("ram_gb", None, status="unknown")
|
|
assert _eval(definitions, "ram_gb", "exists", None, unknown).status == "UNKNOWN"
|
|
assert _eval(definitions, "ram_gb", "!=", 8, None).status == "UNKNOWN"
|
|
|
|
|
|
def test_confidence_boundary(definitions):
|
|
exact = statement("ram_gb", 32, confidence="0.8")
|
|
below = statement("ram_gb", 32, confidence="0.799")
|
|
assert _eval(definitions, "ram_gb", ">=", 16, exact).status == "PASS"
|
|
assert _eval(definitions, "ram_gb", ">=", 16, below).status == "UNKNOWN"
|
|
|
|
|
|
def test_no_type_coercion(definitions):
|
|
text_number = statement("ram_gb", "32")
|
|
text_number = text_number.__class__(**{**text_number.__dict__, "normalized_value": "32"})
|
|
assert _eval(definitions, "ram_gb", ">=", 16, text_number).status == "UNKNOWN"
|