def test_to_resource(nexus_store, registered_building, building_jsonld): context = _merge_jsonld(registered_building.context, NEXUS_CONTEXT) payload = building_jsonld(registered_building, "compacted", True, None) payload["@context"] = context result = nexus_store.service.to_resource(payload) assert str(result) == str(registered_building) assert getattr(result, "context") == registered_building.context assert str(result._store_metadata) == str(registered_building._store_metadata)
def test_load_context_from_list(custom_context, context_iri_file, model_prefixes): test_context = _merge_jsonld(custom_context, context_iri_file) context = Context(test_context) assert is_valid_document(context.document) assert context.iri is None assert context.is_http_iri() is False assert context.base == "http://example.org/" assert context.vocab == "http://example.org/vocab/" assert context.expand("Person") == "http://schema.org/Person" assert context.prefixes == model_prefixes
def _make_jsonld_compacted(resource, store_metadata, store_metadata_data, metadata_context): data = dict() if hasattr(resource, "id"): data["@id"] = resource.id if store_metadata: data["@context"] = _merge_jsonld(resource.context, metadata_context.iri) else: data["@context"] = resource.context data.update({ "@type": resource.type, "@id": resource.id, "name": resource.name }) if hasattr(resource, "id") and store_metadata: data.update(store_metadata_data) return data
def _make_jsonld_compacted(resource, store_metadata, context): data = dict() data_context = resource.context if hasattr(resource, "context") else context if store_metadata and resource._store_metadata is not None: metadata_context_output = metadata_context.iri if metadata_context.is_http_iri() else metadata_context.document["@context"] data["@context"] = _merge_jsonld(data_context, metadata_context_output) else: data["@context"] = data_context if hasattr(resource, "id"): data["@id"] = resource.id data.update({ "@type": resource.type, "description": resource.description, "geo": resource.geo, "image": resource.image, "name": resource.name }) if store_metadata and resource._store_metadata is not None: data.update(metadata_data_compacted) return data
def test_merge_jsonld(self): str_ctx = "string_context" str_ctx2 = "string_context2" dict_ctx = {"a": 1, "b": 2} dict_ctx2 = {"c": 3, "d": 4} list_ctx = [str_ctx, dict_ctx] list_ctx2 = [str_ctx2, dict_ctx2] dict_ctx3 = {"a": 1, "b": 2, "c": 3, "d": 4} assert _merge_jsonld(str_ctx, str_ctx2) == [str_ctx, str_ctx2] assert _merge_jsonld(str_ctx, dict_ctx) == [str_ctx, dict_ctx] assert _merge_jsonld(str_ctx2, list_ctx) == [str_ctx2, str_ctx, dict_ctx] assert _merge_jsonld(list_ctx, str_ctx2) == [str_ctx, dict_ctx, str_ctx2] assert _merge_jsonld(list_ctx, dict_ctx2) == [str_ctx, dict_ctx3] assert _merge_jsonld(list_ctx, list_ctx2) == [str_ctx, dict_ctx3, str_ctx2] assert _merge_jsonld(dict_ctx, str_ctx) == [dict_ctx, str_ctx] assert _merge_jsonld(dict_ctx, list_ctx2) == [dict_ctx3, str_ctx2] assert _merge_jsonld(dict_ctx, dict_ctx2) == dict_ctx3 assert _merge_jsonld(str_ctx, str_ctx) == str_ctx assert _merge_jsonld(dict_ctx, dict_ctx) == dict_ctx assert _merge_jsonld(list_ctx, list_ctx) == list_ctx assert _merge_jsonld(list_ctx, None) == list_ctx