|
| 1 | +use linkml_runtime::{load_json_str, load_yaml_str, LinkMLValue}; |
| 2 | +use linkml_schemaview::identifier::converter_from_schema; |
| 3 | +use linkml_schemaview::io::from_yaml; |
| 4 | +use linkml_schemaview::schemaview::SchemaView; |
| 5 | +use std::path::Path; |
| 6 | + |
| 7 | +fn data_path(name: &str) -> std::path::PathBuf { |
| 8 | + let mut p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 9 | + p.push("tests"); |
| 10 | + p.push("data"); |
| 11 | + p.push(name); |
| 12 | + p |
| 13 | +} |
| 14 | + |
| 15 | +#[test] |
| 16 | +fn object_equality_ignores_null_assignments() { |
| 17 | + // Load personinfo schema and Container class |
| 18 | + let schema = from_yaml(Path::new(&data_path("personinfo.yaml"))).unwrap(); |
| 19 | + let mut sv = SchemaView::new(); |
| 20 | + sv.add_schema(schema.clone()).unwrap(); |
| 21 | + let conv = converter_from_schema(&schema); |
| 22 | + let container = sv |
| 23 | + .get_class( |
| 24 | + &linkml_schemaview::identifier::Identifier::new("Container"), |
| 25 | + &conv, |
| 26 | + ) |
| 27 | + .unwrap() |
| 28 | + .expect("class not found"); |
| 29 | + |
| 30 | + let doc_with_null = r#" |
| 31 | +objects: |
| 32 | + - objecttype: personinfo:Person |
| 33 | + id: "P:1" |
| 34 | + name: "Alice" |
| 35 | + current_address: null |
| 36 | +"#; |
| 37 | + let doc_without_slot = r#" |
| 38 | +objects: |
| 39 | + - objecttype: personinfo:Person |
| 40 | + id: "P:1" |
| 41 | + name: "Alice" |
| 42 | +"#; |
| 43 | + let v1 = load_yaml_str(doc_with_null, &sv, &container, &conv).unwrap(); |
| 44 | + let v2 = load_yaml_str(doc_without_slot, &sv, &container, &conv).unwrap(); |
| 45 | + let p1 = v1.navigate_path(["objects", "0"]).unwrap(); |
| 46 | + let p2 = v2.navigate_path(["objects", "0"]).unwrap(); |
| 47 | + assert!( |
| 48 | + p1.equals(p2), |
| 49 | + "Person with null assignment should equal omission" |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn list_identity_is_order_sensitive() { |
| 55 | + let schema = from_yaml(Path::new(&data_path("personinfo.yaml"))).unwrap(); |
| 56 | + let mut sv = SchemaView::new(); |
| 57 | + sv.add_schema(schema.clone()).unwrap(); |
| 58 | + let conv = converter_from_schema(&schema); |
| 59 | + let container = sv |
| 60 | + .get_class( |
| 61 | + &linkml_schemaview::identifier::Identifier::new("Container"), |
| 62 | + &conv, |
| 63 | + ) |
| 64 | + .unwrap() |
| 65 | + .expect("class not found"); |
| 66 | + |
| 67 | + let doc_a = r#" |
| 68 | +objects: |
| 69 | + - objecttype: personinfo:Person |
| 70 | + id: "P:1" |
| 71 | + name: "Alice" |
| 72 | + has_employment_history: |
| 73 | + - started_at_time: 2019-01-01 |
| 74 | + is_current: true |
| 75 | + - started_at_time: 2020-01-01 |
| 76 | + is_current: false |
| 77 | +"#; |
| 78 | + let doc_b = r#" |
| 79 | +objects: |
| 80 | + - objecttype: personinfo:Person |
| 81 | + id: "P:1" |
| 82 | + name: "Alice" |
| 83 | + has_employment_history: |
| 84 | + - started_at_time: 2020-01-01 |
| 85 | + is_current: false |
| 86 | + - started_at_time: 2019-01-01 |
| 87 | + is_current: true |
| 88 | +"#; |
| 89 | + let v1 = load_yaml_str(doc_a, &sv, &container, &conv).unwrap(); |
| 90 | + let v2 = load_yaml_str(doc_b, &sv, &container, &conv).unwrap(); |
| 91 | + let p1 = v1.navigate_path(["objects", "0"]).unwrap(); |
| 92 | + let p2 = v2.navigate_path(["objects", "0"]).unwrap(); |
| 93 | + assert!(matches!(p1, LinkMLValue::Object { .. })); |
| 94 | + assert!(matches!(p2, LinkMLValue::Object { .. })); |
| 95 | + assert!(!p1.equals(p2), "List order must affect equality"); |
| 96 | +} |
| 97 | + |
| 98 | +#[test] |
| 99 | +fn mapping_equality_is_key_based_not_ordered() { |
| 100 | + // Load mapping schema and Bag class |
| 101 | + let schema = from_yaml(Path::new(&data_path("mapping_schema.yaml"))).unwrap(); |
| 102 | + let mut sv = SchemaView::new(); |
| 103 | + sv.add_schema(schema.clone()).unwrap(); |
| 104 | + let conv = converter_from_schema(&schema); |
| 105 | + let bag = sv |
| 106 | + .get_class( |
| 107 | + &linkml_schemaview::identifier::Identifier::new("Bag"), |
| 108 | + &conv, |
| 109 | + ) |
| 110 | + .unwrap() |
| 111 | + .expect("class not found"); |
| 112 | + |
| 113 | + let doc1 = r#"{ |
| 114 | + "things": { |
| 115 | + "alpha": {"typeURI": "ThingA", "a_only": "foo", "common": "shared"}, |
| 116 | + "beta": {"typeURI": "ThingB", "b_only": true, "common": "shared"} |
| 117 | + } |
| 118 | +}"#; |
| 119 | + let doc2 = r#"{ |
| 120 | + "things": { |
| 121 | + "beta": {"typeURI": "ThingB", "b_only": true, "common": "shared"}, |
| 122 | + "alpha": {"typeURI": "ThingA", "a_only": "foo", "common": "shared"} |
| 123 | + } |
| 124 | +}"#; |
| 125 | + let v1 = load_json_str(doc1, &sv, &bag, &conv).unwrap(); |
| 126 | + let v2 = load_json_str(doc2, &sv, &bag, &conv).unwrap(); |
| 127 | + let m1 = v1.navigate_path(["things"]).unwrap(); |
| 128 | + let m2 = v2.navigate_path(["things"]).unwrap(); |
| 129 | + assert!(matches!(m1, LinkMLValue::Mapping { .. })); |
| 130 | + assert!(matches!(m2, LinkMLValue::Mapping { .. })); |
| 131 | + assert!(m1.equals(m2), "Mapping equality should ignore key order"); |
| 132 | +} |
| 133 | + |
| 134 | +#[test] |
| 135 | +fn enum_scalar_equality_respects_value_and_range() { |
| 136 | + let schema = from_yaml(Path::new(&data_path("personinfo.yaml"))).unwrap(); |
| 137 | + let mut sv = SchemaView::new(); |
| 138 | + sv.add_schema(schema.clone()).unwrap(); |
| 139 | + let conv = converter_from_schema(&schema); |
| 140 | + let container = sv |
| 141 | + .get_class( |
| 142 | + &linkml_schemaview::identifier::Identifier::new("Container"), |
| 143 | + &conv, |
| 144 | + ) |
| 145 | + .unwrap() |
| 146 | + .expect("class not found"); |
| 147 | + |
| 148 | + let doc1 = r#" |
| 149 | +objects: |
| 150 | + - objecttype: personinfo:Person |
| 151 | + id: "P:1" |
| 152 | + name: "Alice" |
| 153 | + gender: "cisgender man" |
| 154 | +"#; |
| 155 | + let doc2 = r#" |
| 156 | +objects: |
| 157 | + - objecttype: personinfo:Person |
| 158 | + id: "P:2" |
| 159 | + name: "Bob" |
| 160 | + gender: "cisgender man" |
| 161 | +"#; |
| 162 | + let doc3 = r#" |
| 163 | +objects: |
| 164 | + - objecttype: personinfo:Person |
| 165 | + id: "P:3" |
| 166 | + name: "Carol" |
| 167 | + gender: "cisgender woman" |
| 168 | +"#; |
| 169 | + let v1 = load_yaml_str(doc1, &sv, &container, &conv).unwrap(); |
| 170 | + let v2 = load_yaml_str(doc2, &sv, &container, &conv).unwrap(); |
| 171 | + let v3 = load_yaml_str(doc3, &sv, &container, &conv).unwrap(); |
| 172 | + let g1 = v1.navigate_path(["objects", "0", "gender"]).unwrap(); |
| 173 | + let g2 = v2.navigate_path(["objects", "0", "gender"]).unwrap(); |
| 174 | + let g3 = v3.navigate_path(["objects", "0", "gender"]).unwrap(); |
| 175 | + assert!(g1.equals(g2)); |
| 176 | + assert!(!g1.equals(g3)); |
| 177 | +} |
0 commit comments