-
Notifications
You must be signed in to change notification settings - Fork 2
/
view_test.rs
176 lines (165 loc) · 5.64 KB
/
view_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use fmodel_rust::view::{View, ViewStateComputation};
use crate::api::{
OrderCancelledEvent, OrderCreatedEvent, OrderEvent, OrderUpdatedEvent, OrderViewState,
ShipmentCreatedEvent, ShipmentEvent, ShipmentViewState,
};
use crate::application::{event_from_sum, Event};
mod api;
mod application;
fn order_view<'a>() -> View<'a, OrderViewState, OrderEvent> {
View {
evolve: Box::new(|state, event| {
let mut new_state = state.clone();
match event {
OrderEvent::Created(evt) => {
new_state.order_id = evt.order_id;
new_state.customer_name = evt.customer_name.to_owned();
new_state.items = evt.items.to_owned();
}
OrderEvent::Updated(evt) => {
new_state.items = evt.updated_items.to_owned();
}
OrderEvent::Cancelled(_) => {
new_state.is_cancelled = true;
}
}
new_state
}),
initial_state: Box::new(|| OrderViewState {
order_id: 0,
customer_name: "".to_string(),
items: Vec::new(),
is_cancelled: false,
}),
}
}
fn shipment_view<'a>() -> View<'a, ShipmentViewState, ShipmentEvent> {
View {
evolve: Box::new(|state, event| {
let mut new_state = state.clone();
match event {
ShipmentEvent::Created(evt) => {
new_state.shipment_id = evt.shipment_id;
new_state.order_id = evt.order_id;
new_state.customer_name = evt.customer_name.to_owned();
new_state.items = evt.items.to_owned();
}
}
new_state
}),
initial_state: Box::new(|| ShipmentViewState {
shipment_id: 0,
order_id: 0,
customer_name: "".to_string(),
items: Vec::new(),
}),
}
}
#[test]
fn test() {
let order_view: View<OrderViewState, OrderEvent> = order_view();
let order_view2: View<OrderViewState, OrderEvent> = crate::order_view();
let shipment_view: View<ShipmentViewState, ShipmentEvent> = shipment_view();
let combined_view = order_view2
.combine(shipment_view)
.map_event(&event_from_sum);
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 new_state = order_view.compute_new_state(None, &[&order_created_event]);
assert_eq!(
new_state,
OrderViewState {
order_id: 1,
customer_name: "John Doe".to_string(),
items: vec!["Item 1".to_string(), "Item 2".to_string()],
is_cancelled: false,
}
);
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 new_combined_state2 = combined_view.compute_new_state(None, &[&order_created_event2]);
assert_eq!(
new_combined_state2,
(
OrderViewState {
order_id: 1,
customer_name: "John Doe".to_string(),
items: vec!["Item 1".to_string(), "Item 2".to_string()],
is_cancelled: false,
},
ShipmentViewState {
shipment_id: 0,
order_id: 0,
customer_name: "".to_string(),
items: Vec::new(),
}
)
);
let shipment_created_event2 = Event::ShipmentCreated(ShipmentCreatedEvent {
shipment_id: 1,
order_id: 1,
customer_name: "John Doe".to_string(),
items: vec!["Item 1".to_string(), "Item 2".to_string()],
});
let new_combined_state3 = combined_view.compute_new_state(None, &[&shipment_created_event2]);
assert_eq!(
new_combined_state3,
(
OrderViewState {
order_id: 0,
customer_name: "".to_string(),
items: Vec::new(),
is_cancelled: false,
},
ShipmentViewState {
shipment_id: 1,
order_id: 1,
customer_name: "John Doe".to_string(),
items: vec!["Item 1".to_string(), "Item 2".to_string()],
}
)
);
let order_updated_event = OrderEvent::Updated(OrderUpdatedEvent {
order_id: 1,
updated_items: vec![
"Item 11".to_string(),
"Item 22".to_string(),
"Item 33".to_string(),
],
});
let new_state = order_view.compute_new_state(Some(new_state), &[&order_updated_event]);
assert_eq!(
new_state,
OrderViewState {
order_id: 1,
customer_name: "John Doe".to_string(),
items: vec![
"Item 11".to_string(),
"Item 22".to_string(),
"Item 33".to_string()
],
is_cancelled: false,
}
);
let order_canceled_event = OrderEvent::Cancelled(OrderCancelledEvent { order_id: 1 });
let new_state = order_view.compute_new_state(Some(new_state), &[&order_canceled_event]);
assert_eq!(
new_state,
OrderViewState {
order_id: 1,
customer_name: "John Doe".to_string(),
items: vec![
"Item 11".to_string(),
"Item 22".to_string(),
"Item 33".to_string()
],
is_cancelled: true,
}
);
}