diff --git a/src/key_value.rs b/src/key_value.rs index be81bd8..c2d6526 100644 --- a/src/key_value.rs +++ b/src/key_value.rs @@ -11,13 +11,66 @@ pub(crate) struct CliStoreManager { impl StoreManager for CliStoreManager { async fn get_store( &self, - name: &str, + param: &str, _stats: Arc, ) -> Result, Error> { - let Some(opts) = self.stores.iter().find(|store| store.name == name) else { + let Some(opts) = self.stores.iter().find(|store| store.param == param) else { return Err(Error::NoSuchStore); }; let store = RedisStore::open(&opts.param).await?; Ok(Arc::new(store)) } } + +#[cfg(test)] +mod tests { + use super::*; + use smol_str::SmolStr; + + struct NoOpStats; + + impl ReadStats for NoOpStats { + fn count_kv_read(&self, _: i32) {} + fn count_kv_byod_read(&self, _: i32) {} + } + + fn make_kv_option(name: &str, param: &str) -> KvStoreOption { + KvStoreOption { + param: SmolStr::new(param), + name: SmolStr::new(name), + prefix: Default::default(), + cache_size: 1000, + cache_ttl: 60, + } + } + + #[tokio::test] + async fn get_store_returns_no_such_store_when_empty() { + let manager = CliStoreManager { stores: vec![] }; + let result = manager + .get_store("redis://localhost:6379", Arc::new(NoOpStats)) + .await; + assert!(matches!(result, Err(Error::NoSuchStore))); + } + + #[tokio::test] + async fn get_store_returns_no_such_store_when_param_unmatched() { + let manager = CliStoreManager { + stores: vec![make_kv_option("mystore", "redis://localhost:6379")], + }; + let result = manager + .get_store("redis://other:6379", Arc::new(NoOpStats)) + .await; + assert!(matches!(result, Err(Error::NoSuchStore))); + } + + #[tokio::test] + async fn get_store_searches_by_param_not_by_name() { + // get_store receives a Redis URL (param); passing the store name must return NoSuchStore + let manager = CliStoreManager { + stores: vec![make_kv_option("mystore", "redis://localhost:6379")], + }; + let result = manager.get_store("mystore", Arc::new(NoOpStats)).await; + assert!(matches!(result, Err(Error::NoSuchStore))); + } +}