Skip to content

Commit c39b659

Browse files
author
Matthijs van Otterdijk
authored
Merge pull request #345 from terminusdb/triples
Restore triples functions
2 parents 280a689 + b9b01a1 commit c39b659

File tree

2 files changed

+101
-27
lines changed

2 files changed

+101
-27
lines changed

terminusdb_client/client/Client.py

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -788,10 +788,6 @@ def get_triples(self, graph_type: str) -> str:
788788
-------
789789
str
790790
"""
791-
792-
### TODO: make triples works again
793-
raise InterfaceError("get_triples is temporary not avaliable in this version")
794-
795791
self._check_connection()
796792
self._validate_graph_type(graph_type)
797793
result = requests.get(
@@ -801,15 +797,16 @@ def get_triples(self, graph_type: str) -> str:
801797
)
802798
return json.loads(_finish_response(result))
803799

804-
def update_triples(self, graph_type: str, turtle, commit_msg: str) -> None:
805-
"""Updates the contents of the specified graph with the triples encoded in turtle format Replaces the entire graph contents
800+
def update_triples(self, graph_type: str, content: str, commit_msg: str) -> None:
801+
"""Updates the contents of the specified graph with the triples encoded in turtle format.
802+
Replaces the entire graph contents
806803
807804
Parameters
808805
----------
809806
graph_type : str
810807
Graph type, either "instance" or "schema".
811-
turtle
812-
Valid set of triples in Turtle format.
808+
content
809+
Valid set of triples in Turtle or Trig format.
813810
commit_msg : str
814811
Commit message.
815812
@@ -818,34 +815,30 @@ def update_triples(self, graph_type: str, turtle, commit_msg: str) -> None:
818815
InterfaceError
819816
if the client does not connect to a database
820817
"""
821-
### TODO: make triples works again
822-
raise InterfaceError(
823-
"update_triples is temporary not avaliable in this version"
824-
)
825-
826818
self._check_connection()
827819
self._validate_graph_type(graph_type)
828-
params = {"commit_info": self._generate_commit(commit_msg)}
829-
params["turtle"] = turtle
820+
params = {"commit_info": self._generate_commit(commit_msg),
821+
"turtle": content,
822+
}
830823
result = requests.post(
831824
self._triples_url(graph_type),
832825
headers=self._default_headers,
833-
params=params,
826+
json=params,
834827
auth=self._auth(),
835828
)
836829
return json.loads(_finish_response(result))
837830

838831
def insert_triples(
839-
self, graph_type: str, turtle, commit_msg: Optional[str] = None
832+
self, graph_type: str, content: str, commit_msg: Optional[str] = None
840833
) -> None:
841834
"""Inserts into the specified graph with the triples encoded in turtle format.
842835
843836
Parameters
844837
----------
845838
graph_type : str
846839
Graph type, either "instance" or "schema".
847-
turtle
848-
Valid set of triples in Turtle format.
840+
content
841+
Valid set of triples in Turtle or Trig format.
849842
commit_msg : str
850843
Commit message.
851844
@@ -854,19 +847,15 @@ def insert_triples(
854847
InterfaceError
855848
if the client does not connect to a database
856849
"""
857-
### TODO: make triples works again
858-
raise InterfaceError(
859-
"insert_triples is temporary not avaliable in this version"
860-
)
861-
862850
self._check_connection()
863851
self._validate_graph_type(graph_type)
864-
params = {"commit_info": self._generate_commit(commit_msg)}
865-
params["turtle"] = turtle
852+
params = {"commit_info": self._generate_commit(commit_msg),
853+
"turtle": content
854+
}
866855
result = requests.put(
867856
self._triples_url(graph_type),
868857
headers=self._default_headers,
869-
params=params,
858+
json=params,
870859
auth=self._auth(),
871860
)
872861
return json.loads(_finish_response(result))

terminusdb_client/tests/integration_tests/test_client.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,91 @@ def test_log(docker_url):
166166
assert log[0]['@type'] == 'InitialCommit'
167167

168168

169+
def test_get_triples(docker_url):
170+
client = Client(docker_url, user_agent=test_user_agent, team="admin")
171+
client.connect()
172+
db_name = "testDB" + str(random())
173+
client.create_database(db_name, team="admin")
174+
client.connect(db=db_name)
175+
# Add a philosopher schema
176+
schema = {"@type": "Class",
177+
"@id": "Philosopher",
178+
"name": "xsd:string"
179+
}
180+
# Add schema and Socrates
181+
client.insert_document(schema, graph_type="schema")
182+
schema_triples = client.get_triples(graph_type='schema')
183+
assert "<schema#Philosopher>\n a sys:Class ;\n <schema#name> xsd:string ." in schema_triples
184+
185+
186+
def test_update_triples(docker_url):
187+
ttl = """
188+
@base <terminusdb:///schema#> .
189+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
190+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
191+
@prefix woql: <http://terminusdb.com/schema/woql#> .
192+
@prefix json: <http://terminusdb.com/schema/json#> .
193+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
194+
@prefix xdd: <http://terminusdb.com/schema/xdd#> .
195+
@prefix vio: <http://terminusdb.com/schema/vio#> .
196+
@prefix sys: <http://terminusdb.com/schema/sys#> .
197+
@prefix api: <http://terminusdb.com/schema/api#> .
198+
@prefix owl: <http://www.w3.org/2002/07/owl#> .
199+
@prefix doc: <data/> .
200+
201+
<schema#Philosopher>
202+
a sys:Class ;
203+
<schema#name> xsd:string .
204+
205+
<terminusdb://context>
206+
a sys:Context ;
207+
sys:base "terminusdb:///data/"^^xsd:string ;
208+
sys:schema "terminusdb:///schema#"^^xsd:string .
209+
"""
210+
client = Client(docker_url, user_agent=test_user_agent, team="admin")
211+
client.connect()
212+
db_name = "testDB" + str(random())
213+
client.create_database(db_name, team="admin")
214+
client.connect(db=db_name)
215+
client.update_triples(graph_type='schema', content=ttl, commit_msg="Update triples")
216+
client.insert_document({"name": "Socrates"})
217+
assert len(list(client.get_all_documents())) == 1
218+
219+
220+
def test_insert_triples(docker_url):
221+
ttl = """
222+
@base <terminusdb:///schema#> .
223+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
224+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
225+
@prefix woql: <http://terminusdb.com/schema/woql#> .
226+
@prefix json: <http://terminusdb.com/schema/json#> .
227+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
228+
@prefix xdd: <http://terminusdb.com/schema/xdd#> .
229+
@prefix vio: <http://terminusdb.com/schema/vio#> .
230+
@prefix sys: <http://terminusdb.com/schema/sys#> .
231+
@prefix api: <http://terminusdb.com/schema/api#> .
232+
@prefix owl: <http://www.w3.org/2002/07/owl#> .
233+
@prefix doc: <data/> .
234+
235+
<schema#Philosopher>
236+
a sys:Class ;
237+
<schema#name> xsd:string .
238+
239+
<terminusdb://context>
240+
a sys:Context ;
241+
sys:base "terminusdb:///data/"^^xsd:string ;
242+
sys:schema "terminusdb:///schema#"^^xsd:string .
243+
"""
244+
client = Client(docker_url, user_agent=test_user_agent, team="admin")
245+
client.connect()
246+
db_name = "testDB" + str(random())
247+
client.create_database(db_name, team="admin")
248+
client.connect(db=db_name)
249+
client.insert_triples(graph_type='schema', content=ttl, commit_msg="Insert triples")
250+
client.insert_document({"name": "Socrates"})
251+
assert len(list(client.get_all_documents())) == 1
252+
253+
169254
def test_get_database(docker_url):
170255
client = Client(docker_url, user_agent=test_user_agent, team="admin")
171256
client.connect()

0 commit comments

Comments
 (0)