Test mux store... kinda

This commit is contained in:
Brian Picciano 2023-07-03 14:30:41 +02:00
parent 7a35befffe
commit 9c36ae1c7b
2 changed files with 124 additions and 36 deletions

View File

@ -50,3 +50,21 @@ pub trait Store {
fn get(&self, descr: origin::Descr) -> Result<sync::Arc<dyn origin::Origin>, GetError>; fn get(&self, descr: origin::Descr) -> Result<sync::Arc<dyn origin::Origin>, GetError>;
fn all_descrs(&self) -> Result<Vec<origin::Descr>, AllDescrsError>; fn all_descrs(&self) -> Result<Vec<origin::Descr>, AllDescrsError>;
} }
pub fn new_mock() -> sync::Arc<sync::Mutex<MockStore>> {
sync::Arc::new(sync::Mutex::new(MockStore::new()))
}
impl Store for sync::Arc<sync::Mutex<MockStore>> {
fn sync(&self, descr: origin::Descr, limits: Limits) -> Result<(), SyncError> {
self.lock().unwrap().sync(descr, limits)
}
fn get(&self, descr: origin::Descr) -> Result<sync::Arc<dyn origin::Origin>, GetError> {
self.lock().unwrap().get(descr)
}
fn all_descrs(&self) -> Result<Vec<origin::Descr>, AllDescrsError> {
self.lock().unwrap().all_descrs()
}
}

View File

@ -28,13 +28,13 @@ where
{ {
fn sync(&self, descr: origin::Descr, limits: store::Limits) -> Result<(), store::SyncError> { fn sync(&self, descr: origin::Descr, limits: store::Limits) -> Result<(), store::SyncError> {
(self.mapping_fn)(&descr) (self.mapping_fn)(&descr)
.or_unexpected_while(format!("fetching store for {:?}", &descr))? .or_unexpected_while(format!("mapping {:?} to store", &descr))?
.sync(descr, limits) .sync(descr, limits)
} }
fn get(&self, descr: origin::Descr) -> Result<sync::Arc<dyn origin::Origin>, store::GetError> { fn get(&self, descr: origin::Descr) -> Result<sync::Arc<dyn origin::Origin>, store::GetError> {
(self.mapping_fn)(&descr) (self.mapping_fn)(&descr)
.or_unexpected_while(format!("fetching store for {:?}", &descr))? .or_unexpected_while(format!("mapping {:?} to store", &descr))?
.get(descr) .get(descr)
} }
@ -49,50 +49,120 @@ where
} }
} }
/*
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::origin::{self, store}; use crate::origin::{self, store};
use mockall::predicate; use mockall::predicate;
use std::{cell, rc}; use std::sync;
#[test] struct Harness {
fn basic() { descr_a: origin::Descr,
let descrA = origin::Descr::Git { descr_b: origin::Descr,
descr_unknown: origin::Descr,
store_a: sync::Arc<sync::Mutex<store::MockStore>>,
store_b: sync::Arc<sync::Mutex<store::MockStore>>,
store: Box<dyn store::Store>,
}
impl Harness {
fn new() -> Harness {
let descr_a = origin::Descr::Git {
url: "A".to_string(), url: "A".to_string(),
branch_name: "A".to_string(), branch_name: "A".to_string(),
}; };
//let descrB = origin::Descr::Git { let descr_b = origin::Descr::Git {
// url: "B".to_string(), url: "B".to_string(),
// branch_name: "B".to_string(), branch_name: "B".to_string(),
//};
let sA_mock = rc::Rc::new(store::MockStore::new());
//let mut sB = store::MockStore::new();
let s = {
let sA: rc::Rc<dyn store::Store> = sA_mock.clone();
let sA = sA.as_ref();
super::new(
|descr| match descr {
&origin::Descr::Git { ref url, .. } if url == "A" => Some(sA),
//&origin::Descr::Git { ref url, .. } if url == "B" => Some(sB),
_ => None,
},
//vec![sA],
)
}; };
sA_mock let store_a = store::new_mock();
let store_b = store::new_mock();
Harness {
descr_a: descr_a.clone(),
descr_b: descr_b.clone(),
descr_unknown: origin::Descr::Git {
url: "X".to_string(),
branch_name: "X".to_string(),
},
store_a: store_a.clone(),
store_b: store_b.clone(),
store: Box::from(super::Store::new(
{
let store_a = store_a.clone();
let store_b = store_b.clone();
move |descr| match descr {
&origin::Descr::Git { ref url, .. } if url == "A" => {
Some(store_a.clone())
}
&origin::Descr::Git { ref url, .. } if url == "B" => {
Some(store_b.clone())
}
_ => None,
}
},
vec![store_a.clone(), store_b.clone()],
)),
}
}
}
#[test]
fn sync() {
let h = Harness::new();
h.store_a
.lock()
.unwrap()
.expect_sync() .expect_sync()
.with(predicate::eq(descrA.clone()), predicate::always()) .with(predicate::eq(h.descr_a.clone()), predicate::always())
.times(1) .times(1)
.return_const(Ok::<(), store::SyncError>(())); .return_const(Ok::<(), store::SyncError>(()));
assert_eq!(Ok(()), s.sync(descrA, store::Limits {})) assert_eq!(Ok(()), h.store.sync(h.descr_a.clone(), store::Limits {}));
h.store_b
.lock()
.unwrap()
.expect_sync()
.with(predicate::eq(h.descr_b.clone()), predicate::always())
.times(1)
.return_const(Ok::<(), store::SyncError>(()));
assert_eq!(Ok(()), h.store.sync(h.descr_b.clone(), store::Limits {}));
assert!(h
.store
.sync(h.descr_unknown.clone(), store::Limits {})
.is_err());
}
#[test]
fn all_descrs() {
let h = Harness::new();
h.store_a
.lock()
.unwrap()
.expect_all_descrs()
.times(1)
.return_const(Ok::<Vec<origin::Descr>, store::AllDescrsError>(vec![h
.descr_a
.clone()]));
h.store_b
.lock()
.unwrap()
.expect_all_descrs()
.times(1)
.return_const(Ok::<Vec<origin::Descr>, store::AllDescrsError>(vec![h
.descr_b
.clone()]));
assert_eq!(
Ok(vec![h.descr_a.clone(), h.descr_b.clone()]),
h.store.all_descrs(),
)
} }
} }
*/