Skip to content

dp3.testing.assertions

Assertion helpers for DP3 module tests.

ModuleAssertions

Bases: TestCase

Partial-match assertions for module hook outputs.

assert_task_emitted

assert_task_emitted(tasks: Iterable[DataPointTask], *, etype: Any = _UNSET, eid: Any = _UNSET, data_points: Any = _UNSET, tags: Any = _UNSET, ttl_tokens: Any = _UNSET, delete: Any = _UNSET) -> DataPointTask

Assert that a task matching the supplied DataPointTask fields was emitted.

Source code in dp3/testing/assertions.py
def assert_task_emitted(  # noqa: PLR0913
    self,
    tasks: Iterable[DataPointTask],
    *,
    etype: Any = _UNSET,
    eid: Any = _UNSET,
    data_points: Any = _UNSET,
    tags: Any = _UNSET,
    ttl_tokens: Any = _UNSET,
    delete: Any = _UNSET,
) -> DataPointTask:
    """Assert that a task matching the supplied ``DataPointTask`` fields was emitted."""
    expected = _selected_fields(
        etype=etype,
        eid=eid,
        data_points=data_points,
        tags=tags,
        ttl_tokens=ttl_tokens,
        delete=delete,
    )
    task_list = list(tasks)
    for task in task_list:
        if self._partial_match(dump_value(task), expected):
            return task
    self.fail(f"No emitted task matched {expected!r}. Emitted tasks: {dump_value(task_list)!r}")

assert_datapoint

assert_datapoint(tasks: Iterable[DataPointTask], *, etype: Any = _UNSET, eid: Any = _UNSET, attr: Any = _UNSET, src: Any = _UNSET, v: Any = _UNSET, c: Any = _UNSET, t1: Any = _UNSET, t2: Any = _UNSET) -> DataPointBase

Assert that a datapoint matching the supplied DataPointBase fields was emitted.

Source code in dp3/testing/assertions.py
def assert_datapoint(  # noqa: PLR0913
    self,
    tasks: Iterable[DataPointTask],
    *,
    etype: Any = _UNSET,
    eid: Any = _UNSET,
    attr: Any = _UNSET,
    src: Any = _UNSET,
    v: Any = _UNSET,
    c: Any = _UNSET,
    t1: Any = _UNSET,
    t2: Any = _UNSET,
) -> DataPointBase:
    """Assert that a datapoint matching the supplied ``DataPointBase`` fields was emitted."""
    expected = _selected_fields(
        etype=etype,
        eid=eid,
        attr=attr,
        src=src,
        v=v,
        c=c,
        t1=t1,
        t2=t2,
    )
    datapoints = list(self.iter_datapoints(tasks))
    for dp in datapoints:
        if self._partial_match(dump_value(dp), expected):
            return dp
    self.fail(
        f"No emitted datapoint matched {expected!r}. "
        f"Emitted datapoints: {dump_value(datapoints)!r}"
    )

dump_value

dump_value(value: Any) -> Any

Convert pydantic values recursively to plain Python containers.

Source code in dp3/testing/assertions.py
def dump_value(value: Any) -> Any:
    """Convert pydantic values recursively to plain Python containers."""
    if isinstance(value, BaseModel):
        return value.model_dump()
    if isinstance(value, list):
        return [dump_value(item) for item in value]
    if isinstance(value, tuple):
        return tuple(dump_value(item) for item in value)
    if isinstance(value, dict):
        return {key: dump_value(item) for key, item in value.items()}
    return value