Skip to content

Commit

Permalink
Add tests for flow_model types.
Browse files Browse the repository at this point in the history
  • Loading branch information
azriel91 committed Mar 10, 2024
1 parent 70f6368 commit f80cc25
Show file tree
Hide file tree
Showing 13 changed files with 445 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ dot_ix = "0.2.0"
dyn-clone = "1.0.17"
enser = "0.1.4"
erased-serde = "0.4.3"
fn_graph = { version = "0.13.0", features = ["async", "graph_info", "interruptible", "resman"] }
fn_graph = { version = "0.13.2", features = ["async", "graph_info", "interruptible", "resman"] }
futures = "0.3.30"
heck = "0.4.1"
indexmap = "2.2.5"
Expand Down
10 changes: 10 additions & 0 deletions crate/flow_model/src/flow_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ pub struct FlowInfo {
/// Serialized representation of the flow graph.
pub graph_info: GraphInfo<ItemInfo>,
}

impl FlowInfo {
/// Returns a new `FlowInfo`.
pub fn new(flow_id: FlowId, graph_info: GraphInfo<ItemInfo>) -> Self {
Self {
flow_id,
graph_info,
}
}
}
8 changes: 8 additions & 0 deletions crate/flow_model/src/flow_spec_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ pub struct FlowSpecInfo {
}

impl FlowSpecInfo {
/// Returns a new `FlowSpecInfo`.
pub fn new(flow_id: FlowId, graph_info: GraphInfo<ItemSpecInfo>) -> Self {
Self {
flow_id,
graph_info,
}
}

/// Returns an [`InfoGraph`] that represents the progress of the flow's
/// execution.
pub fn into_progress_info_graph(&self) -> InfoGraph {
Expand Down
7 changes: 7 additions & 0 deletions crate/flow_model/src/item_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ pub struct ItemInfo {
/// ID of the `Item`.
pub item_id: ItemId,
}

impl ItemInfo {
/// Returns a new `ItemInfo`.
pub fn new(item_id: ItemId) -> Self {
Self { item_id }
}
}
7 changes: 7 additions & 0 deletions crate/flow_model/src/item_spec_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ pub struct ItemSpecInfo {
/// ID of the `Item`.
pub item_id: ItemId,
}

impl ItemSpecInfo {
/// Returns a new `ItemSpecInfo`.
pub fn new(item_id: ItemId) -> Self {
Self { item_id }
}
}
3 changes: 3 additions & 0 deletions crate/flow_model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//! This includes the serializable representation of a `Flow`. Since an actual
//! `Flow` contains logic, it currently resides in `peace_rt_model`.

// Re-exports;
pub use fn_graph::GraphInfo;

pub use crate::{
flow_info::FlowInfo, flow_spec_info::FlowSpecInfo, item_info::ItemInfo,
item_spec_info::ItemSpecInfo,
Expand Down
5 changes: 1 addition & 4 deletions crate/rt_model/src/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ impl<E> Flow<E> {
ItemSpecInfo { item_id }
});

FlowSpecInfo {
flow_id,
graph_info,
}
FlowSpecInfo::new(flow_id, graph_info)
}
}
4 changes: 4 additions & 0 deletions workspace_tests/src/flow_model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod flow_info;
mod flow_spec_info;
mod item_info;
mod item_spec_info;
178 changes: 178 additions & 0 deletions workspace_tests/src/flow_model/flow_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
use peace::{
cfg::{flow_id, item_id},
data::fn_graph::{daggy::Dag, Edge, WouldCycle},
flow_model::{FlowInfo, FlowSpecInfo, GraphInfo, ItemInfo, ItemSpecInfo},
rt_model::{Flow, ItemGraph, ItemGraphBuilder},
};
use peace_items::blank::BlankItem;

use crate::PeaceTestError;

#[test]
fn clone() -> Result<(), Box<dyn std::error::Error>> {
let flow_info = flow_info()?;

assert_eq!(flow_info, Clone::clone(&flow_info));
Ok(())
}

#[test]
fn debug() -> Result<(), Box<dyn std::error::Error>> {
let flow_info = flow_info()?;

assert_eq!(
"FlowInfo { \
flow_id: FlowId(\"flow_id\"), \
graph_info: GraphInfo { \
graph: Dag { graph: Graph { Ty: \"Directed\", node_count: 6, edge_count: 9, edges: (0, 1), (0, 2), (1, 4), (2, 3), (3, 4), (5, 4), (1, 2), (5, 1), (0, 5), node weights: {0: ItemInfo { item_id: ItemId(\"a\") }, 1: ItemInfo { item_id: ItemId(\"b\") }, 2: ItemInfo { item_id: ItemId(\"c\") }, 3: ItemInfo { item_id: ItemId(\"d\") }, 4: ItemInfo { item_id: ItemId(\"e\") }, 5: ItemInfo { item_id: ItemId(\"f\") }}, edge weights: {0: Logic, 1: Logic, 2: Logic, 3: Logic, 4: Logic, 5: Logic, 6: Data, 7: Data, 8: Data} }, cycle_state: DfsSpace { dfs: Dfs { stack: [], discovered: FixedBitSet { data: [], length: 0 } } } } \
} \
}",
format!("{flow_info:?}")
);
Ok(())
}

#[test]
fn serialize() -> Result<(), Box<dyn std::error::Error>> {
let flow_info = flow_info()?;

assert_eq!(
r#"flow_id: flow_id
graph_info:
graph:
nodes:
- item_id: a
- item_id: b
- item_id: c
- item_id: d
- item_id: e
- item_id: f
node_holes: []
edge_property: directed
edges:
- - 0
- 1
- Logic
- - 0
- 2
- Logic
- - 1
- 4
- Logic
- - 2
- 3
- Logic
- - 3
- 4
- Logic
- - 5
- 4
- Logic
- - 1
- 2
- Data
- - 5
- 1
- Data
- - 0
- 5
- Data
"#,
serde_yaml::to_string(&flow_info)?
);
Ok(())
}

#[test]
fn deserialize() -> Result<(), Box<dyn std::error::Error>> {
let flow_info = flow_info()?;

assert_eq!(
flow_info,
serde_yaml::from_str(
r#"flow_id: flow_id
graph_info:
graph:
nodes:
- item_id: a
- item_id: b
- item_id: c
- item_id: d
- item_id: e
- item_id: f
node_holes: []
edge_property: directed
edges:
- [0, 1, Logic]
- [0, 2, Logic]
- [1, 4, Logic]
- [2, 3, Logic]
- [3, 4, Logic]
- [5, 4, Logic]
- [1, 2, Data]
- [5, 1, Data]
- [0, 5, Data]
"#
)?
);
Ok(())
}

fn flow_info() -> Result<FlowInfo, WouldCycle<Edge>> {
let flow = Flow::new(flow_id!("flow_id"), complex_graph()?);
let FlowSpecInfo {
flow_id,
graph_info,
} = flow.flow_spec_info();

let mut graph = graph_info.iter_insertion_with_indices().fold(
Dag::new(),
|mut graph, (_, item_spec_info)| {
let ItemSpecInfo { item_id } = item_spec_info;
let item_info = ItemInfo::new(item_id.clone());
graph.add_node(item_info);
graph
},
);

let edges = graph_info
.raw_edges()
.iter()
.map(|e| (e.source(), e.target(), e.weight));

graph.add_edges(edges).expect(
"Edges are all directed from the original graph, \
so this cannot cause a cycle.",
);

let graph_info = GraphInfo::new(graph);
let flow_info = FlowInfo::new(flow_id, graph_info);
Ok(flow_info)
}

fn complex_graph() -> Result<ItemGraph<PeaceTestError>, WouldCycle<Edge>> {
// a - b --------- e
// \ / /
// '-- c - d /
// /
// f --------'
let mut item_graph_builder = ItemGraphBuilder::new();
let [fn_id_a, fn_id_b, fn_id_c, fn_id_d, fn_id_e, fn_id_f] = item_graph_builder.add_fns([
BlankItem::<()>::new(item_id!("a")).into(),
BlankItem::<()>::new(item_id!("b")).into(),
BlankItem::<()>::new(item_id!("c")).into(),
BlankItem::<()>::new(item_id!("d")).into(),
BlankItem::<()>::new(item_id!("e")).into(),
BlankItem::<()>::new(item_id!("f")).into(),
]);
item_graph_builder.add_logic_edges([
(fn_id_a, fn_id_b),
(fn_id_a, fn_id_c),
(fn_id_b, fn_id_e),
(fn_id_c, fn_id_d),
(fn_id_d, fn_id_e),
(fn_id_f, fn_id_e),
])?;
let item_graph = item_graph_builder.build();
Ok(item_graph)
}
Loading

0 comments on commit f80cc25

Please sign in to comment.