passing all tests
This commit is contained in:
@ -2,23 +2,38 @@ use crate::database::executors::DatabaseExecutor;
|
||||
use serde_json::Value;
|
||||
|
||||
#[cfg(test)]
|
||||
use std::sync::Mutex;
|
||||
use std::cell::RefCell;
|
||||
|
||||
#[cfg(test)]
|
||||
pub struct MockExecutor {
|
||||
pub query_responses: Mutex<Vec<Result<Value, String>>>,
|
||||
pub execute_responses: Mutex<Vec<Result<(), String>>>,
|
||||
pub captured_queries: Mutex<Vec<String>>,
|
||||
pub struct MockState {
|
||||
pub captured_queries: Vec<String>,
|
||||
pub query_responses: Vec<Result<Value, String>>,
|
||||
pub execute_responses: Vec<Result<(), String>>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl MockState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
captured_queries: Default::default(),
|
||||
query_responses: Default::default(),
|
||||
execute_responses: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
pub static MOCK_STATE: RefCell<MockState> = RefCell::new(MockState::new());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub struct MockExecutor {}
|
||||
|
||||
#[cfg(test)]
|
||||
impl MockExecutor {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
query_responses: Mutex::new(Vec::new()),
|
||||
execute_responses: Mutex::new(Vec::new()),
|
||||
captured_queries: Mutex::new(Vec::new()),
|
||||
}
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,22 +41,26 @@ impl MockExecutor {
|
||||
impl DatabaseExecutor for MockExecutor {
|
||||
fn query(&self, sql: &str, _args: Option<&[Value]>) -> Result<Value, String> {
|
||||
println!("DEBUG SQL QUERY: {}", sql);
|
||||
self.captured_queries.lock().unwrap().push(sql.to_string());
|
||||
let mut responses = self.query_responses.lock().unwrap();
|
||||
if responses.is_empty() {
|
||||
return Ok(Value::Array(vec![]));
|
||||
}
|
||||
responses.remove(0)
|
||||
MOCK_STATE.with(|state| {
|
||||
let mut s = state.borrow_mut();
|
||||
s.captured_queries.push(sql.to_string());
|
||||
if s.query_responses.is_empty() {
|
||||
return Ok(Value::Array(vec![]));
|
||||
}
|
||||
s.query_responses.remove(0)
|
||||
})
|
||||
}
|
||||
|
||||
fn execute(&self, sql: &str, _args: Option<&[Value]>) -> Result<(), String> {
|
||||
println!("DEBUG SQL EXECUTE: {}", sql);
|
||||
self.captured_queries.lock().unwrap().push(sql.to_string());
|
||||
let mut responses = self.execute_responses.lock().unwrap();
|
||||
if responses.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
responses.remove(0)
|
||||
MOCK_STATE.with(|state| {
|
||||
let mut s = state.borrow_mut();
|
||||
s.captured_queries.push(sql.to_string());
|
||||
if s.execute_responses.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
s.execute_responses.remove(0)
|
||||
})
|
||||
}
|
||||
|
||||
fn auth_user_id(&self) -> Result<String, String> {
|
||||
@ -54,11 +73,16 @@ impl DatabaseExecutor for MockExecutor {
|
||||
|
||||
#[cfg(test)]
|
||||
fn get_queries(&self) -> Vec<String> {
|
||||
self.captured_queries.lock().unwrap().clone()
|
||||
MOCK_STATE.with(|state| state.borrow().captured_queries.clone())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn reset_mocks(&self) {
|
||||
self.captured_queries.lock().unwrap().clear();
|
||||
MOCK_STATE.with(|state| {
|
||||
let mut s = state.borrow_mut();
|
||||
s.captured_queries.clear();
|
||||
s.query_responses.clear();
|
||||
s.execute_responses.clear();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user