114 lines
2.5 KiB
Rust
114 lines
2.5 KiB
Rust
use crate::*;
|
|
pub mod runner;
|
|
pub mod types;
|
|
use serde_json::json;
|
|
|
|
// Database module tests moved to src/database/executors/mock.rs
|
|
|
|
#[test]
|
|
fn test_library_api() {
|
|
// 1. Initially, schemas are not cached.
|
|
|
|
// Expected uninitialized drop format: errors + null response
|
|
let uninitialized_drop = jspg_validate("test_schema", JsonB(json!({})));
|
|
assert_eq!(
|
|
uninitialized_drop.0,
|
|
json!({
|
|
"type": "drop",
|
|
"errors": [{
|
|
"code": "ENGINE_NOT_INITIALIZED",
|
|
"message": "JSPG extension has not been initialized via jspg_setup",
|
|
"details": { "path": "" }
|
|
}]
|
|
})
|
|
);
|
|
|
|
// 2. Cache schemas
|
|
let db_json = json!({
|
|
"puncs": [],
|
|
"enums": [],
|
|
"relations": [],
|
|
"types": [{
|
|
"schemas": [{
|
|
"$id": "test_schema",
|
|
"type": "object",
|
|
"properties": {
|
|
"name": { "type": "string" }
|
|
},
|
|
"required": ["name"]
|
|
}]
|
|
}]
|
|
});
|
|
|
|
let cache_drop = jspg_setup(JsonB(db_json));
|
|
assert_eq!(
|
|
cache_drop.0,
|
|
json!({
|
|
"type": "drop",
|
|
"response": "success"
|
|
})
|
|
);
|
|
|
|
// 3. Validate jspg_schemas
|
|
let schemas_drop = jspg_schemas();
|
|
assert_eq!(
|
|
schemas_drop.0,
|
|
json!({
|
|
"type": "drop",
|
|
"response": {
|
|
"test_schema": {
|
|
"$id": "test_schema",
|
|
"type": "object",
|
|
"properties": {
|
|
"name": { "type": "string" }
|
|
},
|
|
"required": ["name"]
|
|
}
|
|
}
|
|
})
|
|
);
|
|
|
|
// 4. Validate Happy Path
|
|
let happy_drop = jspg_validate("test_schema", JsonB(json!({"name": "Neo"})));
|
|
assert_eq!(
|
|
happy_drop.0,
|
|
json!({
|
|
"type": "drop",
|
|
"response": "success"
|
|
})
|
|
);
|
|
|
|
// 5. Validate Unhappy Path
|
|
let unhappy_drop = jspg_validate("test_schema", JsonB(json!({"wrong": "data"})));
|
|
assert_eq!(
|
|
unhappy_drop.0,
|
|
json!({
|
|
"type": "drop",
|
|
"errors": [
|
|
{
|
|
"code": "REQUIRED_FIELD_MISSING",
|
|
"message": "Missing name",
|
|
"details": { "path": "/name" }
|
|
},
|
|
{
|
|
"code": "STRICT_PROPERTY_VIOLATION",
|
|
"message": "Unexpected property 'wrong'",
|
|
"details": { "path": "/wrong" }
|
|
}
|
|
]
|
|
})
|
|
);
|
|
|
|
// 6. Clear Schemas
|
|
let clear_drop = jspg_teardown();
|
|
assert_eq!(
|
|
clear_drop.0,
|
|
json!({
|
|
"type": "drop",
|
|
"response": "success"
|
|
})
|
|
);
|
|
}
|
|
|
|
include!("fixtures.rs");
|