114 lines
2.6 KiB
Rust
114 lines
2.6 KiB
Rust
use jspg::*;
|
|
use pgrx::JsonB;
|
|
use serde_json::json;
|
|
|
|
#[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!({})));
|
|
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()",
|
|
"details": { "path": "" }
|
|
}]
|
|
})
|
|
);
|
|
|
|
// 2. Cache schemas
|
|
let puncs = json!([]);
|
|
let types = json!([{
|
|
"schemas": [{
|
|
"$id": "test_schema",
|
|
"type": "object",
|
|
"properties": {
|
|
"name": { "type": "string" }
|
|
},
|
|
"required": ["name"]
|
|
}]
|
|
}]);
|
|
let enums = json!([]);
|
|
|
|
let cache_drop = cache_json_schemas(JsonB(enums), JsonB(types), JsonB(puncs));
|
|
assert_eq!(
|
|
cache_drop.0,
|
|
json!({
|
|
"type": "drop",
|
|
"response": "success"
|
|
})
|
|
);
|
|
|
|
// 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"})));
|
|
assert_eq!(
|
|
happy_drop.0,
|
|
json!({
|
|
"type": "drop",
|
|
"response": "success"
|
|
})
|
|
);
|
|
|
|
// 5. Validate Unhappy Path
|
|
let unhappy_drop = validate_json_schema("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. Mask Happy Path
|
|
let mask_drop = mask_json_schema(
|
|
"test_schema",
|
|
JsonB(json!({"name": "Neo", "extra": "data"})),
|
|
);
|
|
assert_eq!(
|
|
mask_drop.0,
|
|
json!({
|
|
"type": "drop",
|
|
"response": {"name": "Neo"}
|
|
})
|
|
);
|
|
|
|
// 7. Clear Schemas
|
|
let clear_drop = clear_json_schemas();
|
|
assert_eq!(
|
|
clear_drop.0,
|
|
json!({
|
|
"type": "drop",
|
|
"response": "success"
|
|
})
|
|
);
|
|
assert!(!json_schema_cached("test_schema"));
|
|
}
|