use std::collections::HashMap;
use std::sync::Mutex;
use super::{GraphError, GraphParam, GraphRows, GraphStore};
#[derive(Default)]
pub struct InMemoryGraphStore {
rows: Mutex<GraphRows>,
}
impl InMemoryGraphStore {
pub fn new() -> Self {
Self::default()
}
pub fn stage_rows(&self, rows: GraphRows) {
*self.rows.lock().expect("graph store mutex poisoned") = rows;
}
}
impl GraphStore for InMemoryGraphStore {
async fn ensure_graph(&self) -> Result<(), GraphError> {
Ok(())
}
async fn query(&self, _cypher: &str, _params: &HashMap<String, GraphParam>) -> Result<GraphRows, GraphError> {
Ok(self.rows.lock().expect("graph store mutex poisoned").clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test(flavor = "current_thread")]
async fn should_return_staged_rows_from_query() {
let store = InMemoryGraphStore::new();
store.ensure_graph().await.unwrap();
store.stage_rows(vec![vec![("n".to_string(), "Alice".to_string())]]);
let rows = store.query("MATCH (n) RETURN n", &HashMap::new()).await.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0][0], ("n".to_string(), "Alice".to_string()));
}
#[tokio::test(flavor = "current_thread")]
async fn should_return_empty_when_nothing_staged() {
let store = InMemoryGraphStore::new();
let rows = store.query("MATCH (n) RETURN n", &HashMap::new()).await.unwrap();
assert!(rows.is_empty());
}
}