autopush_common/db/
mock.rs

1// mockall::mock currently generates these warnings
2#![allow(clippy::unused_unit)]
3#![allow(clippy::ptr_arg)]
4
5use crate::db::client::DbClient;
6pub use crate::db::client::MockDbClient;
7use crate::db::error::DbResult;
8use crate::db::User;
9use crate::notification::Notification;
10use async_trait::async_trait;
11use std::collections::HashSet;
12use std::sync::Arc;
13use uuid::Uuid;
14
15use super::client::FetchMessageResponse;
16
17#[async_trait]
18impl DbClient for Arc<MockDbClient> {
19    async fn add_user(&self, user: &User) -> DbResult<()> {
20        Arc::as_ref(self).add_user(user).await
21    }
22
23    async fn update_user(&self, user: &mut User) -> DbResult<bool> {
24        Arc::as_ref(self).update_user(user).await
25    }
26
27    async fn get_user(&self, uaid: &Uuid) -> DbResult<Option<User>> {
28        Arc::as_ref(self).get_user(uaid).await
29    }
30
31    async fn remove_user(&self, uaid: &Uuid) -> DbResult<()> {
32        Arc::as_ref(self).remove_user(uaid).await
33    }
34
35    async fn add_channel(&self, uaid: &Uuid, channel_id: &Uuid) -> DbResult<()> {
36        Arc::as_ref(self).add_channel(uaid, channel_id).await
37    }
38
39    async fn add_channels(&self, uaid: &Uuid, channels: HashSet<Uuid>) -> DbResult<()> {
40        Arc::as_ref(self).add_channels(uaid, channels).await
41    }
42
43    async fn get_channels(&self, uaid: &Uuid) -> DbResult<HashSet<Uuid>> {
44        Arc::as_ref(self).get_channels(uaid).await
45    }
46
47    async fn remove_channel(&self, uaid: &Uuid, channel_id: &Uuid) -> DbResult<bool> {
48        Arc::as_ref(self).remove_channel(uaid, channel_id).await
49    }
50
51    async fn remove_node_id(
52        &self,
53        uaid: &Uuid,
54        node_id: &str,
55        connected_at: u64,
56        version: &Option<Uuid>,
57    ) -> DbResult<bool> {
58        Arc::as_ref(self)
59            .remove_node_id(uaid, node_id, connected_at, version)
60            .await
61    }
62
63    async fn save_message(&self, uaid: &Uuid, message: Notification) -> DbResult<()> {
64        Arc::as_ref(self).save_message(uaid, message).await
65    }
66
67    async fn save_messages(&self, uaid: &Uuid, messages: Vec<Notification>) -> DbResult<()> {
68        Arc::as_ref(self).save_messages(uaid, messages).await
69    }
70
71    async fn fetch_topic_messages(
72        &self,
73        uaid: &Uuid,
74        limit: usize,
75    ) -> DbResult<FetchMessageResponse> {
76        Arc::as_ref(self).fetch_topic_messages(uaid, limit).await
77    }
78
79    async fn fetch_timestamp_messages(
80        &self,
81        uaid: &Uuid,
82        timestamp: Option<u64>,
83        limit: usize,
84    ) -> DbResult<FetchMessageResponse> {
85        Arc::as_ref(self)
86            .fetch_timestamp_messages(uaid, timestamp, limit)
87            .await
88    }
89
90    async fn increment_storage(&self, uaid: &Uuid, timestamp: u64) -> DbResult<()> {
91        Arc::as_ref(self).increment_storage(uaid, timestamp).await
92    }
93
94    async fn remove_message(&self, uaid: &Uuid, chidmessageid: &str) -> DbResult<()> {
95        Arc::as_ref(self).remove_message(uaid, chidmessageid).await
96    }
97
98    #[cfg(feature = "reliable_report")]
99    async fn log_report(
100        &self,
101        reliability_id: &str,
102        state: crate::reliability::ReliabilityState,
103    ) -> DbResult<()> {
104        Arc::as_ref(self).log_report(reliability_id, state).await
105    }
106
107    async fn router_table_exists(&self) -> DbResult<bool> {
108        Arc::as_ref(self).router_table_exists().await
109    }
110
111    async fn message_table_exists(&self) -> DbResult<bool> {
112        Arc::as_ref(self).message_table_exists().await
113    }
114
115    async fn health_check(&self) -> DbResult<bool> {
116        Arc::as_ref(self).health_check().await
117    }
118
119    fn box_clone(&self) -> Box<dyn DbClient> {
120        Box::new(Arc::clone(self))
121    }
122
123    fn name(&self) -> String {
124        Arc::as_ref(self).name()
125    }
126}
127
128impl MockDbClient {
129    /// Convert into a type which can be used in place of `Box<dyn DbClient>`.
130    /// Arc is used so that the mock can be cloned. Box is used so it can be
131    /// easily cast to `Box<dyn DbClient>`.
132    #[allow(clippy::redundant_allocation)]
133    pub fn into_boxed_arc(self) -> Box<Arc<Self>> {
134        Box::new(Arc::new(self))
135    }
136}