-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_datalog.py
286 lines (199 loc) · 11.1 KB
/
test_datalog.py
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
import pytest
from xtdb.datalog import (
Expression,
Find,
In,
Limit,
NotJoin,
OrderBy,
OrJoin,
Sample,
Sum,
Timeout,
Where,
WherePredicate,
_BaseAggregate,
)
from xtdb.exceptions import XTDBException
def test_where_clauses():
statement = Where("a", "b", "c")
assert statement.compile() == ":where [[ a :b c ]]"
statement = Where("a", "b")
assert statement.compile() == ":where [[ a :b ]]"
statement = Where("a", "b", "c") & Where("1", "2", "3")
assert statement.compile() == ":where [ [ 1 :2 3 ] [ a :b c ]]"
statement = Where("a", "b", "c") & Where("1", "2", "3") & Where("1", "2", "3")
assert statement.compile() == ":where [ [ 1 :2 3 ] [ a :b c ]]"
statement = Where("a", "b", "c") & Where("1", "2", "3") & Where("x", "y", "z")
assert statement.compile() == ":where [ [ 1 :2 3 ] [ a :b c ] [ x :y z ]]"
statement = Where("a", "b", "c") & Where("1", "2", "3") & Where("1", "2", "3") & Where("a", "b", "c")
assert statement.compile() == ":where [ [ 1 :2 3 ] [ a :b c ]]"
statement = Where("a", "b", "c") & Where("1", "2", "3") & Where("x", "y", "z")
assert statement.compile() == ":where [ [ 1 :2 3 ] [ a :b c ] [ x :y z ]]"
assert str(statement) == statement.compile()
def test_or_clauses():
statement = Where("a", "b", "c") | Where("1", "2", "3")
assert statement.compile() == ":where [(or [ 1 :2 3 ] [ a :b c ])]"
statement = Where("a", "b", "c") | Where("1", "2", "3") | Where("1", "2", "3")
assert statement.compile() == ":where [(or [ 1 :2 3 ] [ a :b c ])]"
def test_not_clauses():
statement = ~Where("a", "b", "c")
assert statement.compile() == ":where [(not [ a :b c ])]"
statement = ~(Where("a", "b", "c") & Where("1", "2", "3"))
assert statement.compile() == ":where [(not [ 1 :2 3 ] [ a :b c ])]"
statement = ~(Where("a", "b", "c") & Where("1", "2", "3") & Where("x", "y"))
assert statement.compile() == ":where [(not [ 1 :2 3 ] [ a :b c ] [ x :y ])]"
def test_where_or_clauses():
statement = Where("a", "b", "c") & Where("1", "2", "3") | Where("x", "y", "z") & Where("9", "8", "7")
assert statement.compile() == ":where [(or (and [ 1 :2 3 ] [ a :b c ]) (and [ 9 :8 7 ] [ x :y z ]))]"
statement = Where("a", "b", "c") & (Where("1", "2", "3") | Where("x", "y", "z"))
assert statement.compile() == ":where [ (or [ 1 :2 3 ] [ x :y z ]) [ a :b c ]]"
# The & operator takes precedence over the | operator
statement = (Where("a", "b", "c") | Where("1", "2", "3")) & Where("x", "y", "z")
assert statement.compile() == ":where [ (or [ 1 :2 3 ] [ a :b c ]) [ x :y z ]]"
statement = (Where("a", "b", "c") | Where("1", "2", "3")) & (Where("x", "y", "z") | Where("9", "8", "7"))
assert statement.compile() == ":where [ (or [ 1 :2 3 ] [ a :b c ]) (or [ 9 :8 7 ] [ x :y z ])]"
with pytest.raises(XTDBException):
Where("a", "b", "c") & Where("1", "2", "3") | Where("x", "y", "z")
with pytest.raises(XTDBException):
Where("a", "b", "c") | Where("1", "2", "3") & Where("x", "y", "z")
def test_find_clauses():
statement = Find("a")
assert statement.compile() == ":find [a]"
statement = Find("a") & Find("b")
assert statement.compile() == ":find [ a b]"
statement = Find("pull(*)") & Find("b") & Find(Expression("(sum ?heads)"))
assert statement.compile() == ":find [ pull(*) b (sum ?heads)]"
with pytest.raises(XTDBException) as ctx:
Find("pull(*)") | Find("b")
assert ctx.exconly() == "xtdb.exceptions.XTDBException: Cannot use | on query keys"
def test_aggregates():
statement = Find("a") & Find(_BaseAggregate("sum", "field"))
assert statement.compile() == ":find [ a (sum field)]"
statement = Find("a") & Find(_BaseAggregate("sample", "field", "12"))
assert statement.compile() == ":find [ a (sample 12 field)]"
with pytest.raises(XTDBException) as ctx:
Find("a") & Find(_BaseAggregate("wrong", "field"))
assert ctx.exconly() == "xtdb.exceptions.XTDBException: Invalid aggregate function"
with pytest.raises(XTDBException) as ctx:
Find("a") & Find(_BaseAggregate("rand", "field"))
assert ctx.exconly() == "xtdb.exceptions.XTDBException: Invalid arguments to aggregate, it needs one argument: N"
def test_concrete_aggregates():
statement = Find("a") & Sum("field")
assert statement.compile() == ":find [ a (sum field)]"
statement = Find("a") & Sum("field") & Sum("field")
assert statement.compile() == ":find [ a (sum field) (sum field)]"
statement = Find("a") & Sample("field", 12)
assert statement.compile() == ":find [ a (sample 12 field)]"
with pytest.raises(XTDBException):
Sample("field", 12) | Sum("field")
def test_find_where():
statement = Find("a") & Where("a", "b", "c")
assert statement.compile() == "{:query {:find [a] :where [[ a :b c ]]}}"
statement = Find("a") & (Where("a", "b", "c") & Where("1", "2", "3"))
assert statement.compile() == "{:query {:find [a] :where [ [ 1 :2 3 ] [ a :b c ]]}}"
statement = Find("a") & (Where("a", "b", "c") | Where("1", "2", "3"))
assert statement.compile() == "{:query {:find [a] :where [(or [ 1 :2 3 ] [ a :b c ])]}}"
statement = Find("a") & Find("b") & (Where("a", "b", "c") | Where("1", "2", "3"))
assert statement.compile() == "{:query {:find [ a b] :where [(or [ 1 :2 3 ] [ a :b c ])]}}"
statement = (
Find("a")
& Sample("field", 12)
& Sum("field")
& ((Where("a", "b", "c") | Where("1", "2", "3")) & Where("x", "y", "z"))
)
assert (
statement.compile()
== "{:query {:find [ a (sample 12 field) (sum field)] :where [ (or [ 1 :2 3 ] [ a :b c ]) [ x :y z ]]}}"
)
statement = Sum("a") & Where("a", "b", "c")
assert statement.compile() == "{:query {:find [(sum a)] :where [[ a :b c ]]}}"
statement = Sum("a") & Sum("b") & Where("a", "b", "c")
assert statement.compile() == "{:query {:find [ (sum a) (sum b)] :where [[ a :b c ]]}}"
def test_find_where_not():
statement = Sum("a") & Sum("b") & ~(Where("a", "b", "c") & Where("x", "y", "z"))
assert statement.compile() == "{:query {:find [ (sum a) (sum b)] :where [(not [ a :b c ] [ x :y z ])]}}"
def test_find_where_in_complete():
statement = Find("a") & Where("a", "b", "c") & Limit(2) & Timeout(29) & OrderBy([("b", "asc")]) & In("c", "d")
# Scalar binding
assert (
statement.compile()
== '{:query {:find [a] :where [[ a :b c ]] :in [c] :order-by [[b :asc]] :limit 2 :timeout 29} :in-args ["d"]}'
)
# Collection binding
statement = Find("a") & Where("a", "b", "c") & Limit(2) & OrderBy([("b", "asc")]) & In(["c", "..."], ["d", "e"])
assert (
statement.compile()
== '{:query {:find [a] :where [[ a :b c ]] :in [[c ...]] :order-by [[b :asc]] :limit 2} :in-args [["d" "e"]]}'
)
# Tuple binding
statement = Find("a") & Where("a", "b", "c") & Limit(2) & OrderBy([("b", "asc")]) & In(["c", "z"], ["d", "e"])
assert (
statement.compile()
== '{:query {:find [a] :where [[ a :b c ]] :in [[c z]] :order-by [[b :asc]] :limit 2} :in-args [["d" "e"]]}'
)
# Relation binding
statement = Find("a") & Where("a", "b", "c") & Limit(2) & In([["c", "z"]], [["d", "e"], ["f", "g"]])
assert (
statement.compile()
== '{:query {:find [a] :where [[ a :b c ]] :in [[[c z]]] :limit 2} :in-args [[["d" "e"] ["f" "g"]]]}'
)
def test_find_where_wrong_order():
with pytest.raises(XTDBException):
Find("a") & Where("a", "b", "c") & Where("1", "2", "3")
with pytest.raises(XTDBException):
Where("a", "b", "c") & Find("a")
with pytest.raises(XTDBException):
(Where("a", "b", "c") | Where("1", "2", "3")) & Find("a")
def test_in():
statement = In(["field", "other-field"], ["value", "other-value"])
assert statement.compile() == " :in [[field other-field]]"
assert statement.compile_values() == ' :in-args [["value" "other-value"]]'
statement = In(["field", "..."], ["value", "other-value"])
assert statement.compile() == " :in [[field ...]]"
assert statement.compile_values() == ' :in-args [["value" "other-value"]]'
def test_order_by():
statement = OrderBy([("field_name", "asc"), ("test-name", "desc")])
assert statement.compile() == " :order-by [[field_name :asc] [test-name :desc]]"
with pytest.raises(XTDBException):
OrderBy([("field_name", "asc"), ("test-name", "esc")])
def test_where_predicate():
statement = WherePredicate("odd?", "b")
assert statement.compile() == ":where [[ (odd? b) ]]"
statement = WherePredicate("+", "1", "2", "b")
assert statement.compile() == ":where [[ (+ 1 2 b) ]]"
# From the docs
statement = WherePredicate("identity", "2", bind="x") & WherePredicate("+", "x", "2", bind="y")
assert statement.compile() == ":where [ [ (+ x 2) y] [ (identity 2) x]]"
statement = Find("a") & (Where("a", "b", "c") & WherePredicate("odd?", "c"))
assert statement.compile() == "{:query {:find [a] :where [ [ (odd? c) ] [ a :b c ]]}}"
def test_range_predicate():
# From the docs
statement = WherePredicate(">", 18, "a")
assert statement.compile() == ":where [[ (> 18 a) ]]"
def test_unification_predicate():
# From the docs
statement = WherePredicate("==", "a", "a2")
assert statement.compile() == ":where [[ (== a a2) ]]"
# From the docs
statement = WherePredicate("!=", "a", "a2")
assert statement.compile() == ":where [[ (!= a a2) ]]"
def test_not_join():
# From the docs
statement = Where("e", "xt/id") & (NotJoin("e") & Where("e", "last-name", "n"))
assert statement.compile() == ":where [ (not-join [e] [ e :last-name n ]) [ e :xt/id ]]"
statement = Where("e", "xt/id") & (NotJoin("e") & (Where("e", "last-name", "n") & Where("e", "name", "n")))
assert statement.compile() == ":where [ (not-join [e] [ e :last-name n ] [ e :name n ]) [ e :xt/id ]]"
statement = Where("e", "xt/id") & (NotJoin("e") & Where("e", "last-name", "n") & Where("e", "name", "n"))
assert statement.compile() == ":where [ (not-join [e] [ e :last-name n ] [ e :name n ]) [ e :xt/id ]]"
statement = Where("e", "xt/id") & NotJoin("e") & Where("e", "last-name", "n") & Where("e", "name", "n")
assert statement.compile() == ":where [ (not-join [e] ) [ e :last-name n ] [ e :name n ] [ e :xt/id ]]"
def test_or_join():
statement = Where("e", "xt/id") & (OrJoin("e") & Where("e", "last-name", "n"))
assert statement.compile() == ":where [ (or-join [e] [ e :last-name n ]) [ e :xt/id ]]"
statement = Where("e", "xt/id") & (OrJoin("e") & (Where("e", "last-name", "n") & Where("e", "name", "n")))
assert statement.compile() == ":where [ (or-join [e] [ e :last-name n ] [ e :name n ]) [ e :xt/id ]]"
statement = Where("e", "xt/id") & (OrJoin("e") & Where("e", "last-name", "n") & Where("e", "name", "n"))
assert statement.compile() == ":where [ (or-join [e] [ e :last-name n ] [ e :name n ]) [ e :xt/id ]]"
statement = Where("e", "xt/id") & OrJoin("e") & Where("e", "last-name", "n") & Where("e", "name", "n")
assert statement.compile() == ":where [ (or-join [e] ) [ e :last-name n ] [ e :name n ] [ e :xt/id ]]"