use crate::dao::GarrisonDao;
use crate::error::{GarrisonError, GarrisonResult};
use crate::stp::GarrisonInterface;
use async_trait::async_trait;
use parking_lot::Mutex;
use std::collections::HashMap;
use std::time::{Duration, Instant};
pub struct MockDao {
store: Mutex<HashMap<String, (String, Option<Instant>)>>,
}
impl MockDao {
pub fn new() -> Self {
Self {
store: Mutex::new(HashMap::new()),
}
}
}
#[async_trait]
impl GarrisonDao for MockDao {
async fn get(&self, key: &str) -> GarrisonResult<Option<String>> {
let mut store = self.store.lock();
match store.get(key) {
Some((value, expire_at)) => {
if let Some(deadline) = expire_at {
if Instant::now() >= *deadline {
store.remove(key);
return Ok(None);
}
}
Ok(Some(value.clone()))
},
None => Ok(None),
}
}
async fn set(&self, key: &str, value: &str, ttl_seconds: u64) -> GarrisonResult<()> {
let expire_at = if ttl_seconds == 0 {
None
} else {
Some(Instant::now() + Duration::from_secs(ttl_seconds))
};
self.store
.lock()
.insert(key.to_string(), (value.to_string(), expire_at));
Ok(())
}
async fn update(&self, key: &str, value: &str) -> GarrisonResult<()> {
let mut store = self.store.lock();
match store.get_mut(key) {
Some((existing, _)) => {
*existing = value.to_string();
Ok(())
},
None => Err(GarrisonError::Dao(format!("dao-key-not-found::{}", key))),
}
}
async fn expire(&self, key: &str, seconds: u64) -> GarrisonResult<()> {
let mut store = self.store.lock();
match store.get_mut(key) {
Some((_, expire_at)) => {
*expire_at = if seconds == 0 {
None
} else {
Some(Instant::now() + Duration::from_secs(seconds))
};
Ok(())
},
None => Err(GarrisonError::Dao(format!("dao-key-not-found::{}", key))),
}
}
async fn delete(&self, key: &str) -> GarrisonResult<()> {
self.store.lock().remove(key);
Ok(())
}
crate::atomic_test_fallback!();
}
pub struct MockInterface {
permissions: HashMap<String, Vec<String>>,
roles: HashMap<String, Vec<String>>,
}
impl MockInterface {
pub fn new() -> Self {
Self {
permissions: HashMap::new(),
roles: HashMap::new(),
}
}
pub fn with_permission(mut self, login_id: &str, perms: &[&str]) -> Self {
self.permissions.insert(
login_id.to_string(),
perms.iter().map(|s| s.to_string()).collect(),
);
self
}
pub fn with_role(mut self, login_id: &str, roles: &[&str]) -> Self {
self.roles.insert(
login_id.to_string(),
roles.iter().map(|s| s.to_string()).collect(),
);
self
}
}
#[async_trait]
impl GarrisonInterface for MockInterface {
async fn get_permission_list(&self, login_id: &str) -> GarrisonResult<Vec<String>> {
Ok(self.permissions.get(login_id).cloned().unwrap_or_default())
}
async fn get_role_list(&self, login_id: &str) -> GarrisonResult<Vec<String>> {
Ok(self.roles.get(login_id).cloned().unwrap_or_default())
}
}
#[cfg(test)]
mod mock_dao_coverage_tests {
use super::*;
#[tokio::test]
async fn mock_dao_atomic_and_default_methods_coverage() {
let dao = MockDao::new();
dao.set("k1", "v1", 60).await.unwrap();
assert!(dao.set_if_absent("a1", "v1", 60).await.unwrap());
assert_eq!(
dao.get_and_delete("a1").await.unwrap().as_deref(),
Some("v1")
);
assert_eq!(dao.incr("ctr", 60).await.unwrap(), 1);
assert_eq!(dao.decr("ctr").await.unwrap(), 0);
dao.rename("k1", "k2").await.unwrap();
assert_eq!(dao.get("k2").await.unwrap().as_deref(), Some("v1"));
assert!(dao
.compare_and_swap("k2", Some("v1"), "v2", 60)
.await
.unwrap());
assert_eq!(dao.get("k2").await.unwrap().as_deref(), Some("v2"));
dao.set_permanent("p1", "val").await.unwrap();
assert_eq!(dao.get("p1").await.unwrap().as_deref(), Some("val"));
assert!(dao.get_with_ttl("k1").await.unwrap().is_none());
assert!(matches!(
dao.get_timeout("k1").await,
Err(GarrisonError::NotImplemented(_))
));
assert!(matches!(
dao.keys("*").await,
Err(GarrisonError::NotImplemented(_))
));
assert!(matches!(
dao.find_social_binding(0, "wechat", "oid").await,
Err(GarrisonError::NotImplemented(_))
));
assert!(matches!(
dao.insert_social_binding(0, "u1", "wechat", "oid", None, 0)
.await,
Err(GarrisonError::NotImplemented(_))
));
assert!(matches!(
dao.compare_and_update_if_greater("k1", 10, 60).await,
Err(GarrisonError::NotImplemented(_))
));
assert!(matches!(
dao.eval_lua("return 1", vec![], vec![]).await,
Err(GarrisonError::NotImplemented(_))
));
#[cfg(any(feature = "db-sqlite", feature = "db-postgres", feature = "db-mysql"))]
{
assert!(matches!(
dao.insert_credit_consumption(0, "r", 1, 100, 100, 0).await,
Err(GarrisonError::NotImplemented(_))
));
assert!(matches!(
dao.query_credit_consumption(0, 0, 0).await,
Err(GarrisonError::NotImplemented(_))
));
assert!(matches!(
dao.query_role_hierarchy_edges(0).await,
Err(GarrisonError::NotImplemented(_))
));
assert!(matches!(
dao.insert_role_hierarchy_edge(0, "c", "p").await,
Err(GarrisonError::NotImplemented(_))
));
assert!(matches!(
dao.delete_role_hierarchy_edge(0, "c", "p").await,
Err(GarrisonError::NotImplemented(_))
));
}
}
}