|
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 |
|
10 | | -from openminds.base import Node, IRI, Link |
| 10 | +from openminds.base import Node, IRI, Link, LinkedNodeEmbedding |
11 | 11 | from utils import build_fake_node |
12 | 12 |
|
13 | 13 | module_names = ( |
@@ -114,8 +114,74 @@ def test_link(): |
114 | 114 | } |
115 | 115 | assert my_dsv1.to_jsonld( |
116 | 116 | include_empty_properties=False, |
117 | | - embed_linked_nodes=False |
| 117 | + embed_linked_nodes=LinkedNodeEmbedding.NEVER |
118 | 118 | ) == my_dsv2.to_jsonld( |
119 | 119 | include_empty_properties=False, |
120 | | - embed_linked_nodes=False |
| 120 | + embed_linked_nodes=LinkedNodeEmbedding.NEVER |
121 | 121 | ) == expected |
| 122 | + |
| 123 | + |
| 124 | +def test_linked_node_embedding(): |
| 125 | + from openminds.v4.core import Organization, Person |
| 126 | + from openminds.v4.core.actors.affiliation import Affiliation |
| 127 | + |
| 128 | + uni = Organization(full_name="University of Somewhere", id="_:001") |
| 129 | + person_with_id = Person( |
| 130 | + given_name="Ada", |
| 131 | + family_name="Lovelace", |
| 132 | + id="_:002", |
| 133 | + affiliations=[Affiliation(member_of=uni)], |
| 134 | + ) |
| 135 | + person_without_id = Person( |
| 136 | + given_name="Ada", |
| 137 | + family_name="Lovelace", |
| 138 | + affiliations=[Affiliation(member_of=uni)], |
| 139 | + ) |
| 140 | + |
| 141 | + # ALWAYS: linked nodes are embedded inline |
| 142 | + result = person_with_id.to_jsonld( |
| 143 | + include_empty_properties=False, |
| 144 | + embed_linked_nodes=LinkedNodeEmbedding.ALWAYS, |
| 145 | + ) |
| 146 | + affiliation = result["affiliation"][0] |
| 147 | + assert affiliation["memberOf"]["@type"] == "https://openminds.om-i.org/types/Organization" |
| 148 | + assert affiliation["memberOf"]["fullName"] == "University of Somewhere" |
| 149 | + |
| 150 | + # NEVER: linked nodes with id are replaced by {"@id": ...} |
| 151 | + result = person_with_id.to_jsonld( |
| 152 | + include_empty_properties=False, |
| 153 | + embed_linked_nodes=LinkedNodeEmbedding.NEVER, |
| 154 | + ) |
| 155 | + affiliation = result["affiliation"][0] |
| 156 | + assert affiliation["memberOf"] == {"@id": "_:001"} |
| 157 | + |
| 158 | + # NEVER: raises ValueError when a linked node has no id |
| 159 | + uni_no_id = Organization(full_name="University of Nowhere") |
| 160 | + person_with_unidentified_org = Person( |
| 161 | + given_name="Ada", |
| 162 | + family_name="Lovelace", |
| 163 | + id="_:003", |
| 164 | + affiliations=[Affiliation(member_of=uni_no_id)], |
| 165 | + ) |
| 166 | + with pytest.raises(ValueError, match="requires @id to be defined"): |
| 167 | + person_with_unidentified_org.to_jsonld( |
| 168 | + include_empty_properties=False, |
| 169 | + embed_linked_nodes=LinkedNodeEmbedding.NEVER, |
| 170 | + ) |
| 171 | + |
| 172 | + # IF_NECESSARY: linked nodes with id are replaced by {"@id": ...} |
| 173 | + result = person_with_id.to_jsonld( |
| 174 | + include_empty_properties=False, |
| 175 | + embed_linked_nodes=LinkedNodeEmbedding.IF_NECESSARY, |
| 176 | + ) |
| 177 | + affiliation = result["affiliation"][0] |
| 178 | + assert affiliation["memberOf"] == {"@id": "_:001"} |
| 179 | + |
| 180 | + # IF_NECESSARY: linked nodes without id are embedded inline |
| 181 | + result = person_with_unidentified_org.to_jsonld( |
| 182 | + include_empty_properties=False, |
| 183 | + embed_linked_nodes=LinkedNodeEmbedding.IF_NECESSARY, |
| 184 | + ) |
| 185 | + affiliation = result["affiliation"][0] |
| 186 | + assert affiliation["memberOf"]["@type"] == "https://openminds.om-i.org/types/Organization" |
| 187 | + assert affiliation["memberOf"]["fullName"] == "University of Nowhere" |
0 commit comments