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
use super::{AdapterError, StoreAdapter};
use anyhow::anyhow;
use async_trait::async_trait;

/// A [StoreAdapter] that store one fixed data record, for testing purpose.
pub struct DumbStoreAdapter<K, R> {
    last_key: Option<K>,
    last_value: Option<R>,
    error: Option<String>,
}

impl<K, R> DumbStoreAdapter<K, R> {
    /// DumbStoreAdapter factory
    pub fn new() -> Self {
        Self {
            last_key: None,
            last_value: None,
            error: None,
        }
    }

    /// DumbStoreAdapter factory that returns error when 'get_record' is called.
    pub fn new_failing_adapter(error: &str) -> Self {
        Self {
            error: Some(error.to_string()),
            ..Self::new()
        }
    }
}

impl<K, R> Default for DumbStoreAdapter<K, R> {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl<K, R> StoreAdapter for DumbStoreAdapter<K, R>
where
    R: Clone + Send + Sync,
    K: PartialEq + Clone + Send + Sync,
{
    type Key = K;
    type Record = R;

    async fn store_record(
        &mut self,
        key: &Self::Key,
        record: &Self::Record,
    ) -> Result<(), AdapterError> {
        let key = key.clone();
        let record = record.clone();

        self.last_key = Some(key);
        self.last_value = Some(record);

        Ok(())
    }

    async fn get_record(&self, key: &Self::Key) -> Result<Option<Self::Record>, AdapterError> {
        match &self.error {
            Some(error) => Err(AdapterError::GeneralError(anyhow!(error.clone()))),
            None => {
                if self.record_exists(key).await? {
                    Ok(self.last_value.as_ref().cloned())
                } else {
                    Ok(None)
                }
            }
        }
    }

    async fn record_exists(&self, key: &Self::Key) -> Result<bool, AdapterError> {
        Ok(self.last_key.is_some() && self.last_key.as_ref().unwrap() == key)
    }

    async fn get_last_n_records(
        &self,
        how_many: usize,
    ) -> Result<Vec<(Self::Key, Self::Record)>, AdapterError> {
        if how_many > 0 {
            match &self.last_key {
                Some(_key) => Ok(vec![(
                    self.last_key.as_ref().cloned().unwrap(),
                    self.last_value.as_ref().cloned().unwrap(),
                )]),
                None => Ok(Vec::new()),
            }
        } else {
            Ok(Vec::new())
        }
    }

    async fn remove(&mut self, key: &Self::Key) -> Result<Option<Self::Record>, AdapterError> {
        if let Some(record) = self.get_record(key).await? {
            self.last_key = None;
            self.last_value = None;

            Ok(Some(record))
        } else {
            Ok(None)
        }
    }

    async fn get_iter(&self) -> Result<Box<dyn Iterator<Item = Self::Record> + '_>, AdapterError> {
        let mut values = vec![];
        if let Some(value) = &self.last_value {
            values.push(value.clone());
        }
        Ok(Box::new(values.into_iter()))
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[tokio::test]
    async fn test_with_no_record_exists() {
        let adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();

        assert!(!adapter.record_exists(&1).await.unwrap());
    }

    #[tokio::test]
    async fn test_with_no_record_get() {
        let adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();

        assert!(adapter.get_record(&1).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_write_record() {
        let mut adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();

        assert!(adapter
            .store_record(&1, &"record".to_string())
            .await
            .is_ok());
        assert_eq!(
            "record".to_owned(),
            adapter.get_record(&1).await.unwrap().unwrap()
        );
    }

    #[tokio::test]
    async fn test_list_with_no_record() {
        let adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();

        assert_eq!(0, adapter.get_last_n_records(10).await.unwrap().len());
    }

    #[tokio::test]
    async fn test_list_with_records() {
        let mut adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();
        adapter
            .store_record(&1, &"record".to_string())
            .await
            .unwrap();
        let list = adapter.get_last_n_records(10).await.unwrap();

        assert_eq!(1, list.len());

        let (key, record) = &list[0];

        assert_eq!(&1, key);
        assert_eq!(&("record".to_owned()), record);
    }

    #[tokio::test]
    async fn test_list_with_last_zero() {
        let mut adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();
        adapter
            .store_record(&1, &"record".to_string())
            .await
            .unwrap();
        let list = adapter.get_last_n_records(0).await.unwrap();

        assert_eq!(0, list.len());
    }

    #[tokio::test]
    async fn test_remove_existing_record() {
        let mut adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();
        adapter
            .store_record(&1, &"record".to_string())
            .await
            .unwrap();
        let value = adapter.remove(&1).await.unwrap().unwrap();

        assert_eq!("record".to_string(), value);
        assert!(!adapter.record_exists(&1).await.unwrap());
    }

    #[tokio::test]
    async fn test_remove_non_existing_record() {
        let mut adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();
        adapter
            .store_record(&1, &"record".to_string())
            .await
            .unwrap();
        let maybe_record = adapter.remove(&0).await.unwrap();

        assert!(maybe_record.is_none());
    }

    #[tokio::test]
    async fn test_iter_record() {
        let mut adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();
        adapter
            .store_record(&1, &"record".to_string())
            .await
            .unwrap();
        let records: Vec<String> = adapter.get_iter().await.unwrap().collect();

        assert_eq!(vec!["record"], records);
    }

    #[tokio::test]
    async fn test_iter_without_record() {
        let adapter: DumbStoreAdapter<u64, String> = DumbStoreAdapter::new();
        let records = adapter.get_iter().await.unwrap();

        assert_eq!(0, records.count());
    }

    #[tokio::test]
    async fn test_return_error_calling_get_record() {
        let adapter: DumbStoreAdapter<String, String> =
            DumbStoreAdapter::new_failing_adapter("error");
        let result = adapter.get_record(&"key".to_string()).await;

        assert!(result.is_err());
    }
}