You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The question, how to (best) mock SPARQLWrapper objects probably comes up for most projects using SPARQLWrapper at some point. So having a generic mocking facility as part of the SPARQLWrapper library would be a real asset.
Unfortunately, since SPARQLWrapper does not use requests, mocking SPARQLWrapper is not entirely straight forward.
Here is a quick draft of a solution I came up with:
The main idea of this is to have the SPARQLWrapper.query method target a local rdflib.Graph instance instead of an actual remote triplestore and apply the respective SPARQLWrapper patch within a context manager.
A simple usage example would be:
fromSPARQLWrapperimportSPARQLWrapperfromrdflibimportGraphfromsparqlwrapper_mock_draft.latestimportSPARQLWrapperLocalTargetdata="""BASE <http://example.org/>PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX rel: <http://www.perceive.net/schemas/relationship/><#green-goblin> rel:enemyOf <#spiderman> ; a foaf:Person ; # in the context of the Marvel universe foaf:name "Green Goblin" .<#spiderman> rel:enemyOf <#green-goblin> ; a foaf:Person ; foaf:name "Spiderman" ."""graph=Graph().parse(data=data, format="ttl")
defcode_unter_test():
s=SPARQLWrapper("some.endpoint")
s.setReturnFormat("json")
s.setQuery("select ?s ?p ?o where {?s ?p ?o .}")
print(s.queryAndConvert())
withSPARQLWrapperLocalTarget(graph):
code_unter_test()
This is not at all tested and probably needs some polishing, but might be a good starting point for a discussion about mocking SPARQLWrapper.
One very basic question would be: Should a mocking facility even be part of SPARQLWrapper or is mocking considered more of an external responsibility?
The text was updated successfully, but these errors were encountered:
The question, how to (best) mock
SPARQLWrapper
objects probably comes up for most projects using SPARQLWrapper at some point. So having a generic mocking facility as part of the SPARQLWrapper library would be a real asset.Unfortunately, since SPARQLWrapper does not use
requests
, mocking SPARQLWrapper is not entirely straight forward.Here is a quick draft of a solution I came up with:
The main idea of this is to have the
SPARQLWrapper.query
method target a localrdflib.Graph
instance instead of an actual remote triplestore and apply the respectiveSPARQLWrapper
patch within a context manager.A simple usage example would be:
This is not at all tested and probably needs some polishing, but might be a good starting point for a discussion about mocking SPARQLWrapper.
One very basic question would be: Should a mocking facility even be part of SPARQLWrapper or is mocking considered more of an external responsibility?
The text was updated successfully, but these errors were encountered: