Skip to content

Commit

Permalink
fix: property sort order + add tests #980
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Oct 8, 2024
1 parent 40c23a4 commit 80b016e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 65 additions & 1 deletion lib/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 80b016e

Please sign in to comment.