|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from decimal import Decimal |
| 6 | +from uuid import UUID |
| 7 | + |
5 | 8 | import pytest |
6 | 9 | from agent_framework import Filter, FilterGroup |
7 | 10 |
|
@@ -55,6 +58,66 @@ def test_numeric_equality_does_not_coerce_invalid_values(collection, value): |
55 | 58 | assert sql == "(NOT (1=0))" and binds == {} |
56 | 59 |
|
57 | 60 |
|
| 61 | +@pytest.mark.parametrize( |
| 62 | + ("field", "value", "expected_bind"), |
| 63 | + [ |
| 64 | + ("number", Decimal("2"), 2), |
| 65 | + ("number", Decimal("2.0"), 2), |
| 66 | + ("number", Decimal("9223372036854775807"), 2**63 - 1), |
| 67 | + ("number", Decimal("2.5"), None), |
| 68 | + ("number", Decimal("9223372036854775808"), None), |
| 69 | + ("text", Decimal("0.5"), 0.5), |
| 70 | + ("text", Decimal("0.1"), None), |
| 71 | + ("text", Decimal("1e1000"), None), |
| 72 | + ], |
| 73 | +) |
| 74 | +def test_decimal_equality_uses_exact_numeric_comparison(definition_factory, field, value, expected_bind): |
| 75 | + compiler = _FilterCompiler(definition_factory(data_type="float")) |
| 76 | + for operator in ("eq", "ne"): |
| 77 | + sql, binds = compiler.compile(Filter(field, operator, value)) |
| 78 | + if expected_bind is None: |
| 79 | + assert sql == ("1=0" if operator == "eq" else "(NOT (1=0))") |
| 80 | + assert binds == {} |
| 81 | + else: |
| 82 | + assert f'"{field if field == "number" else "body text"}" = :f0' in sql |
| 83 | + assert binds == {"f0": expected_bind} |
| 84 | + |
| 85 | + |
| 86 | +def test_decimal_membership_and_nonfinite_values(collection): |
| 87 | + sql, binds = collection._prepare_filter(Filter("number", "in", [Decimal("2"), Decimal("2.5")])) |
| 88 | + assert '"number" = :f0' in sql |
| 89 | + assert binds == {"f0": 2} |
| 90 | + sql, binds = collection._prepare_filter(Filter("number", "not_in", [Decimal("2.5")])) |
| 91 | + assert sql == '("number" IS NOT NULL AND (NOT ((1=0))))' |
| 92 | + assert binds == {} |
| 93 | + with pytest.raises(ValueError, match="finite"): |
| 94 | + collection._prepare_filter(Filter("number", "eq", Decimal("NaN"))) |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize( |
| 98 | + ("operator", "value", "expected_sql"), |
| 99 | + [ |
| 100 | + ("eq", "not-a-uuid", "1=0"), |
| 101 | + ("ne", "not-a-uuid", "(NOT (1=0))"), |
| 102 | + ("in", ["not-a-uuid"], '("doc""id" IS NOT NULL AND (1=0))'), |
| 103 | + ("not_in", ["not-a-uuid"], '("doc""id" IS NOT NULL AND (NOT ((1=0))))'), |
| 104 | + ], |
| 105 | +) |
| 106 | +def test_malformed_uuid_filter_is_non_equal(definition_factory, operator, value, expected_sql): |
| 107 | + compiler = _FilterCompiler(definition_factory(key_type="UUID")) |
| 108 | + sql, binds = compiler.compile(Filter("id", operator, value)) |
| 109 | + assert sql == expected_sql |
| 110 | + assert binds == {} |
| 111 | + |
| 112 | + |
| 113 | +def test_valid_uuid_filter_values_remain_bound(definition_factory): |
| 114 | + compiler = _FilterCompiler(definition_factory(key_type="UUID")) |
| 115 | + valid = UUID(int=2) |
| 116 | + sql, binds = compiler.compile(Filter("id", "in", ["malformed", valid])) |
| 117 | + assert '"doc""id" = :f0' in sql |
| 118 | + assert binds == {"f0": str(valid)} |
| 119 | + |
| 120 | + |
58 | 121 | def test_boolean_equality_does_not_coerce_numbers(collection): |
59 | 122 | sql, binds = collection._prepare_filter(Filter("flag", "eq", True)) |
60 | 123 | assert '"flag" = :f0' in sql and binds == {"f0": 1} |
|
0 commit comments