wip: tests fbackend update -> diff

refactor
Laurin Weger 4 days ago
parent e14ccd616a
commit c0637ddaee
No known key found for this signature in database
GPG Key ID: 9B372BB0B792770F
  1. 15
      engine/verifier/src/orm/mod.rs
  2. 1
      sdk/js/signals/src/connector/applyDiff.ts
  3. 94
      sdk/rust/src/tests/mod.rs
  4. 87
      sdk/rust/src/tests/orm_creation.rs
  5. 1144
      sdk/rust/src/tests/orm_patches.rs

@ -706,6 +706,10 @@ impl Verifier {
scopes.push((scope.clone(), shapes));
}
log_debug!(
"[orm_backend_update], creating patch objects for scopes:\n{}",
scopes.len()
);
for (scope, shapes) in scopes {
let mut orm_changes: OrmChanges = HashMap::new();
@ -725,9 +729,14 @@ impl Verifier {
let subs = self.orm_subscriptions.get(&scope).unwrap();
for sub in subs.iter() {
// TODO: This if-condition is wrong (intended to not re-apply changes coming from the same subscription).
if sub.session_id == session_id {
continue;
}
// if sub.session_id == session_id {
// continue;
log_debug!(
"Applying changes to subscription with nuri {} and shape {}",
sub.nuri.repo(),
sub.shape_type.shape
);
// }
// Create diff from changes & subscription.
fn create_patches_for_nested_object(

@ -67,6 +67,7 @@ function isPrimitive(v: unknown): v is string | number | boolean {
);
}
// TODO: Escape slashes and tildes (~1, ~0)
/**
* Apply a diff to an object.
*

@ -7,8 +7,100 @@
// notice may not be copied, modified, or distributed except
// according to those terms.
use ng_repo::log_err;
use serde_json::Value;
use crate::local_broker::{doc_create, doc_sparql_update};
#[doc(hidden)]
pub mod orm_creation;
#[doc(hidden)]
pub mod orm;
pub mod orm_patches;
#[doc(hidden)]
pub mod create_or_open_wallet;
pub(crate) async fn create_doc_with_data(session_id: u64, sparql_insert: String) -> String {
let doc_nuri = doc_create(
session_id,
"Graph".to_string(),
"test_orm_query".to_string(),
"store".to_string(),
None,
None,
)
.await
.expect("error creating doc");
// Insert data
doc_sparql_update(session_id, sparql_insert, Some(doc_nuri.clone()))
.await
.expect("SPARQL update failed");
return doc_nuri;
}
pub(crate) fn assert_json_eq(expected: &mut Value, actual: &mut Value) {
remove_id_fields(expected);
remove_id_fields(actual);
sort_arrays(expected);
sort_arrays(actual);
let diff = serde_json_diff::values(expected.clone(), actual.clone());
if let Some(diff_) = diff {
log_err!("Expected and actual ORM JSON mismatch.\nDiff: {:?}", diff_);
assert!(false);
}
}
/// Helper to recursively sort all arrays in nested objects into a stable ordering.
/// Arrays are sorted by their JSON string representation.
fn sort_arrays(value: &mut Value) {
match value {
Value::Object(map) => {
for v in map.values_mut() {
sort_arrays(v);
}
}
Value::Array(arr) => {
// First, recursively sort nested structures
for v in arr.iter_mut() {
sort_arrays(v);
}
// Then sort the array itself by JSON string representation
arr.sort_by(|a, b| {
let a_str = canonical_json::ser::to_string(a).unwrap_or_default();
let b_str = canonical_json::ser::to_string(b).unwrap_or_default();
a_str.cmp(&b_str)
});
}
_ => {}
}
}
/// Helper to recursively remove nested "id" fields from nested objects,
/// but only if they are not at the root level.
fn remove_id_fields(value: &mut Value) {
fn remove_id_fields_inner(value: &mut Value, is_root: bool) {
match value {
Value::Object(map) => {
if !is_root {
map.remove("id");
}
for v in map.values_mut() {
remove_id_fields_inner(v, false);
}
}
Value::Array(arr) => {
for v in arr {
remove_id_fields_inner(v, false);
}
}
_ => {}
}
}
remove_id_fields_inner(value, true);
}

@ -9,10 +9,11 @@
use crate::local_broker::{doc_create, doc_sparql_construct, doc_sparql_update, orm_start};
use crate::tests::create_or_open_wallet::create_or_open_wallet;
use crate::tests::{assert_json_eq, create_doc_with_data};
use async_std::stream::StreamExt;
use ng_net::app_protocol::{AppResponse, AppResponseV0, NuriV0};
use ng_net::orm::{
self, BasicType, OrmSchema, OrmSchemaDataType, OrmSchemaLiteralType, OrmSchemaPredicate,
BasicType, OrmSchema, OrmSchemaDataType, OrmSchemaLiteralType, OrmSchemaPredicate,
OrmSchemaShape, OrmShapeType,
};
use ng_verifier::orm::utils::shape_type_to_sparql;
@ -1788,87 +1789,3 @@ fn create_big_schema() -> OrmSchema {
return schema;
}
async fn create_doc_with_data(session_id: u64, sparql_insert: String) -> String {
let doc_nuri = doc_create(
session_id,
"Graph".to_string(),
"test_orm_query".to_string(),
"store".to_string(),
None,
None,
)
.await
.expect("error creating doc");
// Insert data
doc_sparql_update(session_id, sparql_insert, Some(doc_nuri.clone()))
.await
.expect("SPARQL update failed");
return doc_nuri;
}
fn assert_json_eq(expected: &mut Value, actual: &mut Value) {
remove_id_fields(expected);
remove_id_fields(actual);
sort_arrays(expected);
sort_arrays(actual);
let diff = serde_json_diff::values(expected.clone(), actual.clone());
if let Some(diff_) = diff {
log_err!("Expected and actual ORM JSON mismatch.\nDiff: {:?}", diff_);
assert!(false);
}
}
/// Helper to recursively sort all arrays in nested objects into a stable ordering.
/// Arrays are sorted by their JSON string representation.
fn sort_arrays(value: &mut Value) {
match value {
Value::Object(map) => {
for v in map.values_mut() {
sort_arrays(v);
}
}
Value::Array(arr) => {
// First, recursively sort nested structures
for v in arr.iter_mut() {
sort_arrays(v);
}
// Then sort the array itself by JSON string representation
arr.sort_by(|a, b| {
let a_str = canonical_json::ser::to_string(a).unwrap_or_default();
let b_str = canonical_json::ser::to_string(b).unwrap_or_default();
a_str.cmp(&b_str)
});
}
_ => {}
}
}
/// Helper to recursively remove nested "id" fields from nested objects,
/// but only if they are not at the root level.
fn remove_id_fields(value: &mut Value) {
fn remove_id_fields_inner(value: &mut Value, is_root: bool) {
match value {
Value::Object(map) => {
if !is_root {
map.remove("id");
}
for v in map.values_mut() {
remove_id_fields_inner(v, false);
}
}
Value::Array(arr) => {
for v in arr {
remove_id_fields_inner(v, false);
}
}
_ => {}
}
}
remove_id_fields_inner(value, true);
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save