Files
jspg/src/tests/mod.rs
2026-04-24 12:55:25 -04:00

270 lines
9.4 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("source_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": {}
}]
})
);
// 2. Cache schemas
let db_json = json!({
"puncs": [],
"enums": [],
"relations": [
{
"id": "11111111-1111-1111-1111-111111111111",
"type": "relation",
"constraint": "fk_test_target",
"source_type": "source_schema",
"source_columns": ["target_id"],
"destination_type": "target_schema",
"destination_columns": ["id"],
"prefix": "target"
}
],
"types": [
{
"name": "source_schema",
"variations": ["source_schema"],
"hierarchy": ["source_schema", "entity"],
"schemas": {
"source_schema": {
"type": "object",
"properties": {
"type": { "type": "string" },
"name": { "type": "string" },
"target": { "type": "target_schema" }
},
"required": ["name"]
}
}
},
{
"name": "target_schema",
"variations": ["target_schema"],
"hierarchy": ["target_schema", "entity"],
"schemas": {
"target_schema": {
"type": "object",
"properties": {
"value": { "type": "number" }
}
}
}
}
]
});
let cache_drop = jspg_setup(JsonB(db_json));
assert_eq!(
cache_drop.0,
json!({
"type": "drop",
"response": "success"
})
);
// 3. Validate jspg_database mapping natively!
let db_drop = jspg_database();
assert_eq!(
db_drop.0,
json!({
"type": "drop",
"response": {
"enums": {},
"puncs": {},
"relations": {
"fk_test_target": {
"id": "11111111-1111-1111-1111-111111111111",
"type": "relation",
"constraint": "fk_test_target",
"destination_columns": ["id"],
"destination_type": "target_schema",
"prefix": "target",
"source_columns": ["target_id"],
"source_type": "source_schema"
}
},
"types": {
"source_schema": {
"default_fields": [],
"field_types": null,
"fields": [],
"grouped_fields": null,
"hierarchy": ["source_schema", "entity"],
"historical": false,
"id": "",
"longevity": null,
"lookup_fields": [],
"module": "",
"name": "source_schema",
"notify": false,
"null_fields": [],
"ownable": false,
"relationship": false,
"schemas": {
"source_schema": {
"compiledEdges": {
"target": {
"constraint": "fk_test_target",
"forward": true
}
},
"compiledPropertyNames": ["name", "target", "type"],
"properties": {
"name": { "type": "string" },
"target": {
"compiledPropertyNames": ["value"],
"type": "target_schema"
},
"type": { "type": "string" }
},
"required": ["name"],
"type": "object"
},
"source_schema.filter": {
"compiledPropertyNames": ["$and", "$or", "name", "target", "type"],
"properties": {
"$and": {
"type": ["array", "null"],
"items": {
"compiledPropertyNames": ["$and", "$or", "name", "target", "type"],
"type": "source_schema.filter"
}
},
"$or": {
"type": ["array", "null"],
"items": {
"compiledPropertyNames": ["$and", "$or", "name", "target", "type"],
"type": "source_schema.filter"
}
},
"name": { "type": ["string.condition", "null"] },
"target": { "type": ["target_schema.filter", "null"] },
"type": { "type": ["string.condition", "null"] }
},
"type": "filter"
}
},
"sensitive": false,
"source": "",
"type": "",
"variations": ["source_schema"]
},
"target_schema": {
"default_fields": [],
"field_types": null,
"fields": [],
"grouped_fields": null,
"hierarchy": ["target_schema", "entity"],
"historical": false,
"id": "",
"longevity": null,
"lookup_fields": [],
"module": "",
"name": "target_schema",
"notify": false,
"null_fields": [],
"ownable": false,
"relationship": false,
"schemas": {
"target_schema": {
"compiledPropertyNames": ["value"],
"properties": {
"value": { "type": "number" }
},
"type": "object"
},
"target_schema.filter": {
"compiledPropertyNames": ["$and", "$or", "value"],
"properties": {
"$and": {
"type": ["array", "null"],
"items": {
"compiledPropertyNames": ["$and", "$or", "value"],
"type": "target_schema.filter"
}
},
"$or": {
"type": ["array", "null"],
"items": {
"compiledPropertyNames": ["$and", "$or", "value"],
"type": "target_schema.filter"
}
},
"value": { "type": ["number.condition", "null"] }
},
"type": "filter"
}
},
"sensitive": false,
"source": "",
"type": "",
"variations": ["target_schema"]
}
}
}
})
);
// 4. Validate Happy Path
let happy_drop = jspg_validate("source_schema", JsonB(json!({"type": "source_schema", "name": "Neo"})));
assert_eq!(
happy_drop.0,
json!({
"type": "drop",
"response": "success"
})
);
// 5. Validate Unhappy Path
let unhappy_drop = jspg_validate("source_schema", JsonB(json!({"type": "source_schema", "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");