Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

Adds unit tests for Burr

elijahbenizzy2548608f7d18
diff --git a/tests/core/test_action.py b/tests/core/test_action.py
new file mode 100644
index 00000000..102a565b
--- /dev/null
+++ b/tests/core/test_action.py
@@ -0,0 +1,117 @@
+from burr.core import State
+from burr.core.action import Action, Condition, Function, Result, default
+
+
+def test_is_async_true():
+ class AsyncFunction(Function):
+ def reads(self) -> list[str]:
+ return []
+
+ async def run(self, state: State) -> dict:
+ return {}
+
+ func = AsyncFunction()
+ assert func.is_async()
+
+
+def test_is_async_false():
+ class SyncFunction(Function):
+ def reads(self) -> list[str]:
+ return []
+
+ def run(self, state: State) -> dict:
+ return {}
+
+ func = SyncFunction()
+ assert not func.is_async()
+
+
+def test_with_name():
+ class BasicAction(Action):
+ @property
+ def reads(self) -> list[str]:
+ return ["input_variable"]
+
+ def run(self, state: State) -> dict:
+ return {"output_variable": state["input_variable"]}
+
+ @property
+ def writes(self) -> list[str]:
+ return ["output_variable"]
+
+ def update(self, result: dict, state: State) -> State:
+ return state.update(**result)
+
+ action = BasicAction()
+ assert action.name is None # Nothing set initially
+ with_name = action.with_name("my_action")
+ assert with_name.name == "my_action" # Name set on copy
+ assert with_name.reads == action.reads
+ assert with_name.writes == action.writes
+
+
+def test_condition():
+ cond = Condition(["foo"], lambda state: state["foo"] == "bar", name="foo")
+ assert cond.name == "foo"
+ assert cond.reads == ["foo"]
+ assert cond.run(State({"foo": "bar"})) == {Condition.KEY: True}
+ assert cond.run(State({"foo": "baz"})) == {Condition.KEY: False}
+
+
+def test_condition_when():
+ cond = Condition.when(foo="bar")
+ assert cond.name == "foo=bar"
+ assert cond.reads == ["foo"]
+ assert cond.run(State({"foo": "bar"})) == {Condition.KEY: True}
+ assert cond.run(State({"foo": "baz"})) == {Condition.KEY: False}
+
+
+def test_condition_when_complex():
+ cond = Condition.when(foo="bar", baz="qux")
+ assert cond.name == "baz=qux, foo=bar"
+ assert sorted(cond.reads) == ["baz", "foo"]
+ assert cond.run(State({"foo": "bar", "baz": "qux"})) == {Condition.KEY: True}
+ assert cond.run(State({"foo": "baz", "baz": "qux"})) == {Condition.KEY: False}
+ assert cond.run(State({"foo": "bar", "baz": "corge"})) == {Condition.KEY: False}
+ assert cond.run(State({"foo": "baz", "baz": "corge"})) == {Condition.KEY: False}
+
+
+def test_condition_default():
+ cond = default
+ assert cond.name == "default"
+ assert cond.reads == []
+ assert cond.run(State({"foo": "bar"})) == {Condition.KEY: True}
+
+
+def test_condition_expr():
+ cond = Condition.expr("foo == 'bar'")
+ assert cond.name == "foo == 'bar'"
+ assert cond.reads == ["foo"]
+ assert cond.run(State({"foo": "bar"})) == {Condition.KEY: True}
+ assert cond.run(State({"foo": "baz"})) == {Condition.KEY: False}
+
+
+def test_condition_expr_complex():
+ cond = Condition.expr("foo == 'bar' and baz == 'qux'")
+ assert cond.name == "foo == 'bar' and baz == 'qux'"
+ assert sorted(cond.reads) == ["baz", "foo"]
+ assert cond.run(State({"foo": "bar", "baz": "qux"})) == {Condition.KEY: True}
+ assert cond.run(State({"foo": "baz", "baz": "qux"})) == {Condition.KEY: False}
+ assert cond.run(State({"foo": "bar", "baz": "corge"})) == {Condition.KEY: False}
+ assert cond.run(State({"foo": "baz", "baz": "corge"})) == {Condition.KEY: False}
+
+
+def test_result():
+ result = Result(fields=["foo", "bar"])
+ assert result.run(State({"foo": "baz", "bar": "qux", "baz": "quux"})) == {
+ "foo": "baz",
+ "bar": "qux",
+ }
+ assert result.writes == [] # doesn't write anything
+ assert result.reads == ["foo", "bar"]
+ # no results
+ assert result.update(
+ {"foo": "baz", "bar": "qux"}, State({"foo": "baz", "bar": "qux", "baz": "quux"})
+ ) == State(
+ {"foo": "baz", "bar": "qux", "baz": "quux"}
+ ) # no impact
diff --git a/tests/core/test_application.py b/tests/core/test_application.py
new file mode 100644
index 00000000..17b4aafa
--- /dev/null
+++ b/tests/core/test_application.py
@@ -0,0 +1,502 @@
+import asyncio
+from typing import Awaitable, Callable
+
+import pytest
+
+from burr.core import State
+from burr.core.action import Action, Condition, Result, default
+from burr.core.application import (
+ Application,
+ ApplicationBuilder,
+ Transition,
+ _arun_function,
+ _assert_set,
+ _run_function,
+ _validate_actions,
+ _validate_start,
+ _validate_transitions,
+)
+from burr.lifecycle import (
+ PostRunStepHook,
+ PostRunStepHookAsync,
+ PreRunStepHook,
+ PreRunStepHookAsync,
+ internal,
+)
+
+
+class PassedInAction(Action):
+ def __init__(
+ self,
+ reads: list[str],
+ writes: list[str],
+ fn: Callable[[State], dict],
+ update_fn: Callable[[dict, State], State],
+ ):
+ super(PassedInAction, self).__init__()
+ self._reads = reads
+ self._writes = writes
+ self._fn = fn
+ self._update_fn = update_fn
+
+ def run(self, state: State) -> dict:
+ return self._fn(state)
+
+ def update(self, result: dict, state: State) -> State:
+ return self._update_fn(result, state)
+
+ @property
+ def reads(self) -> list[str]:
+ return self._reads
+
+ @property
+ def writes(self) -> list[str]:
+ return self._writes
+
+
+class PassedInActionAsync(PassedInAction):
+ def __init__(
+ self,
+ reads: list[str],
+ writes: list[str],
+ fn: Callable[[State], Awaitable[dict]],
+ update_fn: Callable[[dict, State], State],
+ ):
+ super().__init__(reads=reads, writes=writes, fn=fn, update_fn=update_fn) # type: ignore
+
+ async def run(self, state: State) -> dict:
+ return await self._fn(state)
+
+
+base_counter_action = PassedInAction(
+ reads=["counter"],
+ writes=["counter"],
+ fn=lambda state: {"counter": state.get("counter", 0) + 1},
+ update_fn=lambda result, state: state.update(**result),
+)
+
+
+async def _counter_update_async(state: State) -> dict:
+ await asyncio.sleep(0.0001) # just so we can make this *truly* async
+ # does not matter, but more accurately simulates an async function
+ return {"counter": state.get("counter", 0) + 1}
+
+
+base_counter_action_async = PassedInActionAsync(
+ reads=["counter"],
+ writes=["counter"],
+ fn=_counter_update_async,
+ update_fn=lambda result, state: state.update(**result),
+)
+
+
+def test_run_function():
+ """Tests that we can run a function"""
+ action = base_counter_action
+ state = State({})
+ result = _run_function(action, state)
+ assert result == {"counter": 1}
+
+
+def test_run_function_cant_run_async():
+ """Tests that we can't run an async function"""
+ action = base_counter_action_async
+ state = State({})
+ with pytest.raises(ValueError, match="async"):
+ _run_function(action, state)
+
+
+async def test_a_run_function():
+ """Tests that we can run an async function"""
+ action = base_counter_action_async
+ state = State({})
+ result = await _arun_function(action, state)
+ assert result == {"counter": 1}
+
+
+def test_app_step():
+ """Tests that we can run a step in an app"""
+ counter_action = base_counter_action.with_name("counter")
+ app = Application(
+ actions=[counter_action],
+ transitions=[Transition(counter_action, counter_action, default)],
+ state=State({}),
+ initial_step="counter",
+ )
+ action, result, state = app.step()
+ assert action.name == "counter"
+ assert result == {"counter": 1}
+
+
+def test_app_step_done():
+ """Tests that when we cannot run a step, we return None"""
+ counter_action = base_counter_action.with_name("counter")
+ app = Application(
+ actions=[counter_action], transitions=[], state=State({}), initial_step="counter"
+ )
+ app.step()
+ assert app.step() is None
+
+
+async def test_app_astep():
+ """Tests that we can run an async step in an app"""
+ counter_action = base_counter_action_async.with_name("counter_async")
+ app = Application(
+ actions=[counter_action],
+ transitions=[Transition(counter_action, counter_action, default)],
+ state=State({}),
+ initial_step="counter_async",
+ )
+ action, result, state = await app.astep()
+ assert action.name == "counter_async"
+ assert result == {"counter": 1}
+
+
+async def test_app_astep_done():
+ """Tests that when we cannot run a step, we return None"""
+ counter_action = base_counter_action_async.with_name("counter_async")
+ app = Application(
+ actions=[counter_action], transitions=[], state=State({}), initial_step="counter_async"
+ )
+ await app.astep()
+ assert await app.astep() is None
+
+
+# internal API
+def test_app_many_steps():
+ counter_action = base_counter_action.with_name("counter")
+ app = Application(
+ actions=[counter_action],
+ transitions=[Transition(counter_action, counter_action, default)],
+ state=State({}),
+ initial_step="counter",
+ )
+ action, result = None, None
+ for i in range(100):
+ action, result, state = app.step()
+ assert action.name == "counter"
+ assert result == {"counter": 100}
+
+
+async def test_app_many_a_steps():
+ counter_action = base_counter_action_async.with_name("counter_async")
+ app = Application(
+ actions=[counter_action],
+ transitions=[Transition(counter_action, counter_action, default)],
+ state=State({}),
+ initial_step="counter_async",
+ )
+ action, result = None, None
+ for i in range(100):
+ action, result, state = await app.astep()
+ assert action.name == "counter_async"
+ assert result == {"counter": 100}
+
+
+def test_app_iterate():
+ result_action = Result(fields=["counter"]).with_name("result")
+ counter_action = base_counter_action.with_name("counter")
+ app = Application(
+ actions=[counter_action, result_action],
+ transitions=[
+ Transition(counter_action, counter_action, Condition.expr("counter < 10")),
+ Transition(counter_action, result_action, default),
+ ],
+ state=State({}),
+ initial_step="counter",
+ )
+ res = []
+ gen = app.iterate(until=["result"])
+ counter = 0
+ try:
+ while True:
+ action, result, state = next(gen)
+ if action.name == "counter":
+ assert state["counter"] == counter + 1
+ assert result["counter"] == state["counter"]
+ counter = result["counter"]
+ else:
+ res.append(result)
+ assert state["counter"] == 10
+ assert result["counter"] == 10
+ except StopIteration as e:
+ stop_iteration_error = e
+ generator_result = stop_iteration_error.value
+ state, results = generator_result
+ assert state["counter"] == 10
+ assert len(results) == 1
+ (result,) = results
+ assert result["counter"] == 10
+
+
+async def test_app_a_iterate():
+ result_action = Result(fields=["counter"]).with_name("result")
+ counter_action = base_counter_action_async.with_name("counter")
+ app = Application(
+ actions=[counter_action, result_action],
+ transitions=[
+ Transition(counter_action, counter_action, Condition.expr("counter < 10")),
+ Transition(counter_action, result_action, default),
+ ],
+ state=State({}),
+ initial_step="counter",
+ )
+ res = []
+ gen = app.aiterate(until=["result"])
+ counter = 0
+ # Note that we use an async-for loop cause the API is different, this doesn't
+ # return anything (async generators are not allowed to).
+ async for action, result, state in gen:
+ if action.name == "counter":
+ assert state["counter"] == counter + 1
+ assert result["counter"] == state["counter"]
+ counter = result["counter"]
+ else:
+ res.append(result)
+ assert state["counter"] == 10
+ assert result["counter"] == 10
+
+
+def test_app_run():
+ result_action = Result(fields=["counter"]).with_name("result")
+ counter_action = base_counter_action.with_name("counter")
+ app = Application(
+ actions=[counter_action, result_action],
+ transitions=[
+ Transition(counter_action, counter_action, Condition.expr("counter < 10")),
+ Transition(counter_action, result_action, default),
+ ],
+ state=State({}),
+ initial_step="counter",
+ )
+ state, results = app.run(until=["result"])
+ assert state["counter"] == 10
+ assert len(results) == 1
+ assert results[0]["counter"] == 10
+
+
+async def test_app_a_run():
+ result_action = Result(fields=["counter"]).with_name("result")
+ counter_action = base_counter_action_async.with_name("counter")
+ app = Application(
+ actions=[counter_action, result_action],
+ transitions=[
+ Transition(counter_action, counter_action, Condition.expr("counter < 10")),
+ Transition(counter_action, result_action, default),
+ ],
+ state=State({}),
+ initial_step="counter",
+ )
+ state, results = await app.arun(until=["result"])
+ assert state["counter"] == 10
+ assert len(results) == 1
+ (result,) = results
+ assert result["counter"] == 10
+
+
+async def test_app_a_run_async_and_sync():
+ result_action = Result(fields=["counter"]).with_name("result")
+ counter_action_sync = base_counter_action_async.with_name("counter_sync")
+ counter_action_async = base_counter_action_async.with_name("counter_async")
+ app = Application(
+ actions=[counter_action_sync, counter_action_async, result_action],
+ transitions=[
+ Transition(counter_action_sync, counter_action_async, Condition.expr("counter < 20")),
+ Transition(counter_action_async, counter_action_sync, default),
+ Transition(counter_action_sync, result_action, default),
+ ],
+ state=State({}),
+ initial_step="counter_sync",
+ )
+ state, results = await app.arun(until=["result"])
+ assert state["counter"] > 20
+ assert len(results) == 1
+ (result,) = results
+ assert result["counter"] > 20
+
+
+def test_app_set_state():
+ counter_action = base_counter_action.with_name("counter")
+ app = Application(
+ actions=[counter_action],
+ transitions=[Transition(counter_action, counter_action, default)],
+ state=State(),
+ initial_step="counter",
+ )
+ assert "counter" not in app.state # initial value
+ app.step()
+ assert app.state["counter"] == 1 # updated value
+ state = app.state
+ app.update_state(state.update(counter=2))
+ assert app.state["counter"] == 2 # updated value
+
+
+def test_app_get_next_step():
+ counter_action_1 = base_counter_action.with_name("counter_1")
+ counter_action_2 = base_counter_action.with_name("counter_2")
+ counter_action_3 = base_counter_action.with_name("counter_3")
+ app = Application(
+ actions=[counter_action_1, counter_action_2, counter_action_3],
+ transitions=[
+ Transition(counter_action_1, counter_action_2, default),
+ Transition(counter_action_2, counter_action_3, default),
+ Transition(counter_action_3, counter_action_1, default),
+ ],
+ state=State(),
+ initial_step="counter_1",
+ )
+ # uninitialized -- counter_1
+ assert app.get_next_action().name == "counter_1"
+ app.step()
+ # ran counter_1 -- counter_2
+ assert app.get_next_action().name == "counter_2"
+ app.step()
+ # ran counter_2 -- counter_3
+ assert app.get_next_action().name == "counter_3"
+ app.step()
+ # ran counter_3 -- back to counter_1
+ assert app.get_next_action().name == "counter_1"
+
+
+def test_application_builder_complete():
+ app = (
+ ApplicationBuilder()
+ .with_state(counter=0)
+ .with_actions(counter=base_counter_action, result=Result(fields=["counter"]))
+ .with_transitions(
+ ("counter", "counter", Condition.expr("counter < 10")), ("counter", "result")
+ )
+ .with_entrypoint("counter")
+ .build()
+ )
+ assert len(app._actions) == 2
+ assert len(app._transitions) == 2
+ assert app.get_next_action().name == "counter"
+
+
+def test__validate_transitions_correct():
+ _validate_transitions(
+ [("counter", "counter", Condition.expr("counter < 10")), ("counter", "result", default)],
+ {"counter", "result"},
+ )
+
+
+def test__validate_transitions_missing_action():
+ with pytest.raises(ValueError, match="not found"):
+ _validate_transitions(
+ [
+ ("counter", "counter", Condition.expr("counter < 10")),
+ ("counter", "result", default),
+ ],
+ {"counter"},
+ )
+
+
+def test__validate_transitions_redundant_transition():
+ with pytest.raises(ValueError, match="redundant"):
+ _validate_transitions(
+ [
+ ("counter", "counter", Condition.expr("counter < 10")),
+ ("counter", "result", default),
+ ("counter", "counter", default), # this is unreachable as we already have a default
+ ],
+ {"counter", "result"},
+ )
+
+
+def test__validate_start_valid():
+ _validate_start("counter", {"counter", "result"})
+
+
+def test__validate_start_not_found():
+ with pytest.raises(ValueError, match="not found"):
+ _validate_start("counter", {"result"})
+
+
+def test__validate_actions_valid():
+ _validate_actions([Result(["test"])])
+
+
+def test__validate_actions_empty():
+ with pytest.raises(ValueError, match="at least one"):
+ _validate_actions([])
+
+
+def test__asset_set():
+ _assert_set("foo", "foo", "bar")
+
+
+def test__assert_set_unset():
+ with pytest.raises(ValueError, match="foo"):
+ _assert_set(None, "foo", "bar")
+
+
+def test_application_builder_unset():
+ with pytest.raises(ValueError):
+ ApplicationBuilder().build()
+
+
+def test_application_runs_hooks_sync():
+ class ActionTracker(PreRunStepHook, PostRunStepHook):
+ def __init__(self):
+ self.pre_called = []
+ self.post_called = []
+
+ def pre_run_step(self, *, action: Action, **future_kwargs):
+ self.pre_called.append(action.name)
+
+ def post_run_step(self, *, action: Action, **future_kwargs):
+ self.post_called.append(action.name)
+
+ tracker = ActionTracker()
+ counter_action = base_counter_action.with_name("counter")
+ result_action = Result(fields=["counter"]).with_name("result")
+ app = Application(
+ actions=[counter_action, result_action],
+ transitions=[
+ Transition(counter_action, result_action, Condition.expr("counter >= 10")),
+ Transition(counter_action, counter_action, default),
+ ],
+ state=State({}),
+ initial_step="counter",
+ adapter_set=internal.LifecycleAdapterSet(tracker),
+ )
+ app.run(until=["result"])
+ assert set(tracker.pre_called) == {"counter", "result"}
+ assert set(tracker.post_called) == {"counter", "result"}
+ assert len(tracker.pre_called) == 11
+ assert len(tracker.post_called) == 11
+
+
+async def test_application_runs_hooks_async():
+ class ActionTrackerAsync(PreRunStepHookAsync, PostRunStepHookAsync):
+ def __init__(self):
+ self.pre_called = []
+ self.post_called = []
+
+ async def pre_run_step(self, *, action: Action, **future_kwargs):
+ await asyncio.sleep(0.0001)
+ self.pre_called.append(action.name)
+
+ async def post_run_step(self, *, action: Action, **future_kwargs):
+ await asyncio.sleep(0.0001)
+ self.post_called.append(action.name)
+
+ tracker = ActionTrackerAsync()
+ counter_action = base_counter_action.with_name("counter")
+ result_action = Result(fields=["counter"]).with_name("result")
+ app = Application(
+ actions=[counter_action, result_action],
+ transitions=[
+ Transition(counter_action, result_action, Condition.expr("counter >= 10")),
+ Transition(counter_action, counter_action, default),
+ ],
+ state=State({}),
+ initial_step="counter",
+ adapter_set=internal.LifecycleAdapterSet(tracker),
+ )
+ await app.arun(until=["result"])
+ assert set(tracker.pre_called) == {"counter", "result"}
+ assert set(tracker.post_called) == {"counter", "result"}
+ assert len(tracker.pre_called) == 11
+ assert len(tracker.post_called) == 11
diff --git a/tests/core/test_implementations.py b/tests/core/test_implementations.py
new file mode 100644
index 00000000..0fa5bc32
--- /dev/null
+++ b/tests/core/test_implementations.py
@@ -0,0 +1,15 @@
+import pytest
+
+from burr.core import State
+from burr.core.implementations import Placeholder
+
+
+def test_placedholder_action():
+ action = Placeholder(reads=["foo"], writes=["bar"]).with_name("test")
+ assert action.reads == ["foo"]
+ assert action.writes == ["bar"]
+ with pytest.raises(NotImplementedError):
+ action.run(State({}))
+
+ with pytest.raises(NotImplementedError):
+ action.update({}, State({}))
diff --git a/tests/core/test_state.py b/tests/core/test_state.py
new file mode 100644
index 00000000..fa813051
--- /dev/null
+++ b/tests/core/test_state.py
@@ -0,0 +1,82 @@
+import pytest
+
+from burr.core.state import State
+
+
+def test_state_access():
+ state = State({"foo": "bar"})
+ assert state["foo"] == "bar"
+
+
+def test_state_access_missing():
+ state = State({"foo": "bar"})
+ with pytest.raises(KeyError):
+ _ = state["baz"]
+
+
+def test_state_get():
+ state = State({"foo": "bar"})
+ assert state.get("foo") == "bar"
+
+
+def test_state_get_missing():
+ state = State({"foo": "bar"})
+ assert state.get("baz") is None
+
+
+def test_state_get_missing_default():
+ state = State({"foo": "bar"})
+ assert state.get("baz", "qux") == "qux"
+
+
+def test_state_in():
+ state = State({"foo": "bar"})
+ assert "foo" in state
+ assert "baz" not in state
+
+
+def test_state_get_all():
+ state = State({"foo": "bar", "baz": "qux"})
+ assert state.get_all() == {"foo": "bar", "baz": "qux"}
+
+
+def test_state_merge():
+ state = State({"foo": "bar", "baz": "qux"})
+ other = State({"foo": "baz", "quux": "corge"})
+ merged = state.merge(other)
+ assert merged.get_all() == {"foo": "baz", "baz": "qux", "quux": "corge"}
+
+
+def test_state_subset():
+ state = State({"foo": "bar", "baz": "qux"})
+ subset = state.subset("foo")
+ assert subset.get_all() == {"foo": "bar"}
+
+
+def test_state_append():
+ state = State({"foo": ["bar"]})
+ appended = state.append(foo="baz")
+ assert appended.get_all() == {"foo": ["bar", "baz"]}
+
+
+def test_state_update():
+ state = State({"foo": "bar", "baz": "qux"})
+ updated = state.update(foo="baz")
+ assert updated.get_all() == {"foo": "baz", "baz": "qux"}
+
+
+def test_state_init():
+ state = State({"foo": "bar", "baz": "qux"})
+ assert state.get_all() == {"foo": "bar", "baz": "qux"}
+
+
+def test_state_wipe_delete():
+ state = State({"foo": "bar", "baz": "qux"})
+ wiped = state.wipe(delete=["foo"])
+ assert wiped.get_all() == {"baz": "qux"}
+
+
+def test_state_wipe_keep():
+ state = State({"foo": "bar", "baz": "qux"})
+ wiped = state.wipe(keep=["foo"])
+ assert wiped.get_all() == {"foo": "bar"}
diff --git a/tests/integrations/test_burr_hamilton.py b/tests/integrations/test_burr_hamilton.py
new file mode 100644
index 00000000..1e836ebc
--- /dev/null
+++ b/tests/integrations/test_burr_hamilton.py
@@ -0,0 +1,98 @@
+import pytest
+
+from burr.core import State
+from burr.integrations.hamilton import Hamilton, from_state, from_value, update_state
+from hamilton import ad_hoc_utils, driver
+
+
+def _incrementing_driver():
+ def incremented_count(current_count: int) -> int:
+ return current_count + 1
+
+ def incremented_count_2(current_count: int, increment_by: int = 1) -> int:
+ return current_count + increment_by
+
+ def sum_of_counts(incremented_count: int, incremented_count_2: int) -> int:
+ return incremented_count + incremented_count_2
+
+ mod = ad_hoc_utils.create_temporary_module(
+ incremented_count, incremented_count_2, sum_of_counts
+ )
+ dr = driver.Driver({}, mod)
+ return dr
+
+
+def test_set_driver():
+ dr = _incrementing_driver()
+ Hamilton.set_driver(dr)
+ h = Hamilton({}, {}, driver=dr)
+ assert h.driver == dr
+
+
+def test__extract_inputs_overrides():
+ dr = _incrementing_driver()
+ h = Hamilton(
+ inputs={"current_count": from_state("count"), "incremented_count_2": from_value(2)},
+ outputs={"sum_of_counts": update_state("count")},
+ driver=dr,
+ )
+ inputs, overrides = h._extract_inputs_overrides(State({"count": 0}))
+ assert inputs == {"current_count": 0}
+ assert overrides == {"incremented_count_2": 2}
+
+
+def test__extract_inputs_overrides_missing_inputs():
+ dr = _incrementing_driver()
+ h = Hamilton(
+ inputs={"current_count_not_present": from_state("count")},
+ outputs={"sum_of_counts": update_state("count")},
+ driver=dr,
+ )
+ with pytest.raises(ValueError, match="not available"):
+ inputs, _ = h._extract_inputs_overrides(State({"count": 0}))
+
+
+def test_reads():
+ dr = _incrementing_driver()
+ h = Hamilton(
+ inputs={"current_count": from_state("count"), "incremented_count_2": from_value(2)},
+ outputs={"sum_of_counts": update_state("count")},
+ driver=dr,
+ )
+ assert h.reads == ["count"]
+
+
+def test_writes():
+ dr = _incrementing_driver()
+ h = Hamilton(
+ inputs={"current_count": from_state("count"), "incremented_count_2": from_value(2)},
+ outputs={"sum_of_counts": update_state("count")},
+ driver=dr,
+ )
+ assert h.writes == ["count"]
+
+
+def test_run_step_with_multiple_inputs():
+ dr = _incrementing_driver()
+ h = Hamilton(
+ inputs={"current_count": from_state("count"), "increment_by": from_value(5)},
+ outputs={"sum_of_counts": update_state("count")},
+ driver=dr,
+ )
+ result = h.run(State({"count": 1}))
+ assert result == {"sum_of_counts": 8}
+ new_state = h.update(result, State({"count": 1}))
+ assert new_state.get_all() == {"count": 8}
+
+
+def test_run_step_with_overrides():
+ dr = _incrementing_driver()
+ h = Hamilton(
+ inputs={"current_count": from_state("count"), "incremented_count_2": from_value(2)},
+ outputs={"sum_of_counts": update_state("count")},
+ driver=dr,
+ )
+ result = h.run(State({"count": 1}))
+ assert result == {"sum_of_counts": 4}
+ new_state = h.update(result, State({"count": 1}))
+ assert new_state.get_all() == {"count": 4}
diff --git a/tests/pytest.ini b/tests/pytest.ini
new file mode 100644
index 00000000..40880458
--- /dev/null
+++ b/tests/pytest.ini
@@ -0,0 +1,2 @@
+[pytest]
+asyncio_mode=auto