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

/// A [StoreAdapter] which always fails, for testing purpose.
pub struct FailStoreAdapter<K, R> {
    key: PhantomData<K>,
    certificate: PhantomData<R>,
}

impl<K, R> FailStoreAdapter<K, R> {
    /// FailStoreAdapter factory
    pub fn new() -> Self {
        Self {
            key: PhantomData,
            certificate: PhantomData,
        }
    }
}

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

#[async_trait]
impl<K, R> StoreAdapter for FailStoreAdapter<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> {
        Err(AdapterError::GeneralError(anyhow!(
            "Fail adapter always fails"
        )))
    }

    async fn get_record(&self, _key: &Self::Key) -> Result<Option<Self::Record>, AdapterError> {
        Err(AdapterError::GeneralError(anyhow!(
            "Fail adapter always fails"
        )))
    }

    async fn record_exists(&self, _key: &Self::Key) -> Result<bool, AdapterError> {
        Err(AdapterError::GeneralError(anyhow!(
            "Fail adapter always fails"
        )))
    }

    async fn get_last_n_records(
        &self,
        _how_many: usize,
    ) -> Result<Vec<(Self::Key, Self::Record)>, AdapterError> {
        Err(AdapterError::GeneralError(anyhow!(
            "Fail adapter always fails"
        )))
    }

    async fn remove(&mut self, _key: &Self::Key) -> Result<Option<Self::Record>, AdapterError> {
        Err(AdapterError::GeneralError(anyhow!(
            "Fail adapter always fails"
        )))
    }

    async fn get_iter(&self) -> Result<Box<dyn Iterator<Item = Self::Record> + '_>, AdapterError> {
        Err(AdapterError::GeneralError(anyhow!(
            "Fail adapter always fails"
        )))
    }
}

#[cfg(test)]
mod tests {

    use super::*;

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

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

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

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

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

        assert!(adapter
            .store_record(&1, &"record".to_string())
            .await
            .is_err());
    }

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

        assert!(adapter.get_last_n_records(10).await.is_err());
    }

    #[tokio::test]
    async fn test_list_with_records() {
        let mut adapter: FailStoreAdapter<u64, String> = FailStoreAdapter::new();
        assert!(adapter
            .store_record(&1, &"record".to_string())
            .await
            .is_err());
    }

    #[tokio::test]
    async fn test_list_with_last_zero() {
        let adapter: FailStoreAdapter<u64, String> = FailStoreAdapter::new();
        assert!(adapter.get_last_n_records(0).await.is_err());
    }

    #[tokio::test]
    async fn test_remove_existing_record() {
        let mut adapter: FailStoreAdapter<u64, String> = FailStoreAdapter::new();
        assert!(adapter.remove(&0).await.is_err());
    }

    #[tokio::test]
    async fn test_get_iter() {
        let adapter: FailStoreAdapter<u64, String> = FailStoreAdapter::new();
        assert!(adapter.get_iter().await.is_err());
    }
}