Skip to content

Commit

Permalink
Add test for class based eager flow without yaml (#2985)
Browse files Browse the repository at this point in the history
# Description

Please add an informative description that covers that changes made by
the pull request and link all relevant issues.

# All Promptflow Contribution checklist:
- [ ] **The pull request does not introduce [breaking changes].**
- [ ] **CHANGELOG is updated for new features, bug fixes or other
significant changes.**
- [ ] **I have read the [contribution guidelines](../CONTRIBUTING.md).**
- [ ] **Create an issue and link to the pull request to get dedicated
review from promptflow team. Learn more: [suggested
workflow](../CONTRIBUTING.md#suggested-workflow).**

## General Guidelines and Best Practices
- [ ] Title of the pull request is clear and informative.
- [ ] There are a small number of commits, each of which have an
informative message. This means that previously merged commits do not
appear in the history of the PR. For more information on cleaning up the
commits in your PR, [see this
page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md).

### Testing Guidelines
- [ ] Pull request includes test coverage for the included changes.

---------

Co-authored-by: Ying Chen <[email protected]>
  • Loading branch information
YingChen1996 and Ying Chen authored Apr 25, 2024
1 parent ec19c30 commit 0a7cde3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/promptflow-devkit/tests/sdk_cli_test/e2etests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2686,6 +2686,37 @@ def test_eager_flow_test_without_yaml(self, pf, capsys):
assert "Hello world" in stdout
assert "val1" in stdout

def test_class_based_eager_flow_test_without_yaml(self, pf, capsys):
run_pf_command(
"flow",
"test",
"--flow",
"simple_callable_class:MyFlow",
"--inputs",
"func_input=input",
"--init",
"obj_input=val",
cwd=f"{EAGER_FLOWS_DIR}/basic_callable_class_without_yaml",
)
stdout, _ = capsys.readouterr()
assert "obj_input" in stdout
assert "func_input" in stdout

run_pf_command(
"flow",
"test",
"--flow",
"simple_callable_class:MyFlow",
"--inputs",
f"{EAGER_FLOWS_DIR}/basic_callable_class_without_yaml/inputs.jsonl",
"--init",
f"{EAGER_FLOWS_DIR}/basic_callable_class_without_yaml/init.json",
cwd=f"{EAGER_FLOWS_DIR}/basic_callable_class_without_yaml",
)
stdout, _ = capsys.readouterr()
assert "obj_input" in stdout
assert "func_input" in stdout

def test_eager_flow_test_without_yaml_ui(self, pf, capsys):
run_pf_command(
"flow",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,20 @@ def test_pf_test_flow_in_notebook(self):
cwd=notebook_path.parent,
)

def test_eager_flow_test(self):
def test_eager_flow_test_without_yaml(self):
flow_path = Path(f"{EAGER_FLOWS_DIR}/simple_without_yaml_return_output/").absolute()
with _change_working_dir(flow_path):
result = _client._flows.test(flow="entry:my_flow", inputs={"input_val": "val1"})
assert result == "Hello world! val1"

def test_class_based_eager_flow_test_without_yaml(self):
flow_path = Path(f"{EAGER_FLOWS_DIR}/basic_callable_class_without_yaml/").absolute()
with _change_working_dir(flow_path):
result = _client._flows.test(
flow="simple_callable_class:MyFlow", inputs={"func_input": "input"}, init={"obj_input": "val"}
)
assert result["func_input"] == "input"

def test_eager_flow_test_with_yaml(self):
clear_module_cache("entry")
flow_path = Path(f"{EAGER_FLOWS_DIR}/simple_with_yaml/").absolute()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"obj_input": "val"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"func_input": "func_input"}
{"func_input": "func_input"}
{"func_input": "func_input"}
{"func_input": "func_input"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
from typing import TypedDict

from promptflow.tracing import trace

class FlowOutput(TypedDict):
obj_input: str
func_input: str
obj_id: str


class MyFlow:
def __init__(self, obj_input: str):
self.obj_input = obj_input

@trace
def __call__(self, func_input: str) -> FlowOutput:
return {
"obj_input": self.obj_input,
"func_input": func_input,
"obj_id": id(self),
}

def __aggregate__(self, results: list) -> dict:
return {"length": len(results)}


if __name__ == "__main__":
flow = MyFlow("obj_input")
result = flow("func_input")
print(result)

0 comments on commit 0a7cde3

Please sign in to comment.