Skip to content

Commit bdeaef5

Browse files
add WOQLQuery.date (#369)
* add WOQLQuery.date Co-authored-by: Francesca-Bit <francesca@terminusdb.com> Co-authored-by: Robin de Rooij <rderooij685@gmail.com>
1 parent 1d04acc commit bdeaef5

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

terminusdb_client/tests/test_woqlQuery.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pprint
2+
import datetime as dt
23

34
from terminusdb_client.woqlquery.woql_query import WOQLQuery
45

@@ -247,6 +248,23 @@ def test_cast_method_literal(self):
247248
)
248249
assert woql_object1.to_dict() == woql_object2.to_dict()
249250

251+
def test_cast_method_datetime(self):
252+
value = dt.datetime(2022, 10, 19, 14, 17, 12)
253+
json_datetime = WOQLQuery().datetime(value)
254+
json_datetime2 = WOQLQuery().literal("2022-10-19T14:17:12", "dateTime")
255+
assert json_datetime == json_datetime2
256+
257+
def test_cast_method_date(self):
258+
value = dt.date(2022, 10, 19)
259+
json_datetime = WOQLQuery().date(value)
260+
json_datetime2 = WOQLQuery().literal("2022-10-19", "date")
261+
assert json_datetime == json_datetime2
262+
263+
def test_cast_method_boolean(self):
264+
json_datetime = WOQLQuery().boolean(True)
265+
json_datetime2 = WOQLQuery().literal(True, "boolean")
266+
assert json_datetime == json_datetime2
267+
250268
def test_cast_method_object(self):
251269
woql_object1 = WOQLQuery().cast(
252270
"my_int", "xsd:integer", "v:Duration_Cast", literal_type="owl:Thing"

terminusdb_client/woqlquery/woql_query.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ def datetime(self, input_obj):
11531153
Parameters
11541154
----------
11551155
input_obj : str
1156-
the given input dateTime object
1156+
the given input dateTime object or a datetime string format YYYY-MM-DDThh-mm-ssZ
11571157
11581158
Returns
11591159
-------
@@ -1166,6 +1166,25 @@ def datetime(self, input_obj):
11661166
else:
11671167
raise ValueError("Input need to be either string or a datetime object.")
11681168

1169+
def date(self, input_obj):
1170+
"""Transforms the given date object into the proper json-ld form
1171+
1172+
Parameters
1173+
----------
1174+
input_obj : str
1175+
the given input date object or a date string format YYYY-MM-DD
1176+
1177+
Returns
1178+
-------
1179+
dict
1180+
"""
1181+
if isinstance(input_obj, dt.date):
1182+
return {"@type": "xsd:date", "@value": input_obj.isoformat()}
1183+
elif isinstance(input_obj, str):
1184+
return {"@type": "xsd:date", "@value": input_obj}
1185+
else:
1186+
raise ValueError("Input need to be either string or a date object.")
1187+
11691188
def literal(self, input_val, input_type):
11701189
if ":" not in input_type:
11711190
input_type = "xsd:" + input_type

0 commit comments

Comments
 (0)