Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.
Python aiburrchatbot-frameworkdagsgenerative-aigraphshacktoberfestllmopsllmsmlopspersistent-data-structurestate-machinestate-managementvisibility
Fixes for test case creation
Stefan Krawczyk4f1b8179245a
diff --git a/burr/cli/__main__.py b/burr/cli/__main__.pyindex 085e6ca1..010bcc59 100644--- a/burr/cli/__main__.py+++ b/burr/cli/__main__.py@@ -1,3 +1,4 @@+import importlib.utilimport jsonimport osimport shutil@@ -8,6 +9,7 @@ import timeimport webbrowserfrom contextlib import contextmanagerfrom importlib.resources import files+from types import ModuleTypefrom burr import system, telemetryfrom burr.core.persistence import PersistedStateData@@ -215,8 +217,8 @@ from burr.testing import pytest_generate_tests # noqa: F401@pytest.mark.file_name("{FILE_NAME}")def test_{ACTION_NAME}(input_state, expected_state):\"\"\"Function for testing the action\"\"\"- input_state = state.State(input_state)- expected_state = state.State(expected_state)+ input_state = state.State.deserialize(input_state)+ expected_state = state.State.deserialize(expected_state)_, output_state = {ACTION_NAME}(input_state) # exercise the action# TODO: choose appropriate way to evaluate the output# e.g. exact match, fuzzy match, LLM grade, etc.@@ -228,9 +230,29 @@ def test_{ACTION_NAME}(input_state, expected_state):"""+def import_from_file(file_path: str) -> ModuleType:+ """Import a module from a file path.++ :param file_path: file path. Assume it exists.+ :return:+ """+ # Extract the module name and directory from the file path+ module_name = os.path.basename(file_path).replace(".py", "")+ module_dir = os.path.dirname(file_path)++ # Add the module directory to sys.path if not already included+ if module_dir not in sys.path:+ sys.path.append(module_dir)++ # Import the module+ module = importlib.import_module(module_name)++ return module++@click.command()@click.option("--project-name", required=True, help="Name of the project")[email protected]("--partition-key", required=True, help="Partition key to look at")[email protected]("--partition-key", required=False, help="Partition key to look at")@click.option("--app-id", required=True, help="App ID to pull from.")@click.option("--sequence-id", required=True, help="Sequence ID to pull.")@click.option(@@ -238,12 +260,18 @@ def test_{ACTION_NAME}(input_state, expected_state):required=False,help="What file to write the data to. Else print to console.",)+ "--serde-module",+ required=False,+ help="Python file or fully qualified python module to import for custom serialization/deserialization.",+)def create_test_case(project_name: str,- partition_key: str,+ partition_key: str, # will be None if not passed in.app_id: str,sequence_id: str,target_file_name: str = None,+ serde_module: str = None,):"""Create a test case from a persisted state.@@ -254,7 +282,21 @@ def create_test_case(See examples/test-case-creation/notebook.ipynb for example usage.See examples/test-case-creation/test_application.py for details.++ If you have custom serialization/deserialization then pass in the name of a+ python module, or a path to a python file, to import with your custom serialization/deserialization+ registration functions. This will be imported so that they can be registered."""+ if serde_module:+ if os.path.exists(serde_module):+ print(f"Importing from file {serde_module}")+ import_from_file(serde_module)+ else:+ print(f"Importing module {serde_module}")+ import importlib++ importlib.import_module(serde_module)+# TODO: make this handle instantiating/using a persister other than local trackerfrom burr.tracking.client import LocalTrackingClient@@ -265,7 +307,7 @@ def create_test_case(if not data:print(f"No data found for {app_id} in {project_name} with sequence {sequence_id}")return- state_dict = data["state"].get_all()+ state_dict = data["state"].serialize()print("Found data for action: ", data["position"])# test case jsontc_json = _transform_state_to_test_case(diff --git a/examples/custom-serde/application.py b/examples/custom-serde/application.pyindex eed9f096..ec0511d8 100644--- a/examples/custom-serde/application.py+++ b/examples/custom-serde/application.py@@ -132,6 +132,13 @@ def build_application(partition_key, app_id):if __name__ == "__main__":+ """+ Note: if you try to use the test case creation commandline from the result+ of running this application using the code below it will+ fail because the above SERDE classes were registered/run as the `__main__`+ module. You can either move the SERDE classes to a separate module so+ that you could import them directly, or run the code below, but in another file (e.g. run.py).+ """import pprintimport uuiddiff --git a/examples/custom-serde/run.py b/examples/custom-serde/run.pynew file mode 100644index 00000000..8eb24c35--- /dev/null+++ b/examples/custom-serde/run.py@@ -0,0 +1,34 @@+"""+Example of running the application+from another module to make sure the+SERDE classes are registered in a non __main__+module namespace.++e.g. python run.py+and then+burr-test-case create --project-name serde-example --app-id APP_ID --sequence-id 3 --serde-module application.py+"""+import pprint+import uuid++import application # noqa+from application import build_application++from burr.core import State++# build+app = build_application("client-123", str(uuid.uuid4()))+app.visualize(+ output_file_path="statemachine", include_conditions=True, include_state=True, format="png"+)+# run+action, result, state = app.run(+ halt_after=["terminal_action"], inputs={"user_input": "hello world"}+)+# serialize+serialized_state = state.serialize()+pprint.pprint(serialized_state)+# deserialize+deserialized_state = State.deserialize(serialized_state)+# assert that the state is the same after serialization and deserialization+assert state.get_all() == deserialized_state.get_all()diff --git a/examples/test-case-creation/test_application.py b/examples/test-case-creation/test_application.pyindex d9a3b6b7..d29f9089 100644--- a/examples/test-case-creation/test_application.py+++ b/examples/test-case-creation/test_application.py@@ -53,8 +53,8 @@ def test_prompt_for_more():@pytest.mark.file_name("prompt_for_more.json")def test_prompt_for_more_from_file(input_state, expected_state):"""Function for testing the action"""- input_state = state.State(input_state)- expected_state = state.State(expected_state)+ input_state = state.State.deserialize(input_state)+ expected_state = state.State.deserialize(expected_state)_, output_state = prompt_for_more(input_state) # exercising the action# TODO: choose appropriate way to evaluate the output# e.g. exact match, fuzzy match, LLM grade, etc.