1use crate::CamelError;
8use crate::body::Body;
9
10#[async_trait::async_trait]
27pub trait ClaimCheckRepository: Send + Sync + std::fmt::Debug + 'static {
28 fn name(&self) -> &str;
30
31 async fn set(&self, key: &str, payload: Body) -> Result<(), CamelError>;
33
34 async fn get(&self, key: &str) -> Result<Body, CamelError>;
38
39 async fn get_and_remove(&self, key: &str) -> Result<Body, CamelError>;
43
44 async fn remove(&self, key: &str) -> Result<(), CamelError>;
46
47 async fn push(&self, key: &str, payload: Body) -> Result<(), CamelError>;
49
50 async fn pop(&self, key: &str) -> Result<Body, CamelError>;
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59 use std::sync::Arc;
60
61 #[derive(Debug)]
63 struct StubRepo;
64
65 #[async_trait::async_trait]
66 impl ClaimCheckRepository for StubRepo {
67 fn name(&self) -> &str {
68 "stub"
69 }
70
71 async fn set(&self, _key: &str, _payload: Body) -> Result<(), CamelError> {
72 Ok(())
73 }
74
75 async fn get(&self, _key: &str) -> Result<Body, CamelError> {
76 Ok(Body::Empty)
77 }
78
79 async fn get_and_remove(&self, _key: &str) -> Result<Body, CamelError> {
80 Ok(Body::Empty)
81 }
82
83 async fn remove(&self, _key: &str) -> Result<(), CamelError> {
84 Ok(())
85 }
86
87 async fn push(&self, _key: &str, _payload: Body) -> Result<(), CamelError> {
88 Ok(())
89 }
90
91 async fn pop(&self, _key: &str) -> Result<Body, CamelError> {
92 Ok(Body::Empty)
93 }
94 }
95
96 #[test]
98 fn stub_is_object_safe() {
99 let repo: Arc<dyn ClaimCheckRepository> = Arc::new(StubRepo);
100 assert_eq!(repo.name(), "stub");
101 }
102
103 #[tokio::test]
104 async fn stub_set_and_get() {
105 let repo = StubRepo;
106 repo.set("k", Body::Text("v".into())).await.unwrap();
107 let body = repo.get("k").await.unwrap();
108 assert!(body.is_empty());
109 }
110
111 #[tokio::test]
112 async fn stub_get_and_remove() {
113 let repo = StubRepo;
114 let body = repo.get_and_remove("k").await.unwrap();
115 assert!(body.is_empty());
116 }
117
118 #[tokio::test]
119 async fn stub_remove_is_ok() {
120 let repo = StubRepo;
121 repo.remove("k").await.unwrap();
122 }
123
124 #[tokio::test]
125 async fn stub_push_pop() {
126 let repo = StubRepo;
127 repo.push("k", Body::Text("v".into())).await.unwrap();
128 let body = repo.pop("k").await.unwrap();
129 assert!(body.is_empty());
130 }
131}