-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsaga_test.rs
120 lines (111 loc) · 3.67 KB
/
saga_test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use fmodel_rust::saga::{ActionComputation, Saga};
use crate::api::{
CreateShipmentCommand, OrderCommand, OrderCreatedEvent, OrderEvent, ShipmentCommand,
UpdateOrderCommand,
};
use crate::application::{sum_to_command, Command, Event};
mod api;
mod application;
fn order_saga<'a>() -> Saga<'a, OrderEvent, ShipmentCommand> {
Saga {
react: Box::new(|event| match event {
OrderEvent::Created(evt) => {
vec![ShipmentCommand::Create(CreateShipmentCommand {
shipment_id: evt.order_id,
order_id: evt.order_id,
customer_name: evt.customer_name.to_owned(),
items: evt.items.to_owned(),
})]
}
OrderEvent::Updated(_) => {
vec![]
}
OrderEvent::Cancelled(_) => {
vec![]
}
}),
}
}
fn order_saga_2<'a>() -> Saga<'a, Event, ShipmentCommand> {
Saga {
react: Box::new(|event| match event {
Event::OrderCreated(evt) => {
vec![ShipmentCommand::Create(CreateShipmentCommand {
shipment_id: evt.order_id,
order_id: evt.order_id,
customer_name: evt.customer_name.to_owned(),
items: evt.items.to_owned(),
})]
}
Event::OrderUpdated(_) => {
vec![]
}
Event::OrderCancelled(_) => {
vec![]
}
Event::ShipmentCreated(_) => {
vec![]
}
}),
}
}
fn shipment_saga_2<'a>() -> Saga<'a, Event, OrderCommand> {
Saga {
react: Box::new(|event| match event {
Event::ShipmentCreated(evt) => {
vec![OrderCommand::Update(UpdateOrderCommand {
order_id: evt.order_id,
new_items: evt.items.to_owned(),
})]
}
Event::OrderCreated(_) => {
vec![]
}
Event::OrderUpdated(_) => {
vec![]
}
Event::OrderCancelled(_) => {
vec![]
}
}),
}
}
#[test]
fn test() {
let order_saga: Saga<OrderEvent, ShipmentCommand> = order_saga();
let order_saga_2: Saga<Event, ShipmentCommand> = crate::order_saga_2();
let shipment_saga_2: Saga<Event, OrderCommand> = crate::shipment_saga_2();
let merged_saga = order_saga_2
.merge(shipment_saga_2)
.map_action(&sum_to_command);
let order_created_event = OrderEvent::Created(OrderCreatedEvent {
order_id: 1,
customer_name: "John Doe".to_string(),
items: vec!["Item 1".to_string(), "Item 2".to_string()],
});
let commands = order_saga.compute_new_actions(&order_created_event);
assert_eq!(
commands,
[ShipmentCommand::Create(CreateShipmentCommand {
shipment_id: 1,
order_id: 1,
customer_name: "John Doe".to_string(),
items: vec!["Item 1".to_string(), "Item 2".to_string()],
})]
);
let order_created_event2 = Event::OrderCreated(OrderCreatedEvent {
order_id: 1,
customer_name: "John Doe".to_string(),
items: vec!["Item 1".to_string(), "Item 2".to_string()],
});
let merged_commands = merged_saga.compute_new_actions(&order_created_event2);
assert_eq!(
merged_commands,
[Command::ShipmentCreate(CreateShipmentCommand {
shipment_id: 1,
order_id: 1,
customer_name: "John Doe".to_string(),
items: vec!["Item 1".to_string(), "Item 2".to_string()],
})]
);
}