forked from terminusdb/terminusdb-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_woqlQuery.py
More file actions
732 lines (650 loc) · 26.4 KB
/
test_woqlQuery.py
File metadata and controls
732 lines (650 loc) · 26.4 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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
import pprint
import datetime as dt
from terminusdb_client.woqlquery.woql_query import WOQLQuery, Doc, Var
from .ans_cardinality import * # noqa
from .ans_doctype import * # noqa
from .ans_insert import * # noqa
from .ans_property import * # noqa
from .ans_triple_quad import * # noqa
# expected results
from .woqljson.woqlAndJson import WOQL_AND_JSON
from .woqljson.woqlCastJson import WOQL_CAST_JSON
from .woqljson.woqlConcatJson import WOQL_CONCAT_JSON
from .woqljson.woqlIdgenJson import (
WOQL_IDGEN_JSON,
WOQL_RANDOM_KEY_JSON,
WOQL_UNIQUE_JSON,
)
from .woqljson.woqlJoinSplitJson import WOQL_JOIN_SPLIT_JSON
from .woqljson.woqlJson import WOQL_JSON, WOQL_STAR
from .woqljson.woqlMathJson import WOQL_MATH_JSON
from .woqljson.woqlOrJson import WOQL_OR_JSON
from .woqljson.woqlTrimJson import WOQL_TRIM_JSON
pp = pprint.PrettyPrinter(indent=4)
class TestWoqlQueries:
def test_start_properties_values(self):
woql_object = WOQLQuery()
assert not woql_object._chain_ended
assert not woql_object._contains_update
assert woql_object._vocab["type"] == "rdf:type"
def test_limit_method(self):
woql_object = WOQLQuery().limit(10)
limit_json = {}
assert woql_object.to_dict() == limit_json
def test_start_method(self):
woql_object = WOQLQuery().limit(10).start(0).star()
json_obj = {
"@type": "Limit",
"limit": 10,
"query": {"@type": "Start", "query": WOQL_STAR, "start": 0},
}
assert woql_object.to_dict() == json_obj
def test_woql_not_method(self):
woql_object = WOQLQuery().woql_not(WOQLQuery().triple("a", "b", "c"))
woql_object_chain = WOQLQuery().woql_not()
woql_object_chain.triple("a", "b", "c")
json_obj = {
"@type": "Not",
"query": {
"@type": "Triple",
"subject": {"@type": "NodeValue", "node": "a"},
"predicate": {"@type": "NodeValue", "node": "b"},
"object": {
"@type": "Value",
"node": "c",
},
},
}
assert woql_object.to_dict() == json_obj
assert woql_object_chain.to_dict() == json_obj
def test_woql_and_method(self):
woql_object = WOQLQuery().woql_and(
WOQLQuery().triple("a", "b", "c"), WOQLQuery().triple("1", "2", "3")
)
woql_object2 = WOQLQuery().triple("a", "b", "c") + WOQLQuery().triple(
"1", "2", "3"
)
woql_object3 = WOQLQuery().triple("a", "b", "c") & WOQLQuery().triple(
"1", "2", "3"
)
assert woql_object.to_dict() == WOQL_AND_JSON
assert woql_object2.to_dict() == WOQL_AND_JSON
assert woql_object3.to_dict() == WOQL_AND_JSON
def test_woql_or_method(self):
woql_object = WOQLQuery().woql_or(
WOQLQuery().triple("a", "b", "c"), WOQLQuery().triple("1", "2", "3")
)
woql_object2 = WOQLQuery().triple("a", "b", "c") | WOQLQuery().triple(
"1", "2", "3"
)
assert woql_object.to_dict() == WOQL_OR_JSON
assert woql_object2.to_dict() == WOQL_OR_JSON
def test_opt_method(self):
woql_object = WOQLQuery().opt(WOQLQuery().star())
woql_object_chain = WOQLQuery().opt().star()
json_obj = {"@type": "Optional", "query": WOQL_STAR}
assert woql_object.to_dict() == json_obj
assert woql_object_chain.to_dict() == json_obj
def test_woql_from_method(self):
woql_object = WOQLQuery().woql_from("schema", WOQLQuery().star())
woql_object_chain = WOQLQuery().woql_from("schema").star()
json_obj = {
"@type": "From",
"graph": "schema",
"query": WOQL_STAR,
}
assert woql_object.to_dict() == woql_object_chain.to_dict()
assert woql_object_chain.to_dict() == json_obj
def test_star_method(self):
woql_object = WOQLQuery().star()
assert woql_object.to_dict() == WOQL_STAR
def test_select_method(self):
# TODO: chain won't work
woql_object = WOQLQuery().select("V1", WOQLQuery().star())
woql_object_multiple = WOQLQuery().select("V1", "V2", WOQLQuery().star())
woql_object_chain = WOQLQuery().select("V1").star()
woql_object_chain_multiple = WOQLQuery().select("V1", "V2").star()
assert woql_object_chain_multiple.to_dict() == woql_object_multiple.to_dict()
assert woql_object.to_dict() == woql_object_chain.to_dict()
def test_eq_method(self):
woql_object = WOQLQuery().eq("a", "b")
json_obj = {
"@type": "Equals",
"left": {
"@type": "Value",
"node": "a",
},
"right": {"@type": "Value", "node": "b"},
}
assert woql_object.to_dict() == json_obj
def test_trim_method(self):
woql_object = WOQLQuery().trim("a", "b")
assert woql_object.to_dict() == WOQL_TRIM_JSON
def test_eval_method(self):
woql_object = WOQLQuery().eval(WOQLQuery().plus(1, 2), "v:x")
assert woql_object.to_dict() == WOQL_MATH_JSON["evalJson"]
def test_minus_method(self):
woql_object = WOQLQuery().minus(1, 1)
assert woql_object.to_dict() == WOQL_MATH_JSON["minusJson"]
def test_plus_method(self):
woql_object = WOQLQuery().plus(2, 1)
assert woql_object.to_dict() == WOQL_MATH_JSON["plusJson"]
def test_times_method(self):
woql_object = WOQLQuery().times(2, 1)
assert woql_object.to_dict() == WOQL_MATH_JSON["timesJson"]
def test_divide_method(self):
woql_object = WOQLQuery().divide(2, 1)
assert woql_object.to_dict() == WOQL_MATH_JSON["divideJson"]
def test_exp_method(self):
woql_object = WOQLQuery().exp(2, 1)
assert woql_object.to_dict() == WOQL_MATH_JSON["expJson"]
def test_div_method(self):
woql_object = WOQLQuery().div(2, 1)
assert woql_object.to_dict() == WOQL_MATH_JSON["divJson"]
def test_floor_method(self):
woql_object = WOQLQuery().floor(2.5)
assert woql_object.to_dict() == WOQL_MATH_JSON["floorJson"]
def test_get_method(self):
woql_object = WOQLQuery().get(WOQLQuery().woql_as("a", "b"), "Target")
json_obj = {
"@type": "Get",
"columns": [
{
"@type": "Column",
"indicator": {"@type": "Indicator", "name": "a"},
"variable": "b",
}
],
"resource": "Target",
}
assert woql_object.to_dict() == json_obj
def test_remote_method(self):
woql_object = WOQLQuery().remote({"url": "http://url"})
json_obj = {
"@type": "QueryResource",
"format": "csv",
"source": {"@type": "Source", "url": "http://url"},
}
assert woql_object.to_dict() == json_obj
def test_post_method(self):
woql_object = WOQLQuery().post({"file": "my_json_file", "format": "csv"})
json_obj = {
"@type": "QueryResource",
"source": {"file": "my_json_file", "format": "csv", "@type": "Source"},
"format": "csv",
}
assert woql_object.to_dict() == json_obj
def test_unique_method(self):
woql_object = WOQLQuery().unique("Station", "v:Start_ID", "v:Start_Station_URL")
assert woql_object.to_dict() == WOQL_UNIQUE_JSON
def test_idgen_method(self):
woql_object = WOQLQuery().idgen("Station", "v:Start_ID", "v:Start_Station_URL")
assert woql_object.to_dict() == WOQL_IDGEN_JSON
def test_idgen_random_method(self):
woql_object = WOQLQuery().idgen_random("Person/", "v:Person_ID")
assert woql_object.to_dict() == WOQL_RANDOM_KEY_JSON
def test_typecast_method(self):
woql_object = WOQLQuery().typecast(
"v:Duration", "xsd:integer", "v:Duration_Cast"
)
json_obj = {
"@type": "Typecast",
"value": {
"@type": "Value",
"variable": "Duration",
},
"type": {"@type": "NodeValue", "node": "xsd:integer"},
"result": {"@type": "Value", "variable": "Duration_Cast"},
}
assert woql_object.to_dict() == json_obj
def test_cast_method(self):
woql_object = WOQLQuery().cast("v:Duration", "xsd:integer", "v:Duration_Cast")
assert woql_object.to_dict() == WOQL_CAST_JSON
def test_cast_method_literal(self):
woql_object1 = WOQLQuery().cast(
"1", "xsd:integer", "v:Duration_Cast", literal_type="integer"
)
woql_object2 = WOQLQuery().cast(
WOQLQuery().literal("1", "integer"), "xsd:integer", "v:Duration_Cast"
)
assert woql_object1.to_dict() == woql_object2.to_dict()
def test_cast_method_datetime(self):
value = dt.datetime(2022, 10, 19, 14, 17, 12)
json_datetime = WOQLQuery().datetime(value)
json_datetime2 = WOQLQuery().literal("2022-10-19T14:17:12", "dateTime")
assert json_datetime == json_datetime2
def test_cast_method_date(self):
value = dt.date(2022, 10, 19)
json_datetime = WOQLQuery().date(value)
json_datetime2 = WOQLQuery().literal("2022-10-19", "date")
assert json_datetime == json_datetime2
def test_cast_method_boolean(self):
json_datetime = WOQLQuery().boolean(True)
json_datetime2 = WOQLQuery().literal(True, "boolean")
assert json_datetime == json_datetime2
def test_cast_method_object(self):
woql_object1 = WOQLQuery().cast(
"my_int", "xsd:integer", "v:Duration_Cast", literal_type="owl:Thing"
)
woql_object2 = WOQLQuery().cast(
"my_int", "xsd:integer", "v:Duration_Cast", literal_type="node"
)
woql_object3 = WOQLQuery().cast("my_int", "xsd:integer", "v:Duration_Cast")
assert woql_object1.to_dict() == woql_object3.to_dict()
assert woql_object2.to_dict() == woql_object3.to_dict()
def test_re_method(self):
woql_object = WOQLQuery().re("!\\w+(.*)test$", "v:string", "v:formated")
json_obj = {
"@type": "Regexp",
"pattern": {
"@type": "DataValue",
"data": {"@type": "xsd:string", "@value": "!\\w+(.*)test$"},
},
"result": {
"@type": "DataValue",
"variable": "formated",
},
"string": {
"@type": "DataValue",
"variable": "string",
},
}
assert woql_object.to_dict() == json_obj
def test_join_method(self):
woql_object = WOQLQuery().join(["v:A_obj", "v:B_obj"], ", ", "v:output")
assert woql_object.to_dict() == WOQL_JOIN_SPLIT_JSON["joinJson"]
def test_split_method(self):
woql_object = WOQLQuery().split("A, B, C", ", ", "v:list_obj")
assert woql_object.to_dict() == WOQL_JOIN_SPLIT_JSON["splitJson"]
def test_member_method(self):
woql_object = WOQLQuery().member("v:member", "v:list_obj")
json_obj = {
"@type": "Member",
"member": {
"@type": "Value",
"variable": "member",
},
"list": {
"@type": "Value",
"variable": "list_obj",
},
}
assert woql_object.to_dict() == json_obj
def test_concat_method(self):
woql_object = WOQLQuery().concat(
["v:Duration", " yo ", "v:Duration_Cast"], "v:x"
)
assert woql_object.to_dict() == WOQL_CONCAT_JSON
def test_group_by_method(self):
woql_object = (
WOQLQuery()
.group_by(["v:A", "v:B"], ["v:C"], "v:New")
.triple("v:A", "v:B", "v:C")
)
assert woql_object.to_dict() == WOQL_JSON["groupbyJson"]
def test_order_by_method_asc(self):
woql_object = (
WOQLQuery()
.order_by("v:A", "v:B", "v:C", order=["asc", "desc", "asc"])
.star()
)
assert woql_object.to_dict() == WOQL_JSON["orderbyJson"]
def test_simple_path_query(self):
woql_object = WOQLQuery().path("v:X", "hop", "v:Y", "v:Path")
json_object = {
"@type": "Path",
"subject": {
"@type": "NodeValue",
"variable": "X",
},
"pattern": {
"@type": "PathPredicate",
"predicate": "hop",
},
"object": {
"@type": "Value",
"variable": "Y",
},
"path": {
"@type": "Value",
"variable": "Path",
},
}
assert woql_object.to_dict() == json_object
def test_plus_directed_path_query(self):
woql_object = WOQLQuery().path("v:X", "<hop+", "v:Y", "v:Path")
json_object = {
"@type": "Path",
"subject": {
"@type": "NodeValue",
"variable": "X",
},
"pattern": {
"@type": "PathPlus",
"plus": {
"@type": "InversePathPredicate",
"predicate": "hop",
},
},
"object": {
"@type": "Value",
"variable": "Y",
},
"path": {
"@type": "Value",
"variable": "Path",
},
}
assert woql_object.to_dict() == json_object
def test_grouped_path_query(self):
woql_object = WOQLQuery().path("v:X", "(<hop,hop>)+", "v:Y", "v:Path")
json_object = {
"@type": "Path",
"subject": {
"@type": "NodeValue",
"variable": "X",
},
"pattern": {
"@type": "PathPlus",
"plus": {
"@type": "PathSequence",
"sequence": [
{
"@type": "InversePathPredicate",
"predicate": "hop",
},
{
"@type": "PathPredicate",
"predicate": "hop",
},
],
},
},
"object": {
"@type": "Value",
"variable": "Y",
},
"path": {
"@type": "Value",
"variable": "Path",
},
}
assert woql_object.to_dict() == json_object
def test_double_grouped_path_query(self):
woql_object = WOQLQuery().path(
"v:X", "((<hop,hop>)|(<hop2,hop2>))+", "v:Y", "v:Path"
)
json_object = {
"@type": "Path",
"subject": {
"@type": "NodeValue",
"variable": "X",
},
"pattern": {
"@type": "PathPlus",
"plus": {
"@type": "PathOr",
"or": [
{
"@type": "PathSequence",
"sequence": [
{"@type": "InversePathPredicate", "predicate": "hop"},
{
"@type": "PathPredicate",
"predicate": "hop",
},
],
},
{
"@type": "PathSequence",
"sequence": [
{
"@type": "InversePathPredicate",
"predicate": "hop2",
},
{
"@type": "PathPredicate",
"predicate": "hop2",
},
],
},
],
},
},
"object": {
"@type": "Value",
"variable": "Y",
},
"path": {"@type": "Value", "variable": "Path"},
}
assert woql_object.to_dict() == json_object
def test_percent_in_path(self):
query = WOQLQuery().path(
"after_uri",
"general_variables,relationship_to_preceding_%28quasi%29polity,known,value",
"relationship",
)
assert query.to_dict() == {
"@type": "Path",
"subject": {"@type": "NodeValue", "node": "after_uri"},
"pattern": {
"@type": "PathSequence",
"sequence": [
{"@type": "PathPredicate", "predicate": "general_variables"},
{
"@type": "PathPredicate",
"predicate": "relationship_to_preceding_%28quasi%29polity",
},
{"@type": "PathPredicate", "predicate": "known"},
{"@type": "PathPredicate", "predicate": "value"},
],
},
"object": {"@type": "Value", "node": "relationship"},
}
class TestTripleBuilder:
def test_triple_method(self, triple_opt):
woql_object = WOQLQuery().triple("a", "b", "c")
woql_object_opt = WOQLQuery().triple(
"a", "b", WOQLQuery().string("c"), opt=True
)
assert woql_object.to_dict() == WOQL_JSON["tripleJson"]
assert woql_object_opt.to_dict() == triple_opt
def test_quad_method(self, quad_opt):
woql_object = WOQLQuery().quad("a", "b", "c", "d")
woql_object_opt = WOQLQuery().quad(
"a", "b", WOQLQuery().string("c"), "d", opt=True
)
assert woql_object.to_dict() == WOQL_JSON["quadJson"]
assert woql_object_opt.to_dict() == quad_opt
def test_sub_method(self):
woql_object = WOQLQuery().sub("ClassA", "ClassB")
assert woql_object.to_dict() == WOQL_JSON["subsumptionJson"]
def test_isa_method(self):
woql_object = WOQLQuery().isa("instance", "Class")
assert woql_object.to_dict() == WOQL_JSON["isAJson"]
def test_delete_method(self):
woql_object = WOQLQuery().delete_document("x")
json_obj = {
"@type": "DeleteDocument",
"identifier": {"@type": "NodeValue", "node": "x"},
}
assert woql_object.to_dict() == json_obj
def test_update_method(self):
woql_object = WOQLQuery().update_document("x", "iri")
json_obj = {
"@type": "UpdateDocument",
"document": {"@type": "Value", "node": "x"},
"identifier": {"@type": "NodeValue", "node": "iri"},
}
assert woql_object.to_dict() == json_obj
def test_insert_method(self):
woql_object = WOQLQuery().insert_document("x", "iri")
json_obj = {
"@type": "InsertDocument",
"document": {"@type": "Value", "node": "x"},
"identifier": {"@type": "NodeValue", "node": "iri"},
}
assert woql_object.to_dict() == json_obj
def test_read_method(self):
woql_object = WOQLQuery().read_document("iri", "output")
json_obj = {
"@type": "ReadDocument",
"document": {"@type": "Value", "node": "output"},
"identifier": {"@type": "NodeValue", "node": "iri"},
}
assert woql_object.to_dict() == json_obj
def test_delete_triple_method(self):
woql_object = WOQLQuery().delete_triple("a", "b", "c")
assert woql_object.to_dict() == WOQL_JSON["deleteTripleJson"]
def test_delete_quad_method(self):
woql_object = WOQLQuery().delete_quad("a", "b", "c", "d")
assert woql_object.to_dict() == WOQL_JSON["deleteQuadJson"]
def test_add_triple_method(self):
woql_object = WOQLQuery().add_triple("a", "b", "c")
assert woql_object.to_dict() == WOQL_JSON["addTripleJson"]
def test_add_quad_method(self):
woql_object = WOQLQuery().add_quad("a", "b", "c", "d")
assert woql_object.to_dict() == WOQL_JSON["addQuadJson"]
class TestTripleBuilderChainer:
def test_dot_chain(self):
woql_object = WOQLQuery().triple("A", "B", "C").triple("D", "E", "F")
v2 = WOQLQuery().woql_and(
WOQLQuery().triple("A", "B", "C"), WOQLQuery().triple("D", "E", "F")
)
v3 = WOQLQuery().triple("A", "B", "C").woql_and().triple("D", "E", "F")
assert woql_object.to_dict() == v2.to_dict()
assert woql_object.to_dict() == v3.to_dict()
def test_vars(self):
single_vars = WOQLQuery().vars("a")
vars1, vars2, vars3 = WOQLQuery().vars("a", "b", "c")
assert single_vars.name == "a"
assert (vars1.name, vars2.name, vars3.name) == ("a", "b", "c")
def test_woql_as_method(self):
[x, y, z] = WOQLQuery().vars("x", "y", "z")
query = WOQLQuery().woql_as(x).woql_as(y).woql_as(z)
assert query.to_dict() == [
{
"@type": "Column",
"indicator": {"@type": "Indicator", "index": 0},
"variable": "x",
},
{
"@type": "Column",
"indicator": {"@type": "Indicator", "index": 1},
"variable": "y",
},
{
"@type": "Column",
"indicator": {"@type": "Indicator", "index": 2},
"variable": "z",
},
]
def test_path_method(self):
query = WOQLQuery().path(
"v:person",
"((capability_userstory|userstory_gap){1,2})",
"v:circle",
"v:path_var5",
)
assert (
query.to_json()
== '{"@type": "Path", "object": {"@type": "Value", "variable": "circle"}, "path": {"@type": "Value", "variable": "path_var5"}, "pattern": {"@type": "PathTimes", "from": 1, "times": {"@type": "PathOr", "or": [{"@type": "PathPredicate", "predicate": "capability_userstory"}, {"@type": "PathPredicate", "predicate": "userstory_gap"}]}, "to": 2}, "subject": {"@type": "NodeValue", "variable": "person"}}'
)
def test_compile_path_pattern(self):
result = WOQLQuery()._compile_path_pattern(
"((capability_userstory|userstory_gap){1,2})"
)
assert result == {
"@type": "PathTimes",
"from": 1,
"to": 2,
"times": {
"@type": "PathOr",
"or": [
{"@type": "PathPredicate", "predicate": "capability_userstory"},
{"@type": "PathPredicate", "predicate": "userstory_gap"},
],
},
}
def test_compile_path_pattern2(self):
result = WOQLQuery()._compile_path_pattern("(pred|qred|res)*")
assert result == {
"@type": "PathStar",
"star": {
"@type": "PathOr",
"or": [
{"@type": "PathPredicate", "predicate": "pred"},
{"@type": "PathPredicate", "predicate": "qred"},
{"@type": "PathPredicate", "predicate": "res"},
],
},
}
def test_compile_path_pattern3(self):
result = WOQLQuery()._compile_path_pattern("<left*,right>*")
assert result == {
"@type": "PathSequence",
"sequence": [
{
"@type": "PathStar",
"star": {"@type": "InversePathPredicate", "predicate": "left"},
},
{
"@type": "PathStar",
"star": {"@type": "PathPredicate", "predicate": "right"},
},
],
}
def test_compile_path_pattern4(self):
result = WOQLQuery()._compile_path_pattern(".*,date")
assert result == {
"@type": "PathSequence",
"sequence": [
{"@type": "PathStar", "star": {"@type": "PathPredicate"}},
{"@type": "PathPredicate", "predicate": "date"},
],
}
def test_compile_path_pattern5(self):
result = WOQLQuery()._compile_path_pattern("(ex:pred|evo:qred)*")
assert result == {
"@type": "PathStar",
"star": {
"@type": "PathOr",
"or": [
{"@type": "PathPredicate", "predicate": "ex:pred"},
{"@type": "PathPredicate", "predicate": "evo:qred"},
],
},
}
def test_dot(self):
result = WOQLQuery().dot("document", "field", "value")
assert result.to_dict() == {
"@type": "Dot",
"document": {"@type": "Value", "node": "document"},
"field": {
"@type": "DataValue",
"data": {"@type": "xsd:string", "@value": "field"},
},
"value": {"@type": "Value", "node": "value"},
}
def test_doc(self):
result = WOQLQuery().insert_document(
Doc({"@type": "Car", "wheels": 4})
)
assert result.to_dict() == {'@type': 'InsertDocument', 'document': {'@type': 'Value', 'dictionary': {'@type': 'DictionaryTemplate', 'data': [{'@type': 'FieldValuePair', 'field': '@type', 'value': {'@type': 'Value', 'data': {'@type': 'xsd:string', '@value': 'Car'}}}, {'@type': 'FieldValuePair', 'field': 'wheels', 'value': {'@type': 'Value', 'data': {'@type': 'xsd:integer', '@value': 4}}}]}}}
def test_var(self):
result = WOQLQuery().insert_document(
Doc({"@type": "Car", "wheels": Var("v")})
)
assert result.to_dict() == {
'@type': 'InsertDocument',
'document': {'@type': 'Value',
'dictionary': {'@type': 'DictionaryTemplate',
'data': [{'@type': 'FieldValuePair',
'field': '@type',
'value': {'@type': 'Value',
'data': {'@type': 'xsd:string',
'@value': 'Car'}}},
{'@type': 'FieldValuePair',
'field': 'wheels',
'value': {'@type': 'Value',
'variable': 'v'}}]}}}