forked from aws-samples/serverless-rust-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.rs
269 lines (223 loc) · 7.27 KB
/
memory.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//! # In-memory store implementation
//!
//! This is a simple in-memory store implementation. It is not intended to be
//! used in production, but rather as a simple implementation for local
//! testing purposes.
use super::{Store, StoreDelete, StoreGet, StoreGetAll, StorePut};
use crate::{Error, Product, ProductRange};
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::RwLock;
#[derive(Default)]
pub struct MemoryStore {
data: RwLock<HashMap<String, Product>>,
}
impl MemoryStore {
pub fn new() -> Self {
Default::default()
}
}
impl Store for MemoryStore {}
#[async_trait]
impl StoreGetAll for MemoryStore {
async fn all(&self, _: Option<&str>) -> Result<ProductRange, Error> {
Ok(ProductRange {
products: self
.data
.read()
.unwrap()
.iter()
.map(|(_, v)| v.clone())
.collect(),
next: None,
})
}
}
#[async_trait]
impl StoreGet for MemoryStore {
async fn get(&self, id: &str) -> Result<Option<Product>, Error> {
Ok(self.data.read().unwrap().get(id).cloned())
}
}
#[async_trait]
impl StorePut for MemoryStore {
async fn put(&self, product: &Product) -> Result<(), Error> {
self.data
.write()
.unwrap()
.insert(product.id.clone(), product.clone());
Ok(())
}
}
#[async_trait]
impl StoreDelete for MemoryStore {
async fn delete(&self, id: &str) -> Result<(), Error> {
self.data.write().unwrap().remove(id);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Error;
struct ConstProduct<'a> {
id: &'a str,
name: &'a str,
price: f64,
}
impl Into<Product> for ConstProduct<'_> {
fn into(self) -> Product {
Product {
id: self.id.to_string(),
name: self.name.to_string(),
price: self.price,
}
}
}
const PRODUCT_0: ConstProduct = ConstProduct {
id: "1",
name: "foo",
price: 10.0,
};
const PRODUCT_1: ConstProduct = ConstProduct {
id: "2",
name: "foo",
price: 10.0,
};
#[tokio::test]
async fn test_new() -> Result<(), Error> {
// GIVEN an empty store
let store = MemoryStore::new();
// WHEN we get the length of all products
// THEN we get 0
assert_eq!(store.data.read().unwrap().len(), 0);
Ok(())
}
#[tokio::test]
async fn test_all_empty() -> Result<(), Error> {
// GIVEN an empty store
let store = MemoryStore::new();
// WHEN we get all products
let all = store.all(None).await?;
// THEN we get an empty list
assert_eq!(all.products.len(), 0);
Ok(())
}
#[tokio::test]
async fn test_all1() -> Result<(), Error> {
// GIVEN a store with one product
let product0: Product = PRODUCT_0.into();
let store = MemoryStore::new();
{
let mut data = store.data.write().unwrap();
data.insert(product0.id.clone(), product0.clone());
}
// WHEN we get all products
let all = store.all(None).await?;
// THEN we get the product
assert_eq!(all.products.len(), 1);
assert_eq!(all.products[0], product0);
Ok(())
}
#[tokio::test]
async fn test_all2() -> Result<(), Error> {
// GIVEN a store with two products
let product0: Product = PRODUCT_0.into();
let product1: Product = PRODUCT_1.into();
let store = MemoryStore::new();
{
let mut data = store.data.write().unwrap();
data.insert(product0.id.clone(), product0.clone());
data.insert(product1.id.clone(), product1.clone());
}
// WHEN we get all products
let all = store.all(None).await?;
// THEN we get the products
assert_eq!(all.products.len(), 2);
assert!(all.products.contains(&product0));
assert!(all.products.contains(&product1));
Ok(())
}
#[tokio::test]
async fn test_delete() -> Result<(), Error> {
// GIVEN a store with a product
let product0: Product = PRODUCT_0.into();
let store = MemoryStore::new();
{
let mut data = store.data.write().unwrap();
data.insert(product0.id.clone(), product0.clone());
}
// WHEN deleting the product
store.delete(&product0.id).await?;
// THEN the length of the store is 0
assert_eq!(store.data.read().unwrap().len(), 0);
// AND the product is not returned
assert_eq!(store.get(&product0.id).await?, None);
Ok(())
}
#[tokio::test]
async fn test_delete2() -> Result<(), Error> {
// GIVEN a store with two products
let product0: Product = PRODUCT_0.into();
let product1: Product = PRODUCT_1.into();
let store = MemoryStore::new();
{
let mut data = store.data.write().unwrap();
data.insert(product0.id.clone(), product0.clone());
data.insert(product1.id.clone(), product1.clone());
}
// WHEN deleting the first product
store.delete(&product0.id).await?;
// THEN the length of the store is 1
assert_eq!(store.data.read().unwrap().len(), 1);
// AND the product is not returned
assert_eq!(store.get(&product0.id).await?, None);
// AND the second product is returned
assert_eq!(store.get(&product1.id).await?, Some(product1));
Ok(())
}
#[tokio::test]
async fn test_get() -> Result<(), Error> {
// GIVEN a store with a product
let product0: Product = PRODUCT_0.into();
let store = MemoryStore::new();
{
let mut data = store.data.write().unwrap();
data.insert(product0.id.clone(), product0.clone());
}
// WHEN getting the product
let product = store.get(&product0.id).await?;
// THEN the product is returned
assert_eq!(product, Some(product0));
Ok(())
}
#[tokio::test]
async fn test_put() -> Result<(), Error> {
// GIVEN an empty store and a product
let store = MemoryStore::new();
let product0: Product = PRODUCT_0.into();
// WHEN inserting a product
store.put(&product0).await?;
// THEN the length of the store is 1
assert_eq!(store.data.read().unwrap().len(), 1);
// AND the product is returned
assert_eq!(store.get(&product0.id).await?, Some(product0));
Ok(())
}
#[tokio::test]
async fn test_put2() -> Result<(), Error> {
// GIVEN an empty store and two products
let store = MemoryStore::new();
let product0: Product = PRODUCT_0.into();
let product1: Product = PRODUCT_1.into();
// WHEN inserting two products
store.put(&product0).await?;
store.put(&product1).await?;
// THEN the length of the store is 2
assert_eq!(store.data.read().unwrap().len(), 2);
// AND the products are returned
assert_eq!(store.get(&product0.id).await?, Some(product0));
assert_eq!(store.get(&product1.id).await?, Some(product1));
Ok(())
}
}