Skip to content

Commit fd262bd

Browse files
committed
Wrap test databases into delay
1 parent 2094102 commit fd262bd

11 files changed

+151
-143
lines changed

.github/workflows/build_test.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ jobs:
1414
test:
1515
runs-on: ubuntu-latest
1616
steps:
17-
- uses: actions/checkout@v3
17+
- uses: actions/checkout@v4
1818
- name: Choose Java 11
1919
run: |
2020
echo "JAVA_HOME=$JAVA_HOME_11_X64" >> $GITHUB_ENV
2121
echo "$JAVA_HOME_11_X64/bin" >> $GITHUB_PATH
22+
- name: Setup Clojure
23+
uses: DeLaGuardo/[email protected]
24+
with:
25+
cli: latest
2226
- run: ./script/test_clj.sh
2327
- run: ./script/test_cljs.sh
2428
- run: ./script/test_js.sh

test/datascript/test/ident.cljc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@
33
[clojure.test :as t :refer [is are deftest testing]]
44
[datascript.core :as d]))
55

6-
(def db
7-
(-> (d/empty-db {:ref {:db/valueType :db.type/ref}})
8-
(d/db-with [[:db/add 1 :db/ident :ent1]
9-
[:db/add 2 :db/ident :ent2]
10-
[:db/add 2 :ref 1]])))
6+
(def *db
7+
(delay
8+
(-> (d/empty-db {:ref {:db/valueType :db.type/ref}})
9+
(d/db-with [[:db/add 1 :db/ident :ent1]
10+
[:db/add 2 :db/ident :ent2]
11+
[:db/add 2 :ref 1]]))))
1112

1213
(deftest test-q
1314
(is (= 1 (d/q '[:find ?v .
14-
:where [:ent2 :ref ?v]] db)))
15+
:where [:ent2 :ref ?v]] @*db)))
1516
(is (= 2 (d/q '[:find ?f .
16-
:where [?f :ref :ent1]] db))))
17+
:where [?f :ref :ent1]] @*db))))
1718

1819
(deftest test-transact!
19-
(let [db' (d/db-with db [[:db/add :ent1 :ref :ent2]])]
20+
(let [db' (d/db-with @*db [[:db/add :ent1 :ref :ent2]])]
2021
(is (= 2 (-> (d/entity db' :ent1) :ref :db/id)))))
2122

2223
(deftest test-entity
2324
(is (= {:db/ident :ent1}
24-
(into {} (d/entity db :ent1)))))
25+
(into {} (d/entity @*db :ent1)))))
2526

2627
(deftest test-pull
2728
(is (= {:db/id 1, :db/ident :ent1}
28-
(d/pull db '[*] :ent1))))
29+
(d/pull @*db '[*] :ent1))))

test/datascript/test/pull_api.cljc

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -60,43 +60,44 @@
6060
[16 :part 18]]
6161
(map #(apply d/datom %))))
6262

63-
(def ^:private test-db
64-
(d/init-db test-datoms test-schema))
63+
(def ^:private *test-db
64+
(delay
65+
(d/init-db test-datoms test-schema)))
6566

6667
(deftest test-pull-attr-spec
6768
(is (= {:name "Petr" :aka ["Devil" "Tupen"]}
68-
(d/pull test-db '[:name :aka] 1)))
69+
(d/pull @*test-db '[:name :aka] 1)))
6970

7071
(is (= {:name "Matthew" :father {:db/id 3} :db/id 6}
71-
(d/pull test-db '[:name :father :db/id] 6)))
72+
(d/pull @*test-db '[:name :father :db/id] 6)))
7273

7374
(is (= [{:name "Petr"} {:name "Elizabeth"}
7475
{:name "Eunan"} {:name "Rebecca"}]
75-
(d/pull-many test-db '[:name] [1 5 7 9]))))
76+
(d/pull-many @*test-db '[:name] [1 5 7 9]))))
7677

7778
(deftest test-pull-reverse-attr-spec
7879
(is (= {:name "David" :_child [{:db/id 1}]}
79-
(d/pull test-db '[:name :_child] 2)))
80+
(d/pull @*test-db '[:name :_child] 2)))
8081

8182
(is (= {:name "David" :_child [{:name "Petr"}]}
82-
(d/pull test-db '[:name {:_child [:name]}] 2)))
83+
(d/pull @*test-db '[:name {:_child [:name]}] 2)))
8384

8485
(testing "Reverse non-component references yield collections"
8586
(is (= {:name "Thomas" :_father [{:db/id 6}]}
86-
(d/pull test-db '[:name :_father] 3)))
87+
(d/pull @*test-db '[:name :_father] 3)))
8788

8889
(is (= {:name "Petr" :_father [{:db/id 2} {:db/id 3}]}
89-
(d/pull test-db '[:name :_father] 1)))
90+
(d/pull @*test-db '[:name :_father] 1)))
9091

9192
(is (= {:name "Thomas" :_father [{:name "Matthew"}]}
92-
(d/pull test-db '[:name {:_father [:name]}] 3)))
93+
(d/pull @*test-db '[:name {:_father [:name]}] 3)))
9394

9495
(is (= {:name "Petr" :_father [{:name "David"} {:name "Thomas"}]}
95-
(d/pull test-db '[:name {:_father [:name]}] 1))))
96+
(d/pull @*test-db '[:name {:_father [:name]}] 1))))
9697

9798
(testing "Multiple reverse refs issue-412"
9899
(is (= {:name "Petr" :_father [{:db/id 2} {:db/id 3}]}
99-
(d/pull test-db '[:name :_father :_child] 1)))))
100+
(d/pull @*test-db '[:name :_father :_child] 1)))))
100101

101102
(deftest test-pull-component-attr
102103
(let [parts {:name "Part A",
@@ -132,45 +133,45 @@
132133
test-schema)]
133134

134135
(testing "Component entities are expanded recursively"
135-
(is (= parts (d/pull test-db '[:name :part] 10))))
136+
(is (= parts (d/pull @*test-db '[:name :part] 10))))
136137

137138
(testing "Reverse component references yield a single result"
138139
(is (= {:name "Part A.A" :_part {:db/id 10}}
139-
(d/pull test-db [:name :_part] 11)))
140+
(d/pull @*test-db [:name :_part] 11)))
140141

141142
(is (= {:name "Part A.A" :_part {:name "Part A"}}
142-
(d/pull test-db [:name {:_part [:name]}] 11))))
143+
(d/pull @*test-db [:name {:_part [:name]}] 11))))
143144

144145
(testing "Like explicit recursion, expansion will not allow loops"
145146
(is (= rpart (d/pull recdb '[:name :part] 10))))
146147

147148
(testing "Reverse recursive component issue-411"
148149
(is (= {:name "Part A.A.A.B" :_part {:name "Part A.A.A" :_part {:name "Part A.A" :_part {:name "Part A"}}}}
149-
(d/pull test-db '[:name {:_part ...}] 14)))
150+
(d/pull @*test-db '[:name {:_part ...}] 14)))
150151
(is (= {:name "Part A.A.A.B" :_part {:name "Part A.A.A" :_part {:name "Part A.A"}}}
151-
(d/pull test-db '[:name {:_part 2}] 14))))))
152+
(d/pull @*test-db '[:name {:_part 2}] 14))))))
152153

153154
(deftest test-pull-wildcard
154155
(is (= {:db/id 1
155156
:name "Petr"
156157
:aka ["Devil" "Tupen"]
157158
:child [{:db/id 2} {:db/id 3}]}
158-
(d/pull test-db '[*] 1)))
159+
(d/pull @*test-db '[*] 1)))
159160

160161
(is (= {:db/id 2 :name "David" :_child [{:db/id 1}] :father {:db/id 1}}
161-
(d/pull test-db '[* :_child] 2)))
162+
(d/pull @*test-db '[* :_child] 2)))
162163

163164
(is (= {:aka ["Devil" "Tupen"], :child [{:db/id 2} {:db/id 3}], :name "Petr", :db/id 1}
164-
(d/pull test-db '[:name *] 1)))
165+
(d/pull @*test-db '[:name *] 1)))
165166

166167
(is (= {:aka ["Devil" "Tupen"], :child [{:db/id 2} {:db/id 3}], :name "Petr", :db/id 1}
167-
(d/pull test-db '[:aka :name *] 1)))
168+
(d/pull @*test-db '[:aka :name *] 1)))
168169

169170
(is (= {:aka ["Devil" "Tupen"], :child [{:db/id 2} {:db/id 3}], :name "Petr", :db/id 1}
170-
(d/pull test-db '[:aka :child :name *] 1)))
171+
(d/pull @*test-db '[:aka :child :name *] 1)))
171172

172173
(is (= {:alias ["Devil" "Tupen"], :child [{:db/id 2} {:db/id 3}], :first-name "Petr", :db/id 1}
173-
(d/pull test-db '[[:aka :as :alias] [:name :as :first-name] *] 1)))
174+
(d/pull @*test-db '[[:aka :as :alias] [:name :as :first-name] *] 1)))
174175

175176
(is (= {:db/id 1
176177
:name "Petr"
@@ -181,7 +182,7 @@
181182
{:db/id 3
182183
:father {:db/id 1}
183184
:name "Thomas"}]}
184-
(d/pull test-db '[* {:child ...}] 1))))
185+
(d/pull @*test-db '[* {:child ...}] 1))))
185186

186187
(deftest test-pull-limit
187188
(let [db (d/init-db
@@ -215,75 +216,75 @@
215216

216217
(deftest test-pull-default
217218
(testing "Empty results return nil"
218-
(is (nil? (d/pull test-db '[:foo] 1))))
219+
(is (nil? (d/pull @*test-db '[:foo] 1))))
219220

220221
(testing "A default can be used to replace nil results"
221222
(is (= {:foo "bar"}
222-
(d/pull test-db '[(default :foo "bar")] 1)))
223+
(d/pull @*test-db '[(default :foo "bar")] 1)))
223224
(is (= {:foo "bar"}
224-
(d/pull test-db '[[:foo :default "bar"]] 1)))
225+
(d/pull @*test-db '[[:foo :default "bar"]] 1)))
225226
(is (= {:foo false}
226-
(d/pull test-db '[[:foo :default false]] 1)))
227+
(d/pull @*test-db '[[:foo :default false]] 1)))
227228
(is (= {:bar false}
228-
(d/pull test-db '[[:foo :as :bar :default false]] 1))))
229+
(d/pull @*test-db '[[:foo :as :bar :default false]] 1))))
229230

230231
(testing "default does not override results"
231232
(is (= {:name "Petr", :aka ["Devil" "Tupen"]
232233
:child [{:name "David", :aka "[aka]", :child "[child]"}
233234
{:name "Thomas", :aka "[aka]", :child "[child]"}]}
234-
(d/pull test-db
235+
(d/pull @*test-db
235236
'[[:name :default "[name]"]
236237
[:aka :default "[aka]"]
237238
{[:child :default "[child]"] ...}]
238239
1)))
239240
(is (= {:name "David", :aka "[aka]", :child "[child]"}
240-
(d/pull test-db
241+
(d/pull @*test-db
241242
'[[:name :default "[name]"]
242243
[:aka :default "[aka]"]
243244
{[:child :default "[child]"] ...}]
244245
2))))
245246

246247
(testing "Ref default"
247248
(is (= {:child 1 :db/id 2}
248-
(d/pull test-db '[:db/id [:child :default 1]] 2)))
249+
(d/pull @*test-db '[:db/id [:child :default 1]] 2)))
249250
(is (= {:_child 2 :db/id 1}
250-
(d/pull test-db '[:db/id [:_child :default 2]] 1)))))
251+
(d/pull @*test-db '[:db/id [:_child :default 2]] 1)))))
251252

252253
(deftest test-pull-as
253254
(is (= {"Name" "Petr", :alias ["Devil" "Tupen"]}
254-
(d/pull test-db '[[:name :as "Name"] [:aka :as :alias]] 1))))
255+
(d/pull @*test-db '[[:name :as "Name"] [:aka :as :alias]] 1))))
255256

256257
(deftest test-pull-attr-with-opts
257258
(is (= {"Name" "Nothing"}
258-
(d/pull test-db '[[:x :as "Name" :default "Nothing"]] 1))))
259+
(d/pull @*test-db '[[:x :as "Name" :default "Nothing"]] 1))))
259260

260261
(deftest test-pull-map
261262
(testing "Single attrs yield a map"
262263
(is (= {:name "Matthew" :father {:name "Thomas"}}
263-
(d/pull test-db '[:name {:father [:name]}] 6))))
264+
(d/pull @*test-db '[:name {:father [:name]}] 6))))
264265

265266
(testing "Multi attrs yield a collection of maps"
266267
(is (= {:name "Petr" :child [{:name "David"}
267268
{:name "Thomas"}]}
268-
(d/pull test-db '[:name {:child [:name]}] 1))))
269+
(d/pull @*test-db '[:name {:child [:name]}] 1))))
269270

270271
(testing "Missing attrs are dropped"
271272
(is (= {:name "Petr"}
272-
(d/pull test-db '[:name {:father [:name]}] 1))))
273+
(d/pull @*test-db '[:name {:father [:name]}] 1))))
273274

274275
(testing "Non matching results are removed from collections"
275276
(is (= {:name "Petr"}
276-
(d/pull test-db '[:name {:child [:foo]}] 1))))
277+
(d/pull @*test-db '[:name {:child [:foo]}] 1))))
277278

278279
(testing "Map specs can override component expansion"
279280
(is (= {:name "Part A" :part [{:name "Part A.A"} {:name "Part A.B"}]}
280-
(d/pull test-db '[:name {:part [:name]}] 10)))
281+
(d/pull @*test-db '[:name {:part [:name]}] 10)))
281282

282283
(is (= {:name "Part A" :part [{:name "Part A.A"} {:name "Part A.B"}]}
283-
(d/pull test-db '[:name {:part 1}] 10)))))
284+
(d/pull @*test-db '[:name {:part 1}] 10)))))
284285

285286
(deftest test-pull-recursion
286-
(let [db (-> test-db
287+
(let [db (-> @*test-db
287288
(d/db-with [[:db/add 4 :friend 5]
288289
[:db/add 5 :friend 6]
289290
[:db/add 6 :friend 7]
@@ -448,26 +449,26 @@
448449

449450
(deftest test-lookup-ref-pull
450451
(is (= {:name "Petr" :aka ["Devil" "Tupen"]}
451-
(d/pull test-db '[:name :aka] [:name "Petr"])))
452+
(d/pull @*test-db '[:name :aka] [:name "Petr"])))
452453
(is (= nil
453-
(d/pull test-db '[:name :aka] [:name "NotInDatabase"])))
454+
(d/pull @*test-db '[:name :aka] [:name "NotInDatabase"])))
454455
(is (= [nil {:aka ["Devil" "Tupen"]} nil nil nil]
455-
(d/pull-many test-db
456+
(d/pull-many @*test-db
456457
'[:aka]
457458
[[:name "Elizabeth"]
458459
[:name "Petr"]
459460
[:name "Eunan"]
460461
[:name "Rebecca"]
461462
[:name "Unknown"]])))
462-
(is (nil? (d/pull test-db '[*] [:name "No such name"]))))
463+
(is (nil? (d/pull @*test-db '[*] [:name "No such name"]))))
463464

464465
(deftest test-xform
465466
(is (= {:db/id [1]
466467
:name ["Petr"]
467468
:aka [["Devil" "Tupen"]]
468469
:child [[{:db/id [2], :name ["David"], :aka [nil], :child [nil]}
469470
{:db/id [3], :name ["Thomas"], :aka [nil], :child [nil]}]]}
470-
(d/pull test-db
471+
(d/pull @*test-db
471472
[[:db/id :xform vector]
472473
[:name :xform vector]
473474
[:aka :xform vector]
@@ -476,35 +477,35 @@
476477

477478
(testing ":xform on cardinality/one ref issue-455"
478479
(is (= {:name "David" :father "Petr"}
479-
(d/pull test-db [:name {[:father :xform #(:name %)] ['*]}] 2))))
480+
(d/pull @*test-db [:name {[:father :xform #(:name %)] ['*]}] 2))))
480481

481482
(testing ":xform on reverse ref"
482483
(is (= {:name "Petr" :_father ["David" "Thomas"]}
483-
(d/pull test-db [:name {[:_father :xform #(mapv :name %)] [:name]}] 1))))
484+
(d/pull @*test-db [:name {[:_father :xform #(mapv :name %)] [:name]}] 1))))
484485

485486
(testing ":xform on reverse component ref"
486487
(is (= {:name "Part A.A" :_part "Part A"}
487-
(d/pull test-db [:name {[:_part :xform #(:name %)] [:name]}] 11))))
488+
(d/pull @*test-db [:name {[:_part :xform #(:name %)] [:name]}] 11))))
488489

489490
(testing "missing attrs are processed by xform"
490491
(is (= {:normal [nil]
491492
:aka [nil]
492493
:child [nil]}
493-
(d/pull test-db
494+
(d/pull @*test-db
494495
'[[:normal :xform vector]
495496
[:aka :xform vector]
496497
{[:child :xform vector] ...}]
497498
2))))
498499
(testing "default takes precedence"
499500
(is (= {:unknown "[unknown]"}
500-
(d/pull test-db '[[:unknown :default "[unknown]" :xform vector]] 1)))))
501+
(d/pull @*test-db '[[:unknown :default "[unknown]" :xform vector]] 1)))))
501502

502503
(deftest test-visitor
503504
(let [*trace (volatile! nil)
504505
opts {:visitor (fn [k e a v] (vswap! *trace conj [k e a v]))}
505506
test-fn (fn [pattern id]
506507
(vreset! *trace [])
507-
(d/pull test-db pattern id opts)
508+
(d/pull @*test-db pattern id opts)
508509
@*trace)]
509510
(is (= [[:db.pull/attr 1 :name nil]]
510511
(test-fn [:name] 1)))
@@ -563,13 +564,13 @@
563564
(test-fn [:name :_child] 2))))))
564565

565566
(deftest test-pull-other-dbs
566-
(let [db (-> test-db
567+
(let [db (-> @*test-db
567568
(d/filter (fn [_ datom] (not= "Tupen" (:v datom)))))]
568569
(is (= {:name "Petr" :aka ["Devil"]}
569570
(d/pull db '[:name :aka] 1))))
570-
(let [db (-> test-db d/serializable pr-str clojure.edn/read-string d/from-serializable)]
571+
(let [db (-> @*test-db d/serializable pr-str clojure.edn/read-string d/from-serializable)]
571572
(is (= {:name "Petr" :aka ["Devil" "Tupen"]}
572573
(d/pull db '[:name :aka] 1))))
573-
(let [db (d/init-db (d/datoms test-db :eavt) test-schema)]
574+
(let [db (d/init-db (d/datoms @*test-db :eavt) test-schema)]
574575
(is (= {:name "Petr" :aka ["Devil" "Tupen"]}
575576
(d/pull db '[:name :aka] 1)))))

0 commit comments

Comments
 (0)