forked from terminusdb/terminusdb-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_woql_query_utils.py
More file actions
381 lines (273 loc) · 9.15 KB
/
test_woql_query_utils.py
File metadata and controls
381 lines (273 loc) · 9.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""Tests for WOQLQuery utility methods."""
from terminusdb_client.woqlquery.woql_query import WOQLQuery
def test_to_dict():
"""Test to_dict returns copy of query."""
query = WOQLQuery({"@type": "Triple", "subject": "v:X"})
result = query.to_dict()
assert result["@type"] == "Triple"
assert result["subject"] == "v:X"
# Should be a copy, not the same object
assert result is not query._query
def test_from_dict():
"""Test from_dict sets query from dict."""
query = WOQLQuery()
dictdata = {"@type": "And", "and": []}
result = query.from_dict(dictdata)
assert result is query # Returns self
assert query._query["@type"] == "And"
def test_find_last_subject_with_subject():
"""Test _find_last_subject finds subject in simple query."""
query = WOQLQuery()
json_obj = {"@type": "Triple", "subject": "v:X", "predicate": "v:P"}
result = query._find_last_subject(json_obj)
assert result == json_obj
def test_find_last_subject_in_and():
"""Test _find_last_subject finds subject in And clause."""
query = WOQLQuery()
triple = {"@type": "Triple", "subject": "v:Y"}
json_obj = {
"@type": "And",
"and": [
{"@type": "Other"},
triple
]
}
result = query._find_last_subject(json_obj)
assert result == triple
def test_find_last_subject_in_or():
"""Test _find_last_subject finds subject in Or clause."""
query = WOQLQuery()
triple = {"@type": "Triple", "subject": "v:Z"}
json_obj = {
"@type": "Or",
"or": [
{"@type": "Other"},
triple
]
}
result = query._find_last_subject(json_obj)
assert result == triple
def test_find_last_subject_in_nested_query():
"""Test _find_last_subject finds subject in nested query."""
query = WOQLQuery()
triple = {"@type": "Triple", "subject": "v:A"}
json_obj = {
"@type": "Select",
"query": triple
}
result = query._find_last_subject(json_obj)
assert result == triple
def test_find_last_subject_not_found():
"""Test _find_last_subject returns False when no subject found."""
query = WOQLQuery()
json_obj = {"@type": "Other", "data": "test"}
result = query._find_last_subject(json_obj)
assert result is False
def test_find_last_subject_reverse_order():
"""Test _find_last_subject searches in reverse order."""
query = WOQLQuery()
first = {"@type": "Triple", "subject": "v:First"}
last = {"@type": "Triple", "subject": "v:Last"}
json_obj = {
"@type": "And",
"and": [first, last]
}
result = query._find_last_subject(json_obj)
# Should find the last one
assert result == last
def test_find_last_property_with_object_property():
"""Test _find_last_property finds owl:ObjectProperty."""
query = WOQLQuery()
json_obj = {
"@type": "Triple",
"subject": "v:X",
"predicate": "rdf:type",
"object": "owl:ObjectProperty"
}
result = query._find_last_property(json_obj)
assert result == json_obj
def test_find_last_property_with_datatype_property():
"""Test _find_last_property finds owl:DatatypeProperty."""
query = WOQLQuery()
json_obj = {
"@type": "Triple",
"subject": "v:X",
"predicate": "rdf:type",
"object": "owl:DatatypeProperty"
}
result = query._find_last_property(json_obj)
assert result == json_obj
def test_find_last_property_with_domain():
"""Test _find_last_property finds rdfs:domain predicate."""
query = WOQLQuery()
json_obj = {
"@type": "Triple",
"subject": "v:X",
"predicate": "rdfs:domain",
"object": "v:Y"
}
result = query._find_last_property(json_obj)
assert result == json_obj
def test_find_last_property_with_range():
"""Test _find_last_property finds rdfs:range predicate."""
query = WOQLQuery()
json_obj = {
"@type": "Triple",
"subject": "v:X",
"predicate": "rdfs:range",
"object": "xsd:string"
}
result = query._find_last_property(json_obj)
assert result == json_obj
def test_find_last_property_in_and():
"""Test _find_last_property finds property in And clause."""
query = WOQLQuery()
prop_triple = {
"@type": "Triple",
"subject": "v:X",
"predicate": "rdfs:domain",
"object": "v:Y"
}
json_obj = {
"@type": "And",
"and": [
{"@type": "Other"},
prop_triple
]
}
result = query._find_last_property(json_obj)
assert result == prop_triple
def test_find_last_property_in_or():
"""Test _find_last_property finds property in Or clause."""
query = WOQLQuery()
prop_triple = {
"@type": "Triple",
"subject": "v:X",
"object": "owl:ObjectProperty"
}
json_obj = {
"@type": "Or",
"or": [
{"@type": "Other"},
prop_triple
]
}
result = query._find_last_property(json_obj)
assert result == prop_triple
def test_find_last_property_in_nested_query():
"""Test _find_last_property finds property in nested query."""
query = WOQLQuery()
prop_triple = {
"@type": "Triple",
"subject": "v:X",
"predicate": "rdfs:range",
"object": "v:Y"
}
json_obj = {
"@type": "Select",
"query": prop_triple
}
result = query._find_last_property(json_obj)
assert result == prop_triple
def test_find_last_property_not_found():
"""Test _find_last_property returns False when no property found."""
query = WOQLQuery()
json_obj = {
"@type": "Triple",
"subject": "v:X",
"predicate": "someOther:predicate",
"object": "v:Y"
}
result = query._find_last_property(json_obj)
assert result is False
def test_is_property_triple_object_property():
"""Test _is_property_triple with owl:ObjectProperty."""
query = WOQLQuery()
result = query._is_property_triple("rdf:type", "owl:ObjectProperty")
assert result is True
def test_is_property_triple_datatype_property():
"""Test _is_property_triple with owl:DatatypeProperty."""
query = WOQLQuery()
result = query._is_property_triple("rdf:type", "owl:DatatypeProperty")
assert result is True
def test_is_property_triple_domain():
"""Test _is_property_triple with rdfs:domain predicate."""
query = WOQLQuery()
result = query._is_property_triple("rdfs:domain", "v:Something")
assert result is True
def test_is_property_triple_range():
"""Test _is_property_triple with rdfs:range predicate."""
query = WOQLQuery()
result = query._is_property_triple("rdfs:range", "xsd:string")
assert result is True
def test_is_property_triple_false():
"""Test _is_property_triple returns False for non-property triple."""
query = WOQLQuery()
result = query._is_property_triple("rdf:type", "owl:Class")
assert result is False
def test_is_property_triple_with_dict_pred():
"""Test _is_property_triple with dict predicate."""
query = WOQLQuery()
pred = {"@type": "NodeValue", "node": "rdfs:domain"}
result = query._is_property_triple(pred, "v:X")
assert result is True
def test_is_property_triple_with_dict_obj():
"""Test _is_property_triple with dict object."""
query = WOQLQuery()
obj = {"@type": "NodeValue", "node": "owl:ObjectProperty"}
result = query._is_property_triple("rdf:type", obj)
assert result is True
def test_is_property_triple_with_both_dicts():
"""Test _is_property_triple with both dict pred and obj."""
query = WOQLQuery()
pred = {"@type": "NodeValue", "node": "rdfs:range"}
obj = {"@type": "NodeValue", "node": "xsd:string"}
result = query._is_property_triple(pred, obj)
assert result is True
def test_is_property_triple_with_dict_no_node():
"""Test _is_property_triple with dict without node."""
query = WOQLQuery()
pred = {"@type": "NodeValue", "variable": "P"}
obj = {"@type": "NodeValue", "variable": "O"}
result = query._is_property_triple(pred, obj)
# Neither has 'node', so extracts None
assert result is False
def test_find_last_subject_deeply_nested():
"""Test _find_last_subject with deep nesting."""
query = WOQLQuery()
triple = {"@type": "Triple", "subject": "v:Deep"}
json_obj = {
"@type": "And",
"and": [
{
"@type": "Or",
"or": [
{"@type": "Select", "query": triple}
]
}
]
}
result = query._find_last_subject(json_obj)
assert result == triple
def test_find_last_property_deeply_nested():
"""Test _find_last_property with deep nesting."""
query = WOQLQuery()
triple = {
"@type": "Triple",
"subject": "v:X",
"predicate": "rdfs:domain",
"object": "v:Y"
}
json_obj = {
"@type": "And",
"and": [
{
"@type": "Or",
"or": [
{"@type": "Select", "query": triple}
]
}
]
}
result = query._find_last_property(json_obj)
assert result == triple