Compare commits
1 Commits
1.0.151
...
backup-mai
| Author | SHA1 | Date | |
|---|---|---|---|
| dd83f301f6 |
177
src/tests/inheritance_matching.rs
Normal file
177
src/tests/inheritance_matching.rs
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
use crate::*;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
fn setup_dynamic_pointer_db() {
|
||||||
|
let db_json = json!({
|
||||||
|
"puncs": [],
|
||||||
|
"enums": [],
|
||||||
|
"relations": [],
|
||||||
|
"types": [
|
||||||
|
{
|
||||||
|
"name": "bid.evaluation_context",
|
||||||
|
"variations": ["bid.evaluation_context"],
|
||||||
|
"hierarchy": ["bid.evaluation_context", "entity"],
|
||||||
|
"schemas": {
|
||||||
|
"bid.evaluation_context": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"amount": { "type": "number" }
|
||||||
|
},
|
||||||
|
"required": ["amount"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "work_order.evaluation_context",
|
||||||
|
"variations": ["work_order.evaluation_context"],
|
||||||
|
"hierarchy": ["work_order.evaluation_context", "entity"],
|
||||||
|
"schemas": {
|
||||||
|
"work_order.evaluation_context": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"vendor_id": { "type": "string" }
|
||||||
|
},
|
||||||
|
"required": ["vendor_id"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "evaluation_rule",
|
||||||
|
"variations": ["evaluation_rule"],
|
||||||
|
"hierarchy": ["evaluation_rule", "entity"],
|
||||||
|
"schemas": {
|
||||||
|
"evaluation_rule": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"entity_type": { "type": "string" },
|
||||||
|
"evaluation_context": { "type": "$entity_type.evaluation_context" }
|
||||||
|
},
|
||||||
|
"required": ["entity_type", "evaluation_context"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
let setup_res = jspg_setup(Json(db_json));
|
||||||
|
assert_eq!(
|
||||||
|
setup_res.0,
|
||||||
|
json!({
|
||||||
|
"type": "drop",
|
||||||
|
"response": "success"
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_structural_inheritance_dynamic_pointer() {
|
||||||
|
setup_dynamic_pointer_db();
|
||||||
|
|
||||||
|
// 1. Happy path: entity_type = bid, evaluation_context matches bid.evaluation_context
|
||||||
|
let happy_bid = json!({
|
||||||
|
"entity_type": "bid",
|
||||||
|
"evaluation_context": {
|
||||||
|
"amount": 5000
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let res_happy_bid = jspg_validate("evaluation_rule", JsonB(happy_bid));
|
||||||
|
assert_eq!(
|
||||||
|
res_happy_bid.0,
|
||||||
|
json!({
|
||||||
|
"type": "drop",
|
||||||
|
"response": "success"
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// 2. Happy path: entity_type = work_order, evaluation_context matches work_order.evaluation_context
|
||||||
|
let happy_wo = json!({
|
||||||
|
"entity_type": "work_order",
|
||||||
|
"evaluation_context": {
|
||||||
|
"vendor_id": "vendor-123"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let res_happy_wo = jspg_validate("evaluation_rule", JsonB(happy_wo));
|
||||||
|
assert_eq!(
|
||||||
|
res_happy_wo.0,
|
||||||
|
json!({
|
||||||
|
"type": "drop",
|
||||||
|
"response": "success"
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// 3. Unhappy path: entity_type = bid but evaluation_context has invalid schema (missing required field 'amount')
|
||||||
|
let unhappy_bid = json!({
|
||||||
|
"entity_type": "bid",
|
||||||
|
"evaluation_context": {
|
||||||
|
"vendor_id": "vendor-123"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let res_unhappy_bid = jspg_validate("evaluation_rule", JsonB(unhappy_bid));
|
||||||
|
assert_eq!(
|
||||||
|
res_unhappy_bid.0,
|
||||||
|
json!({
|
||||||
|
"type": "drop",
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"code": "REQUIRED_FIELD_MISSING",
|
||||||
|
"message": "Missing amount",
|
||||||
|
"details": { "path": "evaluation_context/amount" }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"code": "STRICT_PROPERTY_VIOLATION",
|
||||||
|
"message": "Unexpected property 'vendor_id'",
|
||||||
|
"details": { "path": "evaluation_context/vendor_id" }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// 4. Missing discriminator: dynamic pointer cannot resolve property 'entity_type'
|
||||||
|
let missing_disc = json!({
|
||||||
|
"evaluation_context": {}
|
||||||
|
});
|
||||||
|
let res_missing_disc = jspg_validate("evaluation_rule", JsonB(missing_disc));
|
||||||
|
assert_eq!(
|
||||||
|
res_missing_disc.0,
|
||||||
|
json!({
|
||||||
|
"type": "drop",
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"code": "REQUIRED_FIELD_MISSING",
|
||||||
|
"message": "Missing entity_type",
|
||||||
|
"details": { "path": "entity_type" }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"code": "DYNAMIC_TYPE_RESOLUTION_FAILED",
|
||||||
|
"message": "Dynamic type pointer '$entity_type.evaluation_context' could not resolve discriminator property 'entity_type' on parent instance",
|
||||||
|
"details": { "path": "evaluation_context" }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// 5. Unregistered discriminator: target not found in registry
|
||||||
|
let unknown_disc = json!({
|
||||||
|
"entity_type": "unknown",
|
||||||
|
"evaluation_context": {}
|
||||||
|
});
|
||||||
|
let res_unknown_disc = jspg_validate("evaluation_rule", JsonB(unknown_disc));
|
||||||
|
assert_eq!(
|
||||||
|
res_unknown_disc.0,
|
||||||
|
json!({
|
||||||
|
"type": "drop",
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"code": "DYNAMIC_TYPE_RESOLUTION_FAILED",
|
||||||
|
"message": "Resolved dynamic type pointer 'unknown.evaluation_context' was not found in schema registry",
|
||||||
|
"details": { "path": "evaluation_context" }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = jspg_teardown();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_all() {
|
||||||
|
test_structural_inheritance_dynamic_pointer();
|
||||||
|
}
|
||||||
124
src/tests/inline_validator.rs
Normal file
124
src/tests/inline_validator.rs
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
use crate::*;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
fn setup_simple_db() {
|
||||||
|
let db_json = json!({
|
||||||
|
"puncs": [],
|
||||||
|
"enums": [],
|
||||||
|
"relations": [],
|
||||||
|
"types": [
|
||||||
|
{
|
||||||
|
"name": "person",
|
||||||
|
"variations": ["person"],
|
||||||
|
"hierarchy": ["person", "entity"],
|
||||||
|
"schemas": {
|
||||||
|
"person": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"type": { "type": "string" },
|
||||||
|
"name": { "type": "string" }
|
||||||
|
},
|
||||||
|
"required": ["name"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
let setup_res = jspg_setup(Json(db_json));
|
||||||
|
assert_eq!(
|
||||||
|
setup_res.0,
|
||||||
|
json!({
|
||||||
|
"type": "drop",
|
||||||
|
"response": "success"
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_inline_schema_happy_path() {
|
||||||
|
// 1. Valid instance matching the inline schema
|
||||||
|
let inline_schema = r#"{"type": "object", "properties": {"age": {"type": "number"}}, "required": ["age"]}"#;
|
||||||
|
let happy_res = jspg_validate(inline_schema, JsonB(json!({"age": 42})));
|
||||||
|
assert_eq!(
|
||||||
|
happy_res.0,
|
||||||
|
json!({
|
||||||
|
"type": "drop",
|
||||||
|
"response": "success"
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// 2. Invalid instance failing the inline schema (missing required field)
|
||||||
|
let unhappy_res = jspg_validate(inline_schema, JsonB(json!({"name": "Neo"})));
|
||||||
|
assert_eq!(
|
||||||
|
unhappy_res.0,
|
||||||
|
json!({
|
||||||
|
"type": "drop",
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"code": "REQUIRED_FIELD_MISSING",
|
||||||
|
"message": "Missing age",
|
||||||
|
"details": { "path": "age" }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"code": "STRICT_PROPERTY_VIOLATION",
|
||||||
|
"message": "Unexpected property 'name'",
|
||||||
|
"details": { "path": "name" }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// 3. Inline schema referencing registered custom type "person"
|
||||||
|
let inline_with_ref = r#"{"type": "object", "properties": {"who": {"type": "person"}}, "required": ["who"]}"#;
|
||||||
|
let happy_ref_res = jspg_validate(inline_with_ref, JsonB(json!({"who": {"type": "person", "name": "Morpheus"}})));
|
||||||
|
assert_eq!(
|
||||||
|
happy_ref_res.0,
|
||||||
|
json!({
|
||||||
|
"type": "drop",
|
||||||
|
"response": "success"
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
let unhappy_ref_res = jspg_validate(inline_with_ref, JsonB(json!({"who": {"type": "person"}})));
|
||||||
|
assert_eq!(
|
||||||
|
unhappy_ref_res.0,
|
||||||
|
json!({
|
||||||
|
"type": "drop",
|
||||||
|
"errors": [
|
||||||
|
{
|
||||||
|
"code": "REQUIRED_FIELD_MISSING",
|
||||||
|
"message": "Missing name",
|
||||||
|
"details": { "path": "who/name" }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_inline_schema_syntax_errors() {
|
||||||
|
let malformed_schema = r#"{"type": "object", "properties": {"name": {"type": "#;
|
||||||
|
let res = jspg_validate(malformed_schema, JsonB(json!({})));
|
||||||
|
|
||||||
|
let errors = res.0.get("errors").expect("Expected errors block");
|
||||||
|
let err = errors.get(0).expect("Expected at least one error");
|
||||||
|
assert_eq!(err.get("code").unwrap().as_str().unwrap(), "SCHEMA_PARSE_FAILED");
|
||||||
|
assert!(err.get("message").unwrap().as_str().unwrap().contains("Failed to parse inline schema"));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_inline_schema_ref_compilation_errors() {
|
||||||
|
// Try to use a custom type that is not defined in the compiled database context.
|
||||||
|
let invalid_ref_schema = r#"{"type": "object", "properties": {"item": {"type": "non_existent_type"}}}"#;
|
||||||
|
let res = jspg_validate(invalid_ref_schema, JsonB(json!({"item": {}})));
|
||||||
|
|
||||||
|
let errors = res.0.get("errors").expect("Expected errors block");
|
||||||
|
let err = errors.get(0).expect("Expected compilation error");
|
||||||
|
assert_eq!(err.get("code").unwrap().as_str().unwrap(), "PROXY_TYPE_RESOLUTION_FAILED");
|
||||||
|
assert!(err.get("message").unwrap().as_str().unwrap().contains("non_existent_type"));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_all() {
|
||||||
|
setup_simple_db();
|
||||||
|
test_inline_schema_happy_path();
|
||||||
|
test_inline_schema_syntax_errors();
|
||||||
|
test_inline_schema_ref_compilation_errors();
|
||||||
|
let _ = jspg_teardown();
|
||||||
|
}
|
||||||
@ -2,12 +2,28 @@ use crate::*;
|
|||||||
pub mod formatter;
|
pub mod formatter;
|
||||||
pub mod runner;
|
pub mod runner;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
pub mod inline_validator;
|
||||||
|
pub mod swap_concurrency;
|
||||||
|
pub mod inheritance_matching;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
|
lazy_static::lazy_static! {
|
||||||
|
pub static ref TEST_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||||
|
}
|
||||||
|
|
||||||
// Database module tests moved to src/database/executors/mock.rs
|
// Database module tests moved to src/database/executors/mock.rs
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_jspg_custom_audit_suite() {
|
||||||
|
let _guard = TEST_MUTEX.lock().unwrap();
|
||||||
|
inline_validator::run_all();
|
||||||
|
inheritance_matching::run_all();
|
||||||
|
swap_concurrency::run_all();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_library_api() {
|
fn test_library_api() {
|
||||||
|
let _guard = TEST_MUTEX.lock().unwrap();
|
||||||
// 1. Initially, schemas are not cached.
|
// 1. Initially, schemas are not cached.
|
||||||
|
|
||||||
// Expected uninitialized drop format: errors + null response
|
// Expected uninitialized drop format: errors + null response
|
||||||
|
|||||||
91
src/tests/swap_concurrency.rs
Normal file
91
src/tests/swap_concurrency.rs
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
use crate::*;
|
||||||
|
use serde_json::json;
|
||||||
|
use std::thread;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
fn get_concurrency_db() -> serde_json::Value {
|
||||||
|
json!({
|
||||||
|
"puncs": [],
|
||||||
|
"enums": [],
|
||||||
|
"relations": [],
|
||||||
|
"types": [
|
||||||
|
{
|
||||||
|
"name": "person",
|
||||||
|
"variations": ["person"],
|
||||||
|
"hierarchy": ["person", "entity"],
|
||||||
|
"schemas": {
|
||||||
|
"person": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"type": { "type": "string" },
|
||||||
|
"name": { "type": "string" }
|
||||||
|
},
|
||||||
|
"required": ["name"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_concurrent_atomic_swap_isolation() {
|
||||||
|
// 1. Initialize first
|
||||||
|
let db_json = get_concurrency_db();
|
||||||
|
let setup_res = jspg_setup(Json(db_json.clone()));
|
||||||
|
assert_eq!(
|
||||||
|
setup_res.0,
|
||||||
|
json!({
|
||||||
|
"type": "drop",
|
||||||
|
"response": "success"
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// 2. Spawn 8 parallel reader threads validating schemas
|
||||||
|
let num_readers = 8;
|
||||||
|
let mut handles = Vec::new();
|
||||||
|
|
||||||
|
for i in 0..num_readers {
|
||||||
|
handles.push(thread::spawn(move || {
|
||||||
|
let payload = json!({"type": "person", "name": format!("Neo-{}", i)});
|
||||||
|
for _ in 0..100 {
|
||||||
|
let res = jspg_validate("person", JsonB(payload.clone()));
|
||||||
|
// The validation either succeeds or returns ENGINE_NOT_INITIALIZED (if teardown just swapped it)
|
||||||
|
// BUT it must NEVER panic, deadlock, or return corrupt memory!
|
||||||
|
let val = res.0;
|
||||||
|
let is_success = val.get("response").and_then(|r| r.as_str()) == Some("success");
|
||||||
|
let is_uninit = val.get("errors")
|
||||||
|
.and_then(|e| e.as_array())
|
||||||
|
.and_then(|arr| arr.get(0))
|
||||||
|
.and_then(|err| err.get("code"))
|
||||||
|
.and_then(|c| c.as_str()) == Some("ENGINE_NOT_INITIALIZED");
|
||||||
|
|
||||||
|
assert!(is_success || is_uninit, "Unexpected validation response: {:?}", val);
|
||||||
|
thread::yield_now();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Spawn a concurrent writer thread constantly calling setup and teardown
|
||||||
|
let db_json_shared = Arc::new(db_json);
|
||||||
|
let writer_handle = thread::spawn(move || {
|
||||||
|
for _ in 0..50 {
|
||||||
|
let _ = jspg_teardown();
|
||||||
|
thread::yield_now();
|
||||||
|
let _ = jspg_setup(Json((*db_json_shared).clone()));
|
||||||
|
thread::yield_now();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 4. Join everyone and verify no thread panicked
|
||||||
|
for handle in handles {
|
||||||
|
handle.join().expect("Reader thread panicked or deadlocked!");
|
||||||
|
}
|
||||||
|
writer_handle.join().expect("Writer thread panicked or deadlocked!");
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
let _ = jspg_teardown();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_all() {
|
||||||
|
test_concurrent_atomic_swap_isolation();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user