Ejemplo n.º 1
0
    def test_from_graph(self, building, organization, building_jsonld, model_context, store_metadata, metadata_context):
        store_metadata = False
        id = "http://test/1234"
        id_uri = term.URIRef(id)
        graph = Graph()
        graph.add((id_uri, RDF.type, term.URIRef("http://schema.org/Building")))
        graph.add((id_uri,term.URIRef("http://schema.org/name"),term.Literal("The Empire State Building")))
        graph.add((id_uri,term.URIRef("http://schema.org/description"),term.Literal("The Empire State Building is a 102-story landmark in New York City.")))
        graph.add((id_uri,term.URIRef("http://schema.org/image"),term.URIRef("http://www.civil.usherbrooke.ca/cours/gci215a/empire-state-building.jpg")))
        bNode = term.BNode()
        graph.add((id_uri,term.URIRef("http://schema.org/geo"),bNode))
        graph.add((bNode,term.URIRef("http://schema.org/latitude"),term.Literal("40.75")))
        results = from_graph(graph)

        assert isinstance(results, Resource)
        building.id = id
        building.context = model_context.document["@context"]
        expected = building_jsonld(building, "expanded", store_metadata, None)
        result_jsonld = as_jsonld(results, form="expanded", store_metadata=store_metadata,
                  model_context=model_context, metadata_context=metadata_context,
                  context_resolver=None, na=None)
        assert result_jsonld == expected

        graph.remove((id_uri, RDF.type, term.URIRef("http://schema.org/Building")))
        results = from_graph(graph)
        assert len(results) == 3

        graph.add((id_uri, RDF.type, term.URIRef("http://schema.org/Building")))
        graph.add((term.URIRef("http://www.civil.usherbrooke.ca/cours/gci215a/empire-state-building.jpg"), RDF.type, term.URIRef("http://schema.org/Image")))
        results = from_graph(graph, type=["http://schema.org/Building","http://schema.org/Image"])
        assert len(results) == 2
        assert results[0].type is not None
        assert results[1].type is not None

        result_0 = as_jsonld(results[0], form="expanded", store_metadata=store_metadata,
                  model_context=model_context, metadata_context=metadata_context,
                  context_resolver=None, na=None)

        result_1 = as_jsonld(results[1], form="expanded", store_metadata=store_metadata,
                             model_context=model_context, metadata_context=metadata_context,
                             context_resolver=None, na=None)
        results = [result_0, result_1]
        assert set(["http://schema.org/Building","http://schema.org/Image"]) == {result["@type"] for result in results}

        frame = {
            "@type": ['http://schema.org/Image'],
            "@embed": True
        }
        results = from_graph(graph, frame=frame)
        assert isinstance(results, Resource)
        expected = {'@type': 'http://schema.org/Image', '@id': 'http://www.civil.usherbrooke.ca/cours/gci215a/empire-state-building.jpg'}
        result_jsonld = as_jsonld(results, form="expanded", store_metadata=store_metadata,
                         model_context=model_context, metadata_context=metadata_context,
                         context_resolver=None, na=None)
        assert result_jsonld == expected
Ejemplo n.º 2
0
 def from_graph(self,
                data: Graph,
                type: Union[str, List[str]] = None,
                frame: Dict = None,
                use_model_context=False) -> Union[Resource, List[Resource]]:
     context = self._model.context() if use_model_context else None
     return from_graph(data, type, frame, context)
Ejemplo n.º 3
0
 def from_graph(self, data: Graph) -> Union[Resource, List[Resource]]:
     return from_graph(data)