From 80b016e9aeeb023d260804134c8422cf7ddf94bd Mon Sep 17 00:00:00 2001 From: Joep Meindertsma Date: Tue, 8 Oct 2024 10:40:15 +0200 Subject: [PATCH] fix: property sort order + add tests #980 --- CHANGELOG.md | 4 +++ lib/src/parse.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14366194..2f147270 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ By far most changes relate to `atomic-server`, so if not specified, assume the c **Changes to JS assets (including the front-end and JS libraries) are not shown here**, but in [`/browser/CHANGELOG`](/browser/CHANGELOG.md). See [STATUS.md](server/STATUS.md) to learn more about which features will remain stable. +## [v0.40.2] + +- fix property sort order + add tests #980 + ## [v0.40.0] - 2024-10-07 - Speed up Commits by bundling DB transactions #297 diff --git a/lib/src/parse.rs b/lib/src/parse.rs index 74bcb6ef..af68fc83 100644 --- a/lib/src/parse.rs +++ b/lib/src/parse.rs @@ -124,7 +124,7 @@ pub fn parse_json_ad_string( arr.sort_by(|a, b| { let a_is_prop = object_is_property(a); let b_is_prop = object_is_property(b); - a_is_prop.cmp(&b_is_prop) + b_is_prop.cmp(&a_is_prop) }); for item in arr { @@ -649,4 +649,68 @@ mod test { parse_opts.overwrite_outside = true; store.import(&json, &parse_opts).unwrap(); } + + #[test] + fn is_property() { + let json = r#" + { + "https://atomicdata.dev/properties/localId": "newprop", + "https://atomicdata.dev/properties/datatype": "https://atomicdata.dev/datatypes/string", + "https://atomicdata.dev/properties/description": "test property", + "https://atomicdata.dev/properties/isA": [ + "https://atomicdata.dev/classes/Property" + ], + "https://atomicdata.dev/properties/shortname": "homepage" +} + "#; + + let object: serde_json::Value = serde_json::from_str(json).unwrap(); + + assert!( + object_is_property(&object), + "This JSON should be parsed as a property" + ) + } + + #[test] + /// The importer should import properties first + fn parse_sorted_properties() { + let (store, importer) = create_store_and_importer(); + store.populate().unwrap(); + + let json = r#"[ +{ + "@id": "local:test1", + "newprop": "val" +}, + { + "https://atomicdata.dev/properties/localId": "newprop", + "https://atomicdata.dev/properties/datatype": "https://atomicdata.dev/datatypes/string", + "https://atomicdata.dev/properties/description": "test property", + "https://atomicdata.dev/properties/isA": [ + "https://atomicdata.dev/classes/Property" + ], + "https://atomicdata.dev/properties/shortname": "homepage" +}]"#; + + let parse_opts = crate::parse::ParseOpts { + for_agent: ForAgent::Sudo, + importer: Some(importer.clone()), + overwrite_outside: false, + // We sign the importer Commits with the default agent, + // not the one performing the import, because we don't have their private key. + signer: Some(store.get_default_agent().unwrap()), + save: crate::parse::SaveOpts::Commit, + }; + + store.import(json, &parse_opts).unwrap(); + + let found = store.get_resource("local:test1").unwrap(); + assert_eq!(found.get(urls::PARENT).unwrap().to_string(), importer); + + let newprop_subject = format!("{importer}/newprop"); + store + .get_resource(&newprop_subject) + .expect("newprop not found"); + } }