query test progress
This commit is contained in:
43
tests/database/executor.rs
Normal file
43
tests/database/executor.rs
Normal file
@ -0,0 +1,43 @@
|
||||
use jspg::database::executor::DatabaseExecutor;
|
||||
use serde_json::Value;
|
||||
use std::sync::Mutex;
|
||||
|
||||
pub struct MockExecutor {
|
||||
pub query_responses: Mutex<Vec<Result<Value, String>>>,
|
||||
pub execute_responses: Mutex<Vec<Result<(), String>>>,
|
||||
}
|
||||
|
||||
impl MockExecutor {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
query_responses: Mutex::new(Vec::new()),
|
||||
execute_responses: Mutex::new(Vec::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DatabaseExecutor for MockExecutor {
|
||||
fn query(&self, _sql: &str, _args: Option<&[Value]>) -> Result<Value, String> {
|
||||
let mut responses = self.query_responses.lock().unwrap();
|
||||
if responses.is_empty() {
|
||||
return Ok(Value::Array(vec![]));
|
||||
}
|
||||
responses.remove(0)
|
||||
}
|
||||
|
||||
fn execute(&self, _sql: &str, _args: Option<&[Value]>) -> Result<(), String> {
|
||||
let mut responses = self.execute_responses.lock().unwrap();
|
||||
if responses.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
responses.remove(0)
|
||||
}
|
||||
|
||||
fn auth_user_id(&self) -> Result<String, String> {
|
||||
Ok("00000000-0000-0000-0000-000000000000".to_string())
|
||||
}
|
||||
|
||||
fn timestamp(&self) -> Result<String, String> {
|
||||
Ok("2026-03-10T00:00:00Z".to_string())
|
||||
}
|
||||
}
|
||||
1
tests/database/mod.rs
Normal file
1
tests/database/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod executor;
|
||||
29
tests/lib.rs
29
tests/lib.rs
@ -2,20 +2,21 @@ use ::jspg::*;
|
||||
use pgrx::JsonB;
|
||||
use serde_json::json;
|
||||
|
||||
pub mod database;
|
||||
|
||||
#[test]
|
||||
fn test_library_api() {
|
||||
// 1. Initially, schemas are not cached.
|
||||
assert!(!json_schema_cached("test_schema"));
|
||||
|
||||
// Expected uninitialized drop format: errors + null response
|
||||
let uninitialized_drop = validate_json_schema("test_schema", JsonB(json!({})));
|
||||
let uninitialized_drop = jspg_validate("test_schema", JsonB(json!({})));
|
||||
assert_eq!(
|
||||
uninitialized_drop.0,
|
||||
json!({
|
||||
"type": "drop",
|
||||
"errors": [{
|
||||
"code": "VALIDATOR_NOT_INITIALIZED",
|
||||
"message": "JSON Schemas have not been cached yet. Run cache_json_schemas()",
|
||||
"message": "The JSPG database has not been cached yet. Run jspg_setup()",
|
||||
"details": { "path": "" }
|
||||
}]
|
||||
})
|
||||
@ -25,6 +26,7 @@ fn test_library_api() {
|
||||
let db_json = json!({
|
||||
"puncs": [],
|
||||
"enums": [],
|
||||
"relations": [],
|
||||
"types": [{
|
||||
"schemas": [{
|
||||
"$id": "test_schema",
|
||||
@ -37,7 +39,7 @@ fn test_library_api() {
|
||||
}]
|
||||
});
|
||||
|
||||
let cache_drop = jspg_cache_database(JsonB(db_json));
|
||||
let cache_drop = jspg_setup(JsonB(db_json));
|
||||
assert_eq!(
|
||||
cache_drop.0,
|
||||
json!({
|
||||
@ -46,20 +48,8 @@ fn test_library_api() {
|
||||
})
|
||||
);
|
||||
|
||||
// 3. Check schemas are cached
|
||||
assert!(json_schema_cached("test_schema"));
|
||||
|
||||
let show_drop = show_json_schemas();
|
||||
assert_eq!(
|
||||
show_drop.0,
|
||||
json!({
|
||||
"type": "drop",
|
||||
"response": ["test_schema"]
|
||||
})
|
||||
);
|
||||
|
||||
// 4. Validate Happy Path
|
||||
let happy_drop = validate_json_schema("test_schema", JsonB(json!({"name": "Neo"})));
|
||||
let happy_drop = jspg_validate("test_schema", JsonB(json!({"name": "Neo"})));
|
||||
assert_eq!(
|
||||
happy_drop.0,
|
||||
json!({
|
||||
@ -69,7 +59,7 @@ fn test_library_api() {
|
||||
);
|
||||
|
||||
// 5. Validate Unhappy Path
|
||||
let unhappy_drop = validate_json_schema("test_schema", JsonB(json!({"wrong": "data"})));
|
||||
let unhappy_drop = jspg_validate("test_schema", JsonB(json!({"wrong": "data"})));
|
||||
assert_eq!(
|
||||
unhappy_drop.0,
|
||||
json!({
|
||||
@ -90,7 +80,7 @@ fn test_library_api() {
|
||||
);
|
||||
|
||||
// 6. Clear Schemas
|
||||
let clear_drop = clear_json_schemas();
|
||||
let clear_drop = jspg_teardown();
|
||||
assert_eq!(
|
||||
clear_drop.0,
|
||||
json!({
|
||||
@ -98,5 +88,4 @@ fn test_library_api() {
|
||||
"response": "success"
|
||||
})
|
||||
);
|
||||
assert!(!json_schema_cached("test_schema"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user