forked from tonsky/datascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull_api.cljc
327 lines (283 loc) · 11.8 KB
/
pull_api.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
(ns datascript.test.pull-api
(: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]))
(def ^:private test-schema
{:aka { :db/cardinality :db.cardinality/many }
:child { :db/cardinality :db.cardinality/many
:db/valueType :db.type/ref }
:friend { :db/cardinality :db.cardinality/many
:db/valueType :db.type/ref }
:enemy { :db/cardinality :db.cardinality/many
:db/valueType :db.type/ref }
:father { :db/valueType :db.type/ref }
:part { :db/valueType :db.type/ref
:db/isComponent true
:db/cardinality :db.cardinality/many }
:spec { :db/valueType :db.type/ref
:db/isComponent true
:db/cardinality :db.cardinality/one }})
(def test-datoms
(->>
[[1 :name "Petr"]
[1 :aka "Devil"]
[1 :aka "Tupen"]
[2 :name "David"]
[3 :name "Thomas"]
[4 :name "Lucy"]
[5 :name "Elizabeth"]
[6 :name "Matthew"]
[7 :name "Eunan"]
[8 :name "Kerri"]
[9 :name "Rebecca"]
[1 :child 2]
[1 :child 3]
[2 :father 1]
[3 :father 1]
[6 :father 3]
[10 :name "Part A"]
[11 :name "Part A.A"]
[10 :part 11]
[12 :name "Part A.A.A"]
[11 :part 12]
[13 :name "Part A.A.A.A"]
[12 :part 13]
[14 :name "Part A.A.A.B"]
[12 :part 14]
[15 :name "Part A.B"]
[10 :part 15]
[16 :name "Part A.B.A"]
[15 :part 16]
[17 :name "Part A.B.A.A"]
[16 :part 17]
[18 :name "Part A.B.A.B"]
[16 :part 18]]
(map #(apply d/datom %))))
(def ^:private test-db (d/init-db test-datoms test-schema))
(deftest test-pull-attr-spec
(is (= {:name "Petr" :aka ["Devil" "Tupen"]}
(d/pull test-db '[:name :aka] 1)))
(is (= {:name "Matthew" :father {:db/id 3} :db/id 6}
(d/pull test-db '[:name :father :db/id] 6)))
(is (= [{:name "Petr"} {:name "Elizabeth"}
{:name "Eunan"} {:name "Rebecca"}]
(d/pull-many test-db '[:name] [1 5 7 9]))))
(deftest test-pull-reverse-attr-spec
(is (= {:name "David" :_child [{:db/id 1}]}
(d/pull test-db '[:name :_child] 2)))
(is (= {:name "David" :_child [{:name "Petr"}]}
(d/pull test-db '[:name {:_child [:name]}] 2)))
(testing "Reverse non-component references yield collections"
(is (= {:name "Thomas" :_father [{:db/id 6}]}
(d/pull test-db '[:name :_father] 3)))
(is (= {:name "Petr" :_father [{:db/id 2} {:db/id 3}]}
(d/pull test-db '[:name :_father] 1)))
(is (= {:name "Thomas" :_father [{:name "Matthew"}]}
(d/pull test-db '[:name {:_father [:name]}] 3)))
(is (= {:name "Petr" :_father [{:name "David"} {:name "Thomas"}]}
(d/pull test-db '[:name {:_father [:name]}] 1)))))
(deftest test-pull-component-attr
(let [parts {:name "Part A",
:part
[{:db/id 11
:name "Part A.A",
:part
[{:db/id 12
:name "Part A.A.A",
:part
[{:db/id 13 :name "Part A.A.A.A"}
{:db/id 14 :name "Part A.A.A.B"}]}]}
{:db/id 15
:name "Part A.B",
:part
[{:db/id 16
:name "Part A.B.A",
:part
[{:db/id 17 :name "Part A.B.A.A"}
{:db/id 18 :name "Part A.B.A.B"}]}]}]}
rpart (update-in parts [:part 0 :part 0 :part]
(partial into [{:db/id 10}]))
recdb (d/init-db
(concat test-datoms [(d/datom 12 :part 10)])
test-schema)
mutdb (d/init-db
(concat test-datoms [(d/datom 12 :part 10)
(d/datom 12 :spec 10)
(d/datom 10 :spec 13)
(d/datom 13 :spec 12)])
test-schema)]
(testing "Component entities are expanded recursively"
(is (= parts (d/pull test-db '[:name :part] 10))))
(testing "Reverse component references yield a single result"
(is (= {:name "Part A.A" :_part {:db/id 10}}
(d/pull test-db [:name :_part] 11)))
(is (= {:name "Part A.A" :_part {:name "Part A"}}
(d/pull test-db [:name {:_part [:name]}] 11))))
(testing "Like explicit recursion, expansion will not allow loops"
(is (= rpart (d/pull recdb '[:name :part] 10))))))
(deftest test-pull-wildcard
(is (= {:db/id 1 :name "Petr" :aka ["Devil" "Tupen"]
:child [{:db/id 2} {:db/id 3}]}
(d/pull test-db '[*] 1)))
(is (= {:db/id 2 :name "David" :_child [{:db/id 1}] :father {:db/id 1}}
(d/pull test-db '[* :_child] 2))))
(deftest test-pull-limit
(let [db (d/init-db
(concat
test-datoms
[(d/datom 4 :friend 5)
(d/datom 4 :friend 6)
(d/datom 4 :friend 7)
(d/datom 4 :friend 8)]
(for [idx (range 2000)]
(d/datom 8 :aka (str "aka-" idx))))
test-schema)]
(testing "Without an explicit limit, the default is 1000"
(is (= 1000 (->> (d/pull db '[:aka] 8) :aka count))))
(testing "Explicit limit can reduce the default"
(is (= 500 (->> (d/pull db '[(limit :aka 500)] 8) :aka count)))
(is (= 500 (->> (d/pull db '[[:aka :limit 500]] 8) :aka count))))
(testing "Explicit limit can increase the default"
(is (= 1500 (->> (d/pull db '[(limit :aka 1500)] 8) :aka count))))
(testing "A nil limit produces unlimited results"
(is (= 2000 (->> (d/pull db '[(limit :aka nil)] 8) :aka count))))
(testing "Limits can be used as map specification keys"
(is (= {:name "Lucy"
:friend [{:name "Elizabeth"} {:name "Matthew"}]}
(d/pull db '[:name {(limit :friend 2) [:name]}] 4))))))
(deftest test-pull-default
(testing "Empty results return nil"
(is (nil? (d/pull test-db '[:foo] 1))))
(testing "A default can be used to replace nil results"
(is (= {:foo "bar"}
(d/pull test-db '[(default :foo "bar")] 1)))
(is (= {:foo "bar"}
(d/pull test-db '[[:foo :default "bar"]] 1)))))
(deftest test-pull-as
(is (= {"Name" "Petr", :alias ["Devil" "Tupen"]}
(d/pull test-db '[[:name :as "Name"] [:aka :as :alias]] 1))))
(deftest test-pull-attr-with-opts
(is (= {"Name" "Nothing"}
(d/pull test-db '[[:x :as "Name" :default "Nothing"]] 1))))
(deftest test-pull-map
(testing "Single attrs yield a map"
(is (= {:name "Matthew" :father {:name "Thomas"}}
(d/pull test-db '[:name {:father [:name]}] 6))))
(testing "Multi attrs yield a collection of maps"
(is (= {:name "Petr" :child [{:name "David"}
{:name "Thomas"}]}
(d/pull test-db '[:name {:child [:name]}] 1))))
(testing "Missing attrs are dropped"
(is (= {:name "Petr"}
(d/pull test-db '[:name {:father [:name]}] 1))))
(testing "Non matching results are removed from collections"
(is (= {:name "Petr" :child []}
(d/pull test-db '[:name {:child [:foo]}] 1))))
(testing "Map specs can override component expansion"
(let [parts {:name "Part A" :part [{:name "Part A.A"} {:name "Part A.B"}]}]
(is (= parts
(d/pull test-db '[:name {:part [:name]}] 10)))
(is (= parts
(d/pull test-db '[:name {:part 1}] 10))))))
(deftest test-pull-recursion
(let [db (-> test-db
(d/db-with [[:db/add 4 :friend 5]
[:db/add 5 :friend 6]
[:db/add 6 :friend 7]
[:db/add 7 :friend 8]
[:db/add 4 :enemy 6]
[:db/add 5 :enemy 7]
[:db/add 6 :enemy 8]
[:db/add 7 :enemy 4]]))
friends {:db/id 4
:name "Lucy"
:friend
[{:db/id 5
:name "Elizabeth"
:friend
[{:db/id 6
:name "Matthew"
:friend
[{:db/id 7
:name "Eunan"
:friend
[{:db/id 8
:name "Kerri"}]}]}]}]}
enemies {:db/id 4 :name "Lucy"
:friend
[{:db/id 5 :name "Elizabeth"
:friend
[{:db/id 6 :name "Matthew"
:enemy [{:db/id 8 :name "Kerri"}]}]
:enemy
[{:db/id 7 :name "Eunan"
:friend
[{:db/id 8 :name "Kerri"}]
:enemy
[{:db/id 4 :name "Lucy"
:friend [{:db/id 5}]}]}]}]
:enemy
[{:db/id 6 :name "Matthew"
:friend
[{:db/id 7 :name "Eunan"
:friend
[{:db/id 8 :name "Kerri"}]
:enemy [{:db/id 4 :name "Lucy"
:friend [{:db/id 5 :name "Elizabeth"}]}]}]
:enemy
[{:db/id 8 :name "Kerri"}]}]}]
(testing "Infinite recursion"
(is (= friends (d/pull db '[:db/id :name {:friend ...}] 4))))
(testing "Multiple recursion specs in one pattern"
(is (= enemies (d/pull db '[:db/id :name {:friend 2 :enemy 2}] 4))))
(let [db (d/db-with db [[:db/add 8 :friend 4]])]
(testing "Cycles are handled by returning only the :db/id of entities which have been seen before"
(is (= (update-in friends (take 8 (cycle [:friend 0]))
assoc :friend [{:db/id 4 :name "Lucy" :friend [{:db/id 5}]}])
(d/pull db '[:db/id :name {:friend ...}] 4)))))))
(deftest test-dual-recursion
(let [empty (d/empty-db {:part { :db/valueType :db.type/ref }
:spec { :db/valueType :db.type/ref }})]
(let [db (d/db-with empty [[:db/add 1 :part 2]
[:db/add 2 :part 3]
[:db/add 3 :part 1]
[:db/add 1 :spec 2]
[:db/add 2 :spec 1]])]
(is (= (d/pull db '[:db/id {:part ...} {:spec ...}] 1)
{:db/id 1,
:spec {:db/id 2
:spec {:db/id 1,
:spec {:db/id 2}, :part {:db/id 2}}
:part {:db/id 3,
:part {:db/id 1,
:spec {:db/id 2},
:part {:db/id 2}}}}
:part {:db/id 2
:spec {:db/id 1, :spec {:db/id 2}, :part {:db/id 2}}
:part {:db/id 3,
:part {:db/id 1,
:spec {:db/id 2},
:part {:db/id 2}}}}})))))
(deftest test-deep-recursion
(let [start 100
depth 1500
txd (mapcat
(fn [idx]
[(d/datom idx :name (str "Person-" idx))
(d/datom (dec idx) :friend idx)])
(range (inc start) depth))
db (d/init-db (concat
test-datoms
[(d/datom start :name (str "Person-" start))]
txd)
test-schema)
pulled (d/pull db '[:name {:friend ...}] start)
path (->> [:friend 0]
(repeat (dec (- depth start)))
(into [] cat))]
(is (= (str "Person-" (dec depth))
(:name (get-in pulled path))))))
#_(t/test-ns 'datascript.test.pull-api)