-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
query_fns.cljc
356 lines (309 loc) · 11.4 KB
/
query_fns.cljc
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
(ns datascript.test.query-fns
(:require
#?(:cljs [cljs.test :as t :refer-macros [is are deftest testing]]
:clj [clojure.test :as t :refer [is are deftest testing]])
[datascript.core :as d]
[datascript.db :as db]
[datascript.test.core :as tdc])
#?(:clj
(:import [clojure.lang ExceptionInfo])))
(deftest test-query-fns
(testing "predicate without free variables"
(is (= (d/q '[:find ?x
:in [?x ...]
:where [(> 2 1)]] [:a :b :c])
#{[:a] [:b] [:c]})))
(let [db (-> (d/empty-db {:parent {:db/valueType :db.type/ref}})
(d/db-with [ { :db/id 1, :name "Ivan", :age 15 }
{ :db/id 2, :name "Petr", :age 22, :height 240, :parent 1}
{ :db/id 3, :name "Slava", :age 37, :parent 2}]))]
(testing "ground"
(is (= (d/q '[:find ?vowel
:where [(ground [:a :e :i :o :u]) [?vowel ...]]])
#{[:a] [:e] [:i] [:o] [:u]})))
(testing "get-else"
(is (= (d/q '[:find ?e ?age ?height
:where [?e :age ?age]
[(get-else $ ?e :height 300) ?height]] db)
#{[1 15 300] [2 22 240] [3 37 300]}))
(is (thrown-with-msg? ExceptionInfo #"get-else: nil default value is not supported"
(d/q '[:find ?e ?height
:where [?e :age]
[(get-else $ ?e :height nil) ?height]] db))))
(testing "get-some"
(is (= (d/q '[:find ?e ?a ?v
:where [?e :name _]
[(get-some $ ?e :height :age) [?a ?v]]] db)
#{[1 :age 15]
[2 :height 240]
[3 :age 37]})))
(testing "missing?"
(is (= (d/q '[:find ?e ?age
:in $
:where [?e :age ?age]
[(missing? $ ?e :height)]] db)
#{[1 15] [3 37]})))
(testing "missing? back-ref"
(is (= (d/q '[:find ?e
:in $
:where [?e :age ?age]
[(missing? $ ?e :_parent)]] db)
#{[3]})))
(testing "Built-ins"
(is (= (d/q '[:find ?e1 ?e2
:where [?e1 :age ?a1]
[?e2 :age ?a2]
[(< ?a1 18 ?a2)]] db)
#{[1 2] [1 3]}))
(is (= (d/q '[:find ?a1
:where [_ :age ?a1]
[(< ?a1 22)]] db)
#{[15]}))
(is (= (d/q '[:find ?a1
:where [_ :age ?a1]
[(<= ?a1 22)]] db)
#{[15] [22]}))
(is (= (d/q '[:find ?a1
:where [_ :age ?a1]
[(> ?a1 22)]] db)
#{[37]}))
(is (= (d/q '[:find ?a1
:where [_ :age ?a1]
[(>= ?a1 22)]] db)
#{[22] [37]}))
(testing "compare values of different types"
(is (= (d/q '[:find ?e
:where [?e]
[(< ?e 1)]] [[0] [1] [""]])
#{[0]}))
(is (= (d/q '[:find ?e
:where [?e]
[(<= ?e 1)]] [[0] [1] [""]])
#{[0] [1]}))
(is (= (d/q '[:find ?e
:where [?e]
[(> ?e 1)]] [[0] [1] [""]])
#{[""]}))
(is (= (d/q '[:find ?e
:where [?e]
[(>= ?e 1)]] [[0] [1] [""]])
#{[1] [""]})))
(is (= (d/q '[:find ?x ?c
:in [?x ...]
:where [(count ?x) ?c]]
["a" "abc"])
#{["a" 1] ["abc" 3]})))
(testing "Built-in vector, hashmap"
(is (= (d/q '[:find [?tx-data ...]
:where
[(ground :db/add) ?op]
[(vector ?op -1 :attr 12) ?tx-data]])
[[:db/add -1 :attr 12]]))
(is (= (d/q '[:find [?tx-data ...]
:where
[(hash-map :db/id -1 :age 92 :name "Aaron") ?tx-data]])
[{:db/id -1 :age 92 :name "Aaron"}])))
(testing "Passing predicate as source"
(is (= (d/q '[:find ?e
:in $ ?adult
:where [?e :age ?a]
[(?adult ?a)]]
db
#(> % 18))
#{[2] [3]})))
(testing "Calling a function"
(is (= (d/q '[:find ?e1 ?e2 ?e3
:where [?e1 :age ?a1]
[?e2 :age ?a2]
[?e3 :age ?a3]
[(+ ?a1 ?a2) ?a12]
[(= ?a12 ?a3)]]
db)
#{[1 2 3] [2 1 3]})))
(testing "Two conflicting function values for one binding."
(is (= (d/q '[:find ?n
:where [(identity 1) ?n]
[(identity 2) ?n]])
#{})))
(testing "Destructured conflicting function values for two bindings."
(is (= (d/q '[:find ?n ?x
:where [(identity [3 4]) [?n ?x]]
[(identity [1 2]) [?n ?x]]])
#{})))
(testing "Rule bindings interacting with function binding. (fn, rule)"
(is (= (d/q '[:find ?n
:in $ %
:where [(identity 2) ?n]
(my-vals ?n)]
db
'[[(my-vals ?x)
[(identity 1) ?x]]
[(my-vals ?x)
[(identity 2) ?x]]
[(my-vals ?x)
[(identity 3) ?x]]])
#{[2]})))
(testing "Rule bindings interacting with function binding. (rule, fn)"
(is (= (d/q '[:find ?n
:in $ %
:where (my-vals ?n)
[(identity 2) ?n]]
db
'[[(my-vals ?x)
[(identity 1) ?x]]
[(my-vals ?x)
[(identity 2) ?x]]
[(my-vals ?x)
[(identity 3) ?x]]])
#{[2]})))
(testing "Conflicting relational bindings with function binding. (rel, fn)"
(is (= (d/q '[:find ?age
:where [_ :age ?age]
[(identity 100) ?age]]
db)
#{})))
(testing "Conflicting relational bindings with function binding. (fn, rel)"
(is (= (d/q '[:find ?age
:where [(identity 100) ?age]
[_ :age ?age]]
db)
#{})))
(testing "Function on empty rel"
(is (= (d/q '[:find ?e ?y
:where [?e :salary ?x]
[(+ ?x 100) ?y]]
[[0 :age 15] [1 :age 35]])
#{})))
(testing "Returning nil from function filters out tuple from result"
(is (= (d/q '[:find ?x
:in [?in ...] ?f
:where [(?f ?in) ?x]]
[1 2 3 4]
#(when (even? %) %))
#{[2] [4]})))
(testing "Result bindings"
(is (= (d/q '[:find ?a ?c
:in ?in
:where [(ground ?in) [?a _ ?c]]]
[:a :b :c])
#{[:a :c]}))
(is (= (d/q '[:find ?in
:in ?in
:where [(ground ?in) _]]
:a)
#{[:a]}))
(is (= (d/q '[:find ?x ?z
:in ?in
:where [(ground ?in) [[?x _ ?z]...]]]
[[:a :b :c] [:d :e :f]])
#{[:a :c] [:d :f]}))
(is (= (d/q '[:find ?in
:in [?in ...]
:where [(ground ?in) _]]
[])
#{})))
))
(deftest test-predicates
(let [entities [{:db/id 1 :name "Ivan" :age 10}
{:db/id 2 :name "Ivan" :age 20}
{:db/id 3 :name "Oleg" :age 10}
{:db/id 4 :name "Oleg" :age 20}]
db (d/db-with (d/empty-db) entities)]
(are [q res] (= (d/q (quote q) db) res)
;; plain predicate
[:find ?e ?a
:where [?e :age ?a]
[(> ?a 10)]]
#{[2 20] [4 20]}
;; join in predicate
[:find ?e ?e2
:where [?e :name]
[?e2 :name]
[(< ?e ?e2)]]
#{[1 2] [1 3] [1 4] [2 3] [2 4] [3 4]}
;; join with extra symbols
[:find ?e ?e2
:where [?e :age ?a]
[?e2 :age ?a2]
[(< ?e ?e2)]]
#{[1 2] [1 3] [1 4] [2 3] [2 4] [3 4]}
;; empty result
[:find ?e ?e2
:where [?e :name "Ivan"]
[?e2 :name "Oleg"]
[(= ?e ?e2)]]
#{}
;; pred over const, true
[:find ?e
:where [?e :name "Ivan"]
[?e :age 20]
[(= ?e 2)]]
#{[2]}
;; pred over const, false
[:find ?e
:where [?e :name "Ivan"]
[?e :age 20]
[(= ?e 1)]]
#{})
(let [pred (fn [db e a]
(= a (:age (d/entity db e))))]
(is (= (d/q '[:find ?e
:in $ ?pred
:where [?e :age ?a]
[(?pred $ ?e 10)]]
db pred)
#{[1] [3]})))))
(deftest test-exceptions
(is (thrown-msg? "Unknown predicate 'fun in [(fun ?e)]"
(d/q '[:find ?e
:in [?e ...]
:where [(fun ?e)]]
[1])))
(is (thrown-msg? "Unknown function 'fun in [(fun ?e) ?x]"
(d/q '[:find ?e ?x
:in [?e ...]
:where [(fun ?e) ?x]]
[1])))
(is (thrown-msg? "Insufficient bindings: #{?x} not bound in [(zero? ?x)]"
(d/q '[:find ?x
:where [(zero? ?x)]])))
(is (thrown-msg? "Insufficient bindings: #{?x} not bound in [(inc ?x) ?y]"
(d/q '[:find ?x
:where [(inc ?x) ?y]])))
(is (thrown-msg? "Where uses unknown source vars: [$2]"
(d/q '[:find ?x
:where [?x] [(zero? $2 ?x)]])))
(is (thrown-msg? "Where uses unknown source vars: [$]"
(d/q '[:find ?x
:in $2
:where [$2 ?x] [(zero? $ ?x)]]))))
(deftest test-issue-180
(is (= #{}
(d/q '[:find ?e ?a
:where [_ :pred ?pred]
[?e :age ?a]
[(?pred ?a)]]
(d/db-with (d/empty-db) [[:db/add 1 :age 20]])))))
(defn sample-query-fn [] 42)
#?(:clj
(deftest test-symbol-resolution
(is (= 42 (d/q '[:find ?x .
:where [(datascript.test.query-fns/sample-query-fn) ?x]])))))
(deftest test-issue-445
(let [db (-> (d/empty-db {:name {:db/unique :db.unique/identity}})
(d/db-with [{:db/id 1 :name "Ivan" :age 15}
{:db/id 2 :name "Petr" :age 22 :height 240}]))]
(testing "get-else using lookup ref"
(is (= "Unknown"
(d/q '[:find ?height .
:in $ ?e
:where [(get-else $ ?e :height "Unknown") ?height]]
db
[:name "Ivan"]))))
(testing "get-some using lookup ref"
(is (= #{[[:name "Petr"] :age 22]}
(d/q '[:find ?e ?a ?v
:in $ ?e
:where [(get-some $ ?e :weight :age :height) [?a ?v]]]
db
[:name "Petr"]))))))