def test_optional_field(self): definition = { "fields": { "geographicFocus": { "where": "?subj vivo:geoGraphicFocus ?obj .", "optional": True } } } q = generate_sparql_construct(definition, "http://test/", "123") self.assertTrue("""CONSTRUCT { ?v0 :type ?v1 . ?v0 :geographicFocus ?v2 . } WHERE { BIND ((subj-ns:123) AS ?v0 ) { ?v0 vitro:mostSpecificType ?v1 . } OPTIONAL { ?v0 vivo:geoGraphicFocus ?v2 . } }""" in q)
def test_child_field(self): child_definition = { "where": "?subj a bibo:Document .", "fields": { "title": { "where": "?subj rdfs:label ?obj ." }, "issue": { "where": "?subj bibo:issue ?obj .", "optional": True } } } definition = { "fields": { "publications": { "where": """ ?subj vivo:relatedBy ?aship . ?aship a vivo:Authorship . ?aship vivo:relates ?obj . """, "definition": child_definition, "optional": True } } } q = generate_sparql_construct(definition, "http://test/", "123") self.assertTrue("""CONSTRUCT { ?v0 :type ?v1 . ?v0 :publications ?v2 . ?v2 :type ?v3 . ?v2 :issue ?v4 . ?v2 :title ?v5 . } WHERE { BIND ((subj-ns:123) AS ?v0 ) { ?v0 vitro:mostSpecificType ?v1 . } OPTIONAL { ?v0 vivo:relatedBy ?aship . ?aship a vivo:Authorship . ?aship vivo:relates ?v2 . ?v2 vitro:mostSpecificType ?v3 . ?v2 a bibo:Document . ?v2 rdfs:label ?v5 . OPTIONAL { ?v2 bibo:issue ?v4 . } } }""" in q)
def test_where(self): definition = { "where": "?subj a foaf:Person ." } q = generate_sparql_construct(definition, "http://test/", "123") self.assertTrue("""WHERE { BIND ((subj-ns:123) AS ?v0 ) { ?v0 a foaf:Person . ?v0 vitro:mostSpecificType ?v1 . } }""" in q)
def test_list_field(self): definition = { "fields": { "geographicFocus": { "where": "?subj vivo:geoGraphicFocus ?obj .", "list": True } } } q = generate_sparql_construct(definition, "http://test/", "123") self.assertTrue("""CONSTRUCT { ?v0 :type ?v1 . ?v0 :geographicFocus ?v2 . ?v0 :geographicFocus rdf:List . }""" in q)
def execute( definition, subject_namespace, subject_identifier, endpoint, username, password, serialization_format="json", indent_size=4, ): q = generate_sparql_construct(definition, subject_namespace, subject_identifier, indent_size=indent_size) log.debug("Construct query: %s", q) g = query(endpoint, username, password, q) s = transform_graph(g, subject_namespace + subject_identifier) o = _serialize(s, serialization_format, indent_size) return o, s, g, q
def test_field(self): definition = { "fields": { "name": { "where": "?subj rdfs:label ?obj ." } } } q = generate_sparql_construct(definition, "http://test/", "123") self.assertTrue("""CONSTRUCT { ?v0 :type ?v1 . ?v0 :name ?v2 . } WHERE { BIND ((subj-ns:123) AS ?v0 ) { ?v0 vitro:mostSpecificType ?v1 . ?v0 rdfs:label ?v2 . } }""" in q)