Skip to content

Commit 169c50b

Browse files
committed
feat(rs): add utility functions for testing flow functions
1 parent 2c6a30e commit 169c50b

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/ops/functions/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ pub mod embed_text;
22
pub mod extract_by_llm;
33
pub mod parse_json;
44
pub mod split_recursively;
5+
6+
#[cfg(test)]
7+
mod test_utils;

src/ops/functions/test_utils.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use crate::builder::plan::AnalyzedValueMapping;
2+
use crate::ops::sdk::{
3+
EnrichedValueType, FlowInstanceContext, OpArgSchema, OpArgsResolver, SimpleFunctionExecutor,
4+
SimpleFunctionFactoryBase, Value,
5+
};
6+
use anyhow::Result;
7+
use serde::de::DeserializeOwned;
8+
use serde_json::Value as JsonValue;
9+
use std::sync::Arc;
10+
11+
// This function provides a helper to create OpArgSchema for literal values.
12+
pub fn new_literal_op_arg_schema(
13+
name: Option<&str>,
14+
value: Value,
15+
value_type: EnrichedValueType,
16+
) -> OpArgSchema {
17+
OpArgSchema {
18+
name: name.map_or(crate::base::spec::OpArgName(None), |n| {
19+
crate::base::spec::OpArgName(Some(n.to_string()))
20+
}),
21+
value_type,
22+
analyzed_value: AnalyzedValueMapping::Constant { value },
23+
}
24+
}
25+
26+
// This function tests a flow function by providing a spec, input argument schemas, and values.
27+
pub async fn test_flow_function<S, R, F>(
28+
factory: Arc<F>,
29+
spec_json: JsonValue,
30+
input_arg_schemas: Vec<OpArgSchema>,
31+
input_arg_values: Vec<Value>,
32+
context: Arc<FlowInstanceContext>,
33+
) -> Result<Value>
34+
where
35+
S: DeserializeOwned + Send + Sync + 'static,
36+
R: Send + Sync + 'static,
37+
F: SimpleFunctionFactoryBase<Spec = S, ResolvedArgs = R> + ?Sized,
38+
{
39+
// 1. Deserialize Spec
40+
let spec: S = serde_json::from_value(spec_json)?;
41+
42+
// 2. Resolve Schema & Args
43+
// The caller of test_flow_function will be responsible for creating these schemas.
44+
let mut args_resolver = OpArgsResolver::new(&input_arg_schemas)?;
45+
46+
let (resolved_args_from_schema, _output_schema): (R, EnrichedValueType) = factory
47+
.resolve_schema(&spec, &mut args_resolver, &context)
48+
.await?;
49+
50+
args_resolver.done()?;
51+
52+
// 3. Build Executor
53+
let executor: Box<dyn SimpleFunctionExecutor> = factory
54+
.build_executor(spec, resolved_args_from_schema, Arc::clone(&context))
55+
.await?;
56+
57+
// 4. Evaluate
58+
let result = executor.evaluate(input_arg_values).await?;
59+
60+
Ok(result)
61+
}

0 commit comments

Comments
 (0)