|
60 | 60 | [16 :part 18]] |
61 | 61 | (map #(apply d/datom %)))) |
62 | 62 |
|
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))) |
65 | 66 |
|
66 | 67 | (deftest test-pull-attr-spec |
67 | 68 | (is (= {:name "Petr" :aka ["Devil" "Tupen"]} |
68 | | - (d/pull test-db '[:name :aka] 1))) |
| 69 | + (d/pull @*test-db '[:name :aka] 1))) |
69 | 70 |
|
70 | 71 | (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))) |
72 | 73 |
|
73 | 74 | (is (= [{:name "Petr"} {:name "Elizabeth"} |
74 | 75 | {: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])))) |
76 | 77 |
|
77 | 78 | (deftest test-pull-reverse-attr-spec |
78 | 79 | (is (= {:name "David" :_child [{:db/id 1}]} |
79 | | - (d/pull test-db '[:name :_child] 2))) |
| 80 | + (d/pull @*test-db '[:name :_child] 2))) |
80 | 81 |
|
81 | 82 | (is (= {:name "David" :_child [{:name "Petr"}]} |
82 | | - (d/pull test-db '[:name {:_child [:name]}] 2))) |
| 83 | + (d/pull @*test-db '[:name {:_child [:name]}] 2))) |
83 | 84 |
|
84 | 85 | (testing "Reverse non-component references yield collections" |
85 | 86 | (is (= {:name "Thomas" :_father [{:db/id 6}]} |
86 | | - (d/pull test-db '[:name :_father] 3))) |
| 87 | + (d/pull @*test-db '[:name :_father] 3))) |
87 | 88 |
|
88 | 89 | (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))) |
90 | 91 |
|
91 | 92 | (is (= {:name "Thomas" :_father [{:name "Matthew"}]} |
92 | | - (d/pull test-db '[:name {:_father [:name]}] 3))) |
| 93 | + (d/pull @*test-db '[:name {:_father [:name]}] 3))) |
93 | 94 |
|
94 | 95 | (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)))) |
96 | 97 |
|
97 | 98 | (testing "Multiple reverse refs issue-412" |
98 | 99 | (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))))) |
100 | 101 |
|
101 | 102 | (deftest test-pull-component-attr |
102 | 103 | (let [parts {:name "Part A", |
|
132 | 133 | test-schema)] |
133 | 134 |
|
134 | 135 | (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)))) |
136 | 137 |
|
137 | 138 | (testing "Reverse component references yield a single result" |
138 | 139 | (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))) |
140 | 141 |
|
141 | 142 | (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)))) |
143 | 144 |
|
144 | 145 | (testing "Like explicit recursion, expansion will not allow loops" |
145 | 146 | (is (= rpart (d/pull recdb '[:name :part] 10)))) |
146 | 147 |
|
147 | 148 | (testing "Reverse recursive component issue-411" |
148 | 149 | (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))) |
150 | 151 | (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)))))) |
152 | 153 |
|
153 | 154 | (deftest test-pull-wildcard |
154 | 155 | (is (= {:db/id 1 |
155 | 156 | :name "Petr" |
156 | 157 | :aka ["Devil" "Tupen"] |
157 | 158 | :child [{:db/id 2} {:db/id 3}]} |
158 | | - (d/pull test-db '[*] 1))) |
| 159 | + (d/pull @*test-db '[*] 1))) |
159 | 160 |
|
160 | 161 | (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))) |
162 | 163 |
|
163 | 164 | (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))) |
165 | 166 |
|
166 | 167 | (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))) |
168 | 169 |
|
169 | 170 | (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))) |
171 | 172 |
|
172 | 173 | (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))) |
174 | 175 |
|
175 | 176 | (is (= {:db/id 1 |
176 | 177 | :name "Petr" |
|
181 | 182 | {:db/id 3 |
182 | 183 | :father {:db/id 1} |
183 | 184 | :name "Thomas"}]} |
184 | | - (d/pull test-db '[* {:child ...}] 1)))) |
| 185 | + (d/pull @*test-db '[* {:child ...}] 1)))) |
185 | 186 |
|
186 | 187 | (deftest test-pull-limit |
187 | 188 | (let [db (d/init-db |
|
215 | 216 |
|
216 | 217 | (deftest test-pull-default |
217 | 218 | (testing "Empty results return nil" |
218 | | - (is (nil? (d/pull test-db '[:foo] 1)))) |
| 219 | + (is (nil? (d/pull @*test-db '[:foo] 1)))) |
219 | 220 |
|
220 | 221 | (testing "A default can be used to replace nil results" |
221 | 222 | (is (= {:foo "bar"} |
222 | | - (d/pull test-db '[(default :foo "bar")] 1))) |
| 223 | + (d/pull @*test-db '[(default :foo "bar")] 1))) |
223 | 224 | (is (= {:foo "bar"} |
224 | | - (d/pull test-db '[[:foo :default "bar"]] 1))) |
| 225 | + (d/pull @*test-db '[[:foo :default "bar"]] 1))) |
225 | 226 | (is (= {:foo false} |
226 | | - (d/pull test-db '[[:foo :default false]] 1))) |
| 227 | + (d/pull @*test-db '[[:foo :default false]] 1))) |
227 | 228 | (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)))) |
229 | 230 |
|
230 | 231 | (testing "default does not override results" |
231 | 232 | (is (= {:name "Petr", :aka ["Devil" "Tupen"] |
232 | 233 | :child [{:name "David", :aka "[aka]", :child "[child]"} |
233 | 234 | {:name "Thomas", :aka "[aka]", :child "[child]"}]} |
234 | | - (d/pull test-db |
| 235 | + (d/pull @*test-db |
235 | 236 | '[[:name :default "[name]"] |
236 | 237 | [:aka :default "[aka]"] |
237 | 238 | {[:child :default "[child]"] ...}] |
238 | 239 | 1))) |
239 | 240 | (is (= {:name "David", :aka "[aka]", :child "[child]"} |
240 | | - (d/pull test-db |
| 241 | + (d/pull @*test-db |
241 | 242 | '[[:name :default "[name]"] |
242 | 243 | [:aka :default "[aka]"] |
243 | 244 | {[:child :default "[child]"] ...}] |
244 | 245 | 2)))) |
245 | 246 |
|
246 | 247 | (testing "Ref default" |
247 | 248 | (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))) |
249 | 250 | (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))))) |
251 | 252 |
|
252 | 253 | (deftest test-pull-as |
253 | 254 | (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)))) |
255 | 256 |
|
256 | 257 | (deftest test-pull-attr-with-opts |
257 | 258 | (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)))) |
259 | 260 |
|
260 | 261 | (deftest test-pull-map |
261 | 262 | (testing "Single attrs yield a map" |
262 | 263 | (is (= {:name "Matthew" :father {:name "Thomas"}} |
263 | | - (d/pull test-db '[:name {:father [:name]}] 6)))) |
| 264 | + (d/pull @*test-db '[:name {:father [:name]}] 6)))) |
264 | 265 |
|
265 | 266 | (testing "Multi attrs yield a collection of maps" |
266 | 267 | (is (= {:name "Petr" :child [{:name "David"} |
267 | 268 | {:name "Thomas"}]} |
268 | | - (d/pull test-db '[:name {:child [:name]}] 1)))) |
| 269 | + (d/pull @*test-db '[:name {:child [:name]}] 1)))) |
269 | 270 |
|
270 | 271 | (testing "Missing attrs are dropped" |
271 | 272 | (is (= {:name "Petr"} |
272 | | - (d/pull test-db '[:name {:father [:name]}] 1)))) |
| 273 | + (d/pull @*test-db '[:name {:father [:name]}] 1)))) |
273 | 274 |
|
274 | 275 | (testing "Non matching results are removed from collections" |
275 | 276 | (is (= {:name "Petr"} |
276 | | - (d/pull test-db '[:name {:child [:foo]}] 1)))) |
| 277 | + (d/pull @*test-db '[:name {:child [:foo]}] 1)))) |
277 | 278 |
|
278 | 279 | (testing "Map specs can override component expansion" |
279 | 280 | (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))) |
281 | 282 |
|
282 | 283 | (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))))) |
284 | 285 |
|
285 | 286 | (deftest test-pull-recursion |
286 | | - (let [db (-> test-db |
| 287 | + (let [db (-> @*test-db |
287 | 288 | (d/db-with [[:db/add 4 :friend 5] |
288 | 289 | [:db/add 5 :friend 6] |
289 | 290 | [:db/add 6 :friend 7] |
|
448 | 449 |
|
449 | 450 | (deftest test-lookup-ref-pull |
450 | 451 | (is (= {:name "Petr" :aka ["Devil" "Tupen"]} |
451 | | - (d/pull test-db '[:name :aka] [:name "Petr"]))) |
| 452 | + (d/pull @*test-db '[:name :aka] [:name "Petr"]))) |
452 | 453 | (is (= nil |
453 | | - (d/pull test-db '[:name :aka] [:name "NotInDatabase"]))) |
| 454 | + (d/pull @*test-db '[:name :aka] [:name "NotInDatabase"]))) |
454 | 455 | (is (= [nil {:aka ["Devil" "Tupen"]} nil nil nil] |
455 | | - (d/pull-many test-db |
| 456 | + (d/pull-many @*test-db |
456 | 457 | '[:aka] |
457 | 458 | [[:name "Elizabeth"] |
458 | 459 | [:name "Petr"] |
459 | 460 | [:name "Eunan"] |
460 | 461 | [:name "Rebecca"] |
461 | 462 | [:name "Unknown"]]))) |
462 | | - (is (nil? (d/pull test-db '[*] [:name "No such name"])))) |
| 463 | + (is (nil? (d/pull @*test-db '[*] [:name "No such name"])))) |
463 | 464 |
|
464 | 465 | (deftest test-xform |
465 | 466 | (is (= {:db/id [1] |
466 | 467 | :name ["Petr"] |
467 | 468 | :aka [["Devil" "Tupen"]] |
468 | 469 | :child [[{:db/id [2], :name ["David"], :aka [nil], :child [nil]} |
469 | 470 | {:db/id [3], :name ["Thomas"], :aka [nil], :child [nil]}]]} |
470 | | - (d/pull test-db |
| 471 | + (d/pull @*test-db |
471 | 472 | [[:db/id :xform vector] |
472 | 473 | [:name :xform vector] |
473 | 474 | [:aka :xform vector] |
|
476 | 477 |
|
477 | 478 | (testing ":xform on cardinality/one ref issue-455" |
478 | 479 | (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)))) |
480 | 481 |
|
481 | 482 | (testing ":xform on reverse ref" |
482 | 483 | (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)))) |
484 | 485 |
|
485 | 486 | (testing ":xform on reverse component ref" |
486 | 487 | (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)))) |
488 | 489 |
|
489 | 490 | (testing "missing attrs are processed by xform" |
490 | 491 | (is (= {:normal [nil] |
491 | 492 | :aka [nil] |
492 | 493 | :child [nil]} |
493 | | - (d/pull test-db |
| 494 | + (d/pull @*test-db |
494 | 495 | '[[:normal :xform vector] |
495 | 496 | [:aka :xform vector] |
496 | 497 | {[:child :xform vector] ...}] |
497 | 498 | 2)))) |
498 | 499 | (testing "default takes precedence" |
499 | 500 | (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))))) |
501 | 502 |
|
502 | 503 | (deftest test-visitor |
503 | 504 | (let [*trace (volatile! nil) |
504 | 505 | opts {:visitor (fn [k e a v] (vswap! *trace conj [k e a v]))} |
505 | 506 | test-fn (fn [pattern id] |
506 | 507 | (vreset! *trace []) |
507 | | - (d/pull test-db pattern id opts) |
| 508 | + (d/pull @*test-db pattern id opts) |
508 | 509 | @*trace)] |
509 | 510 | (is (= [[:db.pull/attr 1 :name nil]] |
510 | 511 | (test-fn [:name] 1))) |
|
563 | 564 | (test-fn [:name :_child] 2)))))) |
564 | 565 |
|
565 | 566 | (deftest test-pull-other-dbs |
566 | | - (let [db (-> test-db |
| 567 | + (let [db (-> @*test-db |
567 | 568 | (d/filter (fn [_ datom] (not= "Tupen" (:v datom)))))] |
568 | 569 | (is (= {:name "Petr" :aka ["Devil"]} |
569 | 570 | (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)] |
571 | 572 | (is (= {:name "Petr" :aka ["Devil" "Tupen"]} |
572 | 573 | (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)] |
574 | 575 | (is (= {:name "Petr" :aka ["Devil" "Tupen"]} |
575 | 576 | (d/pull db '[:name :aka] 1))))) |
0 commit comments