use pgrx::prelude::*;
use serde_json::json;
#[pg_extern]
fn validate(data_graph_id: i64, shapes_graph_id: i64) -> pgrx::JsonB {
let data_triples = count_quads(data_graph_id);
let shapes_triples = count_quads(shapes_graph_id);
pgrx::JsonB(json!({
"status": "stub",
"reason": "ERRATA E-009 — shacl_validation upstream dep block (iri_s/rudof_iri split + rdf-12 feature unification vs reasonable)",
"data_graph_id": data_graph_id,
"shapes_graph_id": shapes_graph_id,
"data_triples": data_triples,
"shapes_triples": shapes_triples,
"conforms": serde_json::Value::Null,
"results": [],
}))
}
fn count_quads(graph_id: i64) -> i64 {
Spi::get_one_with_args::<i64>(
"SELECT count(*)::bigint FROM pgrdf._pgrdf_quads WHERE graph_id = $1",
&[graph_id.into()],
)
.ok()
.flatten()
.unwrap_or(0)
}
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use pgrx::prelude::*;
#[pg_test]
fn validate_stub_shape() {
let g_data: i64 = 8500;
let g_shapes: i64 = 8501;
Spi::run_with_args("SELECT pgrdf.add_graph($1)", &[g_data.into()]).unwrap();
Spi::run_with_args(
"SELECT pgrdf.parse_turtle('@prefix ex:<http://example.com/> . ex:a ex:p ex:b .', $1)",
&[g_data.into()],
)
.unwrap();
Spi::run_with_args("SELECT pgrdf.add_graph($1)", &[g_shapes.into()]).unwrap();
Spi::run_with_args(
"SELECT pgrdf.parse_turtle('@prefix sh:<http://www.w3.org/ns/shacl#> . sh:NodeShape sh:targetClass sh:NodeShape .', $1)",
&[g_shapes.into()],
)
.unwrap();
let j: pgrx::JsonB = Spi::get_one_with_args(
"SELECT pgrdf.validate($1, $2)",
&[g_data.into(), g_shapes.into()],
)
.unwrap()
.unwrap();
let v = &j.0;
assert_eq!(v["status"], "stub");
assert_eq!(v["data_graph_id"], g_data);
assert_eq!(v["shapes_graph_id"], g_shapes);
assert_eq!(v["data_triples"], 1);
assert!(v["conforms"].is_null());
assert!(v["results"].is_array());
}
#[pg_test]
fn validate_stub_unknown_graphs() {
let j: pgrx::JsonB = Spi::get_one("SELECT pgrdf.validate(999990::bigint, 999991::bigint)")
.unwrap()
.unwrap();
let v = &j.0;
assert_eq!(v["data_triples"], 0);
assert_eq!(v["shapes_triples"], 0);
assert_eq!(v["status"], "stub");
}
}