deer-flow/backend/tests/test_boolean_guards.py
yetuge 0eed90df3a
refactor(config): converge the boolean guards into one shared helper (#6226)
* refactor(config): converge the boolean guards into one shared helper

Willem-bd's #6171 review follow-up: a tiny shared helper lets the next numeric field get the guard in one line and unifies the messages. Consolidates 14 validator copies across 9 config files into deerflow/config/_boolean_guards.reject_boolean(value, info, kind=...); the bare per-file messages gain their field name prefix, command_timeout keeps its distinct wording.

Signed-off-by: yetuge <2219677952@qq.com>

* refactor(config): converge stream_bridge_config and pin the guard wording

Review follow-ups on #6226: convert the last surviving per-file copy (stream_bridge_config) so deerflow/config is fully converged, make kind required keyword-only so a float field cannot silently inherit integer wording, and pin the unified field-prefixed messages with direct tests.

Signed-off-by: yetuge <2219677952@qq.com>

* test(config): cast the boolean-guard test probe to ValidationInfo

The _info probe was annotated -> object, so a type checker flags every call
site that passes its result to reject_boolean's info: ValidationInfo
parameter (runtime was fine since the helper only reads .field_name).
Return cast(ValidationInfo, _Probe(field_name)) to keep the probe honest
for both.

Signed-off-by: yetuge <2219677952@qq.com>

---------

Signed-off-by: yetuge <2219677952@qq.com>
2026-10-04 09:09:15 +08:00

35 lines
1.1 KiB
Python

"""Direct tests for the shared boolean-rejection helper."""
from typing import cast
import pytest
from pydantic import ValidationInfo
from deerflow.config._boolean_guards import reject_boolean
class _Probe:
"""Minimal harness so ``info.field_name`` resolves like a real field."""
def __init__(self, field_name: str) -> None:
self.field_name = field_name
def _info(field_name: str) -> ValidationInfo:
return cast(ValidationInfo, _Probe(field_name))
def test_reject_boolean_names_the_field() -> None:
with pytest.raises(ValueError, match="recursion_limit must be an integer, not a boolean"):
reject_boolean(True, _info("recursion_limit"), kind="an integer")
def test_reject_boolean_passes_numbers_through() -> None:
assert reject_boolean(0, _info("hard_limit"), kind="an integer") == 0
assert reject_boolean(2.5, _info("interval"), kind="a number") == 2.5
def test_reject_boolean_custom_kind_wording() -> None:
with pytest.raises(ValueError, match="command_timeout must be a number of seconds or null, not a boolean"):
reject_boolean(True, _info("command_timeout"), kind="a number of seconds or null")