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_cleaners.py
More file actions
659 lines (409 loc) · 16.6 KB
/
test_woql_query_cleaners.py
File metadata and controls
659 lines (409 loc) · 16.6 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
"""Tests for WOQLQuery cleaning and expansion methods."""
import datetime
from terminusdb_client.woqlquery.woql_query import WOQLQuery, Var, Doc
def test_clean_subject_with_string():
"""Test _clean_subject with plain string."""
query = WOQLQuery()
result = query._clean_subject("testSubject")
assert result["@type"] == "NodeValue"
assert result["node"] == "testSubject"
def test_clean_subject_with_colon():
"""Test _clean_subject with URI string containing colon."""
query = WOQLQuery()
result = query._clean_subject("doc:Person")
assert result["@type"] == "NodeValue"
assert result["node"] == "doc:Person"
def test_clean_subject_with_vocab():
"""Test _clean_subject uses vocab mapping."""
query = WOQLQuery()
result = query._clean_subject("type")
# "type" maps to "rdf:type" in SHORT_NAME_MAPPING
assert result["@type"] == "NodeValue"
assert result["node"] == "rdf:type"
def test_clean_subject_with_var():
"""Test _clean_subject with Var object."""
query = WOQLQuery()
var = Var("Subject")
result = query._clean_subject(var)
assert result["@type"] == "NodeValue"
assert result["variable"] == "Subject"
def test_clean_subject_with_dict():
"""Test _clean_subject returns dict as-is."""
query = WOQLQuery()
input_dict = {"@type": "NodeValue", "node": "test"}
result = query._clean_subject(input_dict)
assert result == input_dict
def test_clean_subject_with_invalid_type():
"""Test _clean_subject raises ValueError for invalid type."""
query = WOQLQuery()
try:
query._clean_subject(123)
assert False, "Should have raised ValueError"
except ValueError as e:
assert "Subject must be a URI string" in str(e)
def test_clean_predicate_with_string():
"""Test _clean_predicate with plain string."""
query = WOQLQuery()
result = query._clean_predicate("testPredicate")
assert result["@type"] == "NodeValue"
assert result["node"] == "testPredicate"
def test_clean_predicate_with_colon():
"""Test _clean_predicate with URI containing colon."""
query = WOQLQuery()
result = query._clean_predicate("rdf:type")
assert result["@type"] == "NodeValue"
assert result["node"] == "rdf:type"
def test_clean_predicate_with_vocab():
"""Test _clean_predicate uses vocab mapping."""
query = WOQLQuery()
result = query._clean_predicate("label")
# "label" maps to "rdfs:label"
assert result["@type"] == "NodeValue"
assert result["node"] == "rdfs:label"
def test_clean_predicate_with_var():
"""Test _clean_predicate with Var object."""
query = WOQLQuery()
var = Var("Predicate")
result = query._clean_predicate(var)
assert result["@type"] == "NodeValue"
assert result["variable"] == "Predicate"
def test_clean_predicate_with_dict():
"""Test _clean_predicate returns dict as-is."""
query = WOQLQuery()
input_dict = {"@type": "NodeValue", "node": "test"}
result = query._clean_predicate(input_dict)
assert result == input_dict
def test_clean_predicate_with_invalid_type():
"""Test _clean_predicate raises ValueError for invalid type."""
query = WOQLQuery()
try:
query._clean_predicate(123)
assert False, "Should have raised ValueError"
except ValueError as e:
assert "Predicate must be a URI string" in str(e)
def test_clean_path_predicate_with_colon():
"""Test _clean_path_predicate with URI containing colon."""
query = WOQLQuery()
result = query._clean_path_predicate("doc:parent")
assert result == "doc:parent"
def test_clean_path_predicate_with_vocab():
"""Test _clean_path_predicate uses vocab mapping."""
query = WOQLQuery()
result = query._clean_path_predicate("type")
assert result == "rdf:type"
def test_clean_path_predicate_with_plain_string():
"""Test _clean_path_predicate with plain string."""
query = WOQLQuery()
result = query._clean_path_predicate("customPredicate")
assert result == "customPredicate"
def test_clean_path_predicate_with_none():
"""Test _clean_path_predicate returns False for None."""
query = WOQLQuery()
result = query._clean_path_predicate(None)
assert result is False
def test_clean_object_with_string():
"""Test _clean_object with plain string."""
query = WOQLQuery()
result = query._clean_object("testObject")
assert result["@type"] == "Value"
assert result["node"] == "testObject"
def test_clean_object_with_v_prefix():
"""Test _clean_object with v: prefixed variable."""
query = WOQLQuery()
result = query._clean_object("v:MyVar")
assert result["@type"] == "Value"
assert result["variable"] == "MyVar"
def test_clean_object_with_var():
"""Test _clean_object with Var object."""
query = WOQLQuery()
var = Var("Object")
result = query._clean_object(var)
assert result["@type"] == "Value"
assert result["variable"] == "Object"
def test_clean_object_with_doc():
"""Test _clean_object with Doc object."""
query = WOQLQuery()
doc = Doc({"name": "Alice"})
result = query._clean_object(doc)
# Should return the encoded version
assert "@type" in result
def test_clean_object_with_int():
"""Test _clean_object with integer."""
query = WOQLQuery()
result = query._clean_object(42)
assert result["@type"] == "Value"
assert result["data"]["@type"] == "xsd:integer"
assert result["data"]["@value"] == 42
def test_clean_object_with_float():
"""Test _clean_object with float."""
query = WOQLQuery()
result = query._clean_object(3.14)
assert result["@type"] == "Value"
assert result["data"]["@type"] == "xsd:decimal"
assert result["data"]["@value"] == 3.14
def test_clean_object_with_bool():
"""Test _clean_object with boolean."""
query = WOQLQuery()
result = query._clean_object(True)
assert result["@type"] == "Value"
assert result["data"]["@type"] == "xsd:boolean"
assert result["data"]["@value"] is True
def test_clean_object_with_date():
"""Test _clean_object with datetime."""
query = WOQLQuery()
dt = datetime.datetime(2025, 1, 1, 12, 0, 0)
result = query._clean_object(dt)
assert result["@type"] == "Value"
assert result["data"]["@type"] == "xsd:dateTime"
assert result["data"]["@value"] == "2025-01-01T12:00:00"
def test_clean_object_with_dict_value():
"""Test _clean_object with dict containing @value."""
query = WOQLQuery()
input_dict = {"@type": "xsd:string", "@value": "test"}
result = query._clean_object(input_dict)
assert result["@type"] == "Value"
assert result["data"] == input_dict
def test_clean_object_with_plain_dict():
"""Test _clean_object with dict without @value."""
query = WOQLQuery()
input_dict = {"@type": "Custom", "node": "test"}
result = query._clean_object(input_dict)
assert result == input_dict
def test_clean_object_with_list():
"""Test _clean_object with list."""
query = WOQLQuery()
result = query._clean_object([1, "test", True])
assert isinstance(result, list)
assert len(result) == 3
def test_clean_object_with_custom_target():
"""Test _clean_object with custom target type."""
query = WOQLQuery()
result = query._clean_object(42, "xsd:long")
assert result["data"]["@type"] == "xsd:long"
def test_clean_data_value_with_string():
"""Test _clean_data_value with string."""
query = WOQLQuery()
result = query._clean_data_value("test")
assert result["@type"] == "DataValue"
assert result["data"]["@type"] == "xsd:string"
assert result["data"]["@value"] == "test"
def test_clean_data_value_with_v_prefix():
"""Test _clean_data_value with v: prefixed variable."""
query = WOQLQuery()
result = query._clean_data_value("v:MyVar")
assert result["@type"] == "DataValue"
assert result["variable"] == "MyVar"
def test_clean_data_value_with_var():
"""Test _clean_data_value with Var object."""
query = WOQLQuery()
var = Var("Data")
result = query._clean_data_value(var)
assert result["@type"] == "DataValue"
assert result["variable"] == "Data"
def test_clean_data_value_with_int():
"""Test _clean_data_value with integer."""
query = WOQLQuery()
result = query._clean_data_value(100)
assert result["@type"] == "DataValue"
assert result["data"]["@type"] == "xsd:integer"
assert result["data"]["@value"] == 100
def test_clean_data_value_with_float():
"""Test _clean_data_value with float."""
query = WOQLQuery()
result = query._clean_data_value(2.5)
assert result["@type"] == "DataValue"
assert result["data"]["@type"] == "xsd:decimal"
assert result["data"]["@value"] == 2.5
def test_clean_data_value_with_bool():
"""Test _clean_data_value with boolean."""
query = WOQLQuery()
result = query._clean_data_value(False)
assert result["@type"] == "DataValue"
assert result["data"]["@type"] == "xsd:boolean"
assert result["data"]["@value"] is False
def test_clean_data_value_with_date():
"""Test _clean_data_value with datetime."""
query = WOQLQuery()
dt = datetime.date(2025, 1, 1)
result = query._clean_data_value(dt)
assert result["@type"] == "DataValue"
assert result["data"]["@type"] == "xsd:dateTime"
assert "2025-01-01" in result["data"]["@value"]
def test_clean_data_value_with_dict_value():
"""Test _clean_data_value with dict containing @value."""
query = WOQLQuery()
input_dict = {"@type": "xsd:decimal", "@value": 3.14}
result = query._clean_data_value(input_dict)
assert result["@type"] == "DataValue"
assert result["data"] == input_dict
def test_clean_data_value_with_plain_dict():
"""Test _clean_data_value with dict without @value."""
query = WOQLQuery()
input_dict = {"@type": "Custom"}
result = query._clean_data_value(input_dict)
assert result == input_dict
def test_clean_arithmetic_value_with_string():
"""Test _clean_arithmetic_value with string."""
query = WOQLQuery()
result = query._clean_arithmetic_value("42")
assert result["@type"] == "ArithmeticValue"
assert result["data"]["@value"] == "42"
def test_clean_arithmetic_value_with_v_prefix():
"""Test _clean_arithmetic_value with v: prefixed variable."""
query = WOQLQuery()
result = query._clean_arithmetic_value("v:Count")
assert result["@type"] == "ArithmeticValue"
assert result["variable"] == "Count"
def test_clean_arithmetic_value_with_int():
"""Test _clean_arithmetic_value with integer."""
query = WOQLQuery()
result = query._clean_arithmetic_value(50)
assert result["@type"] == "ArithmeticValue"
assert result["data"]["@type"] == "xsd:integer"
assert result["data"]["@value"] == 50
def test_clean_arithmetic_value_with_float():
"""Test _clean_arithmetic_value with float."""
query = WOQLQuery()
result = query._clean_arithmetic_value(7.5)
assert result["@type"] == "ArithmeticValue"
assert result["data"]["@type"] == "xsd:decimal"
assert result["data"]["@value"] == 7.5
def test_clean_arithmetic_value_with_bool():
"""Test _clean_arithmetic_value with boolean."""
query = WOQLQuery()
result = query._clean_arithmetic_value(True)
assert result["@type"] == "ArithmeticValue"
assert result["data"]["@type"] == "xsd:boolean"
def test_clean_arithmetic_value_with_date():
"""Test _clean_arithmetic_value with datetime."""
query = WOQLQuery()
dt = datetime.date(2025, 6, 15)
result = query._clean_arithmetic_value(dt)
assert result["@type"] == "ArithmeticValue"
assert result["data"]["@type"] == "xsd:dateTime"
def test_clean_arithmetic_value_with_dict_value():
"""Test _clean_arithmetic_value with dict containing @value."""
query = WOQLQuery()
input_dict = {"@type": "xsd:integer", "@value": 999}
result = query._clean_arithmetic_value(input_dict)
assert result["@type"] == "ArithmeticValue"
assert result["data"] == input_dict
def test_clean_arithmetic_value_with_plain_dict():
"""Test _clean_arithmetic_value with dict without @value."""
query = WOQLQuery()
input_dict = {"@type": "Custom"}
result = query._clean_arithmetic_value(input_dict)
assert result == input_dict
def test_clean_node_value_with_string():
"""Test _clean_node_value with plain string."""
query = WOQLQuery()
result = query._clean_node_value("nodeId")
assert result["@type"] == "NodeValue"
assert result["node"] == "nodeId"
def test_clean_node_value_with_v_prefix():
"""Test _clean_node_value with v: prefixed variable."""
query = WOQLQuery()
result = query._clean_node_value("v:Node")
assert result["@type"] == "NodeValue"
assert result["variable"] == "Node"
def test_clean_node_value_with_var():
"""Test _clean_node_value with Var object."""
query = WOQLQuery()
var = Var("MyNode")
result = query._clean_node_value(var)
assert result["@type"] == "NodeValue"
assert result["variable"] == "MyNode"
def test_clean_node_value_with_dict():
"""Test _clean_node_value with dict."""
query = WOQLQuery()
input_dict = {"@type": "Custom", "node": "test"}
result = query._clean_node_value(input_dict)
assert result == input_dict
def test_clean_node_value_with_other_type():
"""Test _clean_node_value with non-string, non-Var, non-dict."""
query = WOQLQuery()
result = query._clean_node_value(12345)
assert result["@type"] == "NodeValue"
assert result["node"] == 12345
def test_clean_graph():
"""Test _clean_graph returns graph as-is."""
query = WOQLQuery()
result = query._clean_graph("instance/main")
assert result == "instance/main"
def test_expand_variable_with_v_prefix():
"""Test _expand_variable with v: prefix."""
query = WOQLQuery()
result = query._expand_variable("v:MyVar", "Value")
assert result["@type"] == "Value"
assert result["variable"] == "MyVar"
def test_expand_variable_with_always_true():
"""Test _expand_variable with always=True."""
query = WOQLQuery()
result = query._expand_variable("MyVar", "Value", always=True)
assert result["@type"] == "Value"
assert result["variable"] == "MyVar"
def test_expand_variable_without_v_prefix():
"""Test _expand_variable without v: prefix and always=False."""
query = WOQLQuery()
result = query._expand_variable("nodeId", "NodeValue")
assert result["@type"] == "NodeValue"
assert result["node"] == "nodeId"
def test_expand_variable_with_var_object():
"""Test _expand_variable with Var object."""
query = WOQLQuery()
var = Var("X")
result = query._expand_variable(var, "DataValue")
assert result["@type"] == "DataValue"
assert result["variable"] == "X"
def test_expand_value_variable():
"""Test _expand_value_variable helper."""
query = WOQLQuery()
result = query._expand_value_variable("v:Val")
assert result["@type"] == "Value"
assert result["variable"] == "Val"
def test_expand_node_variable():
"""Test _expand_node_variable helper."""
query = WOQLQuery()
result = query._expand_node_variable("v:Node")
assert result["@type"] == "NodeValue"
assert result["variable"] == "Node"
def test_expand_data_variable():
"""Test _expand_data_variable helper."""
query = WOQLQuery()
result = query._expand_data_variable("v:Data")
assert result["@type"] == "DataValue"
assert result["variable"] == "Data"
def test_expand_arithmetic_variable():
"""Test _expand_arithmetic_variable helper."""
query = WOQLQuery()
result = query._expand_arithmetic_variable("v:Arith")
assert result["@type"] == "ArithmeticValue"
assert result["variable"] == "Arith"
def test_to_json():
"""Test to_json serializes query."""
query = WOQLQuery({"@type": "Triple"})
result = query.to_json()
assert isinstance(result, str)
assert "@type" in result
assert "Triple" in result
def test_from_json():
"""Test from_json deserializes query."""
query = WOQLQuery()
json_str = '{"@type": "Triple", "subject": "v:X"}'
result = query.from_json(json_str)
assert result is query
assert query._query["@type"] == "Triple"
def test_json_without_input():
"""Test _json returns JSON string when no input."""
query = WOQLQuery({"@type": "And", "and": []})
result = query._json()
assert isinstance(result, str)
# The query is serialized to JSON
assert "{" in result and "}" in result
def test_json_with_input():
"""Test _json sets query when input provided."""
query = WOQLQuery()
json_str = '{"@type": "Or"}'
result = query._json(json_str)
assert result is query
assert query._query["@type"] == "Or"