use super::{AdapterError, StoreAdapter};
use anyhow::anyhow;
use async_trait::async_trait;
use std::marker::PhantomData;
pub struct FailStoreAdapter<K, R> {
key: PhantomData<K>,
certificate: PhantomData<R>,
}
impl<K, R> FailStoreAdapter<K, R> {
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());
}
}