Files
hatchet/examples/python/dependency_injection/test_dependency_injection.py
matt c8d5144ed4 [Python] Feat: Dependency Injection, Improved error handling (#2067)
* feat: first pass at dependency injection

* feat: add DI example + tests

* feat: finish up tests

* feat: docs

* chore: gen

* chore: lint

* chore: changelog + ver

* fix: split up paragraphs

* refactor: improve impl

* chore: gen

* feat: inject input + ctx into deps

* chore: gen

* [Python] Feat: More use of `logger.exception` (#2069)

* feat: add more instances of `logger.exception`

* chore: ver

* chore: changelog

* fix: one more error log

* chore: gen

* chore: gen

* chore: lint

* fix: improve shutdown

* chore: changelog

* unwind: exit handler

* feat: task run error

* feat: improve error serde with more context

* chore: changelog

* fix: changelog

* chore: gen

* fix: rm celpy + lark dep, casing issues

* chore: changelog

* fix: respect log levels over the API

* fix: changelog

* refactor: rename log forwarder

* fix: circular import
2025-08-11 23:10:44 -04:00

50 lines
1.3 KiB
Python

import pytest
from examples.dependency_injection.worker import (
ASYNC_DEPENDENCY_VALUE,
SYNC_DEPENDENCY_VALUE,
Output,
async_dep,
async_task_with_dependencies,
di_workflow,
durable_async_task_with_dependencies,
durable_sync_task_with_dependencies,
sync_dep,
sync_task_with_dependencies,
)
from hatchet_sdk import EmptyModel
from hatchet_sdk.runnables.workflow import Standalone
@pytest.mark.parametrize(
"task",
[
async_task_with_dependencies,
sync_task_with_dependencies,
durable_async_task_with_dependencies,
durable_sync_task_with_dependencies,
],
)
@pytest.mark.asyncio(loop_scope="session")
async def test_di_standalones(
task: Standalone[EmptyModel, Output],
) -> None:
result = await task.aio_run()
assert isinstance(result, Output)
assert result.sync_dep == SYNC_DEPENDENCY_VALUE
assert result.async_dep == ASYNC_DEPENDENCY_VALUE
@pytest.mark.asyncio(loop_scope="session")
async def test_di_workflows() -> None:
result = await di_workflow.aio_run()
assert len(result) == 4
for output in result.values():
parsed = Output.model_validate(output)
assert parsed.sync_dep == SYNC_DEPENDENCY_VALUE
assert parsed.async_dep == ASYNC_DEPENDENCY_VALUE