From 43046e193740151b11446a36af651da8885f033c Mon Sep 17 00:00:00 2001 From: Nikita Prokopov Date: Wed, 19 Jun 2024 16:27:36 +0200 Subject: [PATCH] Wrap test databases into delay --- test/datascript/test/ident.cljc | 21 ++-- test/datascript/test/pull_api.cljc | 113 +++++++++++---------- test/datascript/test/pull_parser.cljc | 35 +++---- test/datascript/test/query_find_specs.cljc | 35 +++---- test/datascript/test/query_not.cljc | 10 +- test/datascript/test/query_or.cljc | 16 +-- test/datascript/test/query_pull.cljc | 27 ++--- test/datascript/test/query_return_map.cljc | 25 ++--- test/datascript/test/query_v3.cljc | 1 - test/datascript/test/serialize.cljc | 5 +- 10 files changed, 146 insertions(+), 142 deletions(-) diff --git a/test/datascript/test/ident.cljc b/test/datascript/test/ident.cljc index ef49cc8a..a5d84d04 100644 --- a/test/datascript/test/ident.cljc +++ b/test/datascript/test/ident.cljc @@ -3,26 +3,27 @@ [clojure.test :as t :refer [is are deftest testing]] [datascript.core :as d])) -(def db - (-> (d/empty-db {:ref {:db/valueType :db.type/ref}}) - (d/db-with [[:db/add 1 :db/ident :ent1] - [:db/add 2 :db/ident :ent2] - [:db/add 2 :ref 1]]))) +(def *db + (delay + (-> (d/empty-db {:ref {:db/valueType :db.type/ref}}) + (d/db-with [[:db/add 1 :db/ident :ent1] + [:db/add 2 :db/ident :ent2] + [:db/add 2 :ref 1]])))) (deftest test-q (is (= 1 (d/q '[:find ?v . - :where [:ent2 :ref ?v]] db))) + :where [:ent2 :ref ?v]] @*db))) (is (= 2 (d/q '[:find ?f . - :where [?f :ref :ent1]] db)))) + :where [?f :ref :ent1]] @*db)))) (deftest test-transact! - (let [db' (d/db-with db [[:db/add :ent1 :ref :ent2]])] + (let [db' (d/db-with @*db [[:db/add :ent1 :ref :ent2]])] (is (= 2 (-> (d/entity db' :ent1) :ref :db/id))))) (deftest test-entity (is (= {:db/ident :ent1} - (into {} (d/entity db :ent1))))) + (into {} (d/entity @*db :ent1))))) (deftest test-pull (is (= {:db/id 1, :db/ident :ent1} - (d/pull db '[*] :ent1)))) + (d/pull @*db '[*] :ent1)))) diff --git a/test/datascript/test/pull_api.cljc b/test/datascript/test/pull_api.cljc index e2db2c92..cac581d4 100644 --- a/test/datascript/test/pull_api.cljc +++ b/test/datascript/test/pull_api.cljc @@ -60,43 +60,44 @@ [16 :part 18]] (map #(apply d/datom %)))) -(def ^:private test-db - (d/init-db test-datoms test-schema)) +(def ^:private *test-db + (delay + (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))) + (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))) + (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])))) + (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))) + (d/pull @*test-db '[:name :_child] 2))) (is (= {:name "David" :_child [{:name "Petr"}]} - (d/pull test-db '[:name {:_child [:name]}] 2))) + (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))) + (d/pull @*test-db '[:name :_father] 3))) (is (= {:name "Petr" :_father [{:db/id 2} {:db/id 3}]} - (d/pull test-db '[:name :_father] 1))) + (d/pull @*test-db '[:name :_father] 1))) (is (= {:name "Thomas" :_father [{:name "Matthew"}]} - (d/pull test-db '[:name {:_father [:name]}] 3))) + (d/pull @*test-db '[:name {:_father [:name]}] 3))) (is (= {:name "Petr" :_father [{:name "David"} {:name "Thomas"}]} - (d/pull test-db '[:name {:_father [:name]}] 1)))) + (d/pull @*test-db '[:name {:_father [:name]}] 1)))) (testing "Multiple reverse refs issue-412" (is (= {:name "Petr" :_father [{:db/id 2} {:db/id 3}]} - (d/pull test-db '[:name :_father :_child] 1))))) + (d/pull @*test-db '[:name :_father :_child] 1))))) (deftest test-pull-component-attr (let [parts {:name "Part A", @@ -132,45 +133,45 @@ test-schema)] (testing "Component entities are expanded recursively" - (is (= parts (d/pull test-db '[:name :part] 10)))) + (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))) + (d/pull @*test-db [:name :_part] 11))) (is (= {:name "Part A.A" :_part {:name "Part A"}} - (d/pull test-db [:name {:_part [:name]}] 11)))) + (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)))) (testing "Reverse recursive component issue-411" (is (= {:name "Part A.A.A.B" :_part {:name "Part A.A.A" :_part {:name "Part A.A" :_part {:name "Part A"}}}} - (d/pull test-db '[:name {:_part ...}] 14))) + (d/pull @*test-db '[:name {:_part ...}] 14))) (is (= {:name "Part A.A.A.B" :_part {:name "Part A.A.A" :_part {:name "Part A.A"}}} - (d/pull test-db '[:name {:_part 2}] 14)))))) + (d/pull @*test-db '[:name {:_part 2}] 14)))))) (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))) + (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))) + (d/pull @*test-db '[* :_child] 2))) (is (= {:aka ["Devil" "Tupen"], :child [{:db/id 2} {:db/id 3}], :name "Petr", :db/id 1} - (d/pull test-db '[:name *] 1))) + (d/pull @*test-db '[:name *] 1))) (is (= {:aka ["Devil" "Tupen"], :child [{:db/id 2} {:db/id 3}], :name "Petr", :db/id 1} - (d/pull test-db '[:aka :name *] 1))) + (d/pull @*test-db '[:aka :name *] 1))) (is (= {:aka ["Devil" "Tupen"], :child [{:db/id 2} {:db/id 3}], :name "Petr", :db/id 1} - (d/pull test-db '[:aka :child :name *] 1))) + (d/pull @*test-db '[:aka :child :name *] 1))) (is (= {:alias ["Devil" "Tupen"], :child [{:db/id 2} {:db/id 3}], :first-name "Petr", :db/id 1} - (d/pull test-db '[[:aka :as :alias] [:name :as :first-name] *] 1))) + (d/pull @*test-db '[[:aka :as :alias] [:name :as :first-name] *] 1))) (is (= {:db/id 1 :name "Petr" @@ -181,7 +182,7 @@ {:db/id 3 :father {:db/id 1} :name "Thomas"}]} - (d/pull test-db '[* {:child ...}] 1)))) + (d/pull @*test-db '[* {:child ...}] 1)))) (deftest test-pull-limit (let [db (d/init-db @@ -215,29 +216,29 @@ (deftest test-pull-default (testing "Empty results return nil" - (is (nil? (d/pull test-db '[:foo] 1)))) + (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))) + (d/pull @*test-db '[(default :foo "bar")] 1))) (is (= {:foo "bar"} - (d/pull test-db '[[:foo :default "bar"]] 1))) + (d/pull @*test-db '[[:foo :default "bar"]] 1))) (is (= {:foo false} - (d/pull test-db '[[:foo :default false]] 1))) + (d/pull @*test-db '[[:foo :default false]] 1))) (is (= {:bar false} - (d/pull test-db '[[:foo :as :bar :default false]] 1)))) + (d/pull @*test-db '[[:foo :as :bar :default false]] 1)))) (testing "default does not override results" (is (= {:name "Petr", :aka ["Devil" "Tupen"] :child [{:name "David", :aka "[aka]", :child "[child]"} {:name "Thomas", :aka "[aka]", :child "[child]"}]} - (d/pull test-db + (d/pull @*test-db '[[:name :default "[name]"] [:aka :default "[aka]"] {[:child :default "[child]"] ...}] 1))) (is (= {:name "David", :aka "[aka]", :child "[child]"} - (d/pull test-db + (d/pull @*test-db '[[:name :default "[name]"] [:aka :default "[aka]"] {[:child :default "[child]"] ...}] @@ -245,45 +246,45 @@ (testing "Ref default" (is (= {:child 1 :db/id 2} - (d/pull test-db '[:db/id [:child :default 1]] 2))) + (d/pull @*test-db '[:db/id [:child :default 1]] 2))) (is (= {:_child 2 :db/id 1} - (d/pull test-db '[:db/id [:_child :default 2]] 1))))) + (d/pull @*test-db '[:db/id [:_child :default 2]] 1))))) (deftest test-pull-as (is (= {"Name" "Petr", :alias ["Devil" "Tupen"]} - (d/pull test-db '[[:name :as "Name"] [:aka :as :alias]] 1)))) + (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)))) + (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)))) + (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)))) + (d/pull @*test-db '[:name {:child [:name]}] 1)))) (testing "Missing attrs are dropped" (is (= {:name "Petr"} - (d/pull test-db '[:name {:father [:name]}] 1)))) + (d/pull @*test-db '[:name {:father [:name]}] 1)))) (testing "Non matching results are removed from collections" (is (= {:name "Petr"} - (d/pull test-db '[:name {:child [:foo]}] 1)))) + (d/pull @*test-db '[:name {:child [:foo]}] 1)))) (testing "Map specs can override component expansion" (is (= {:name "Part A" :part [{:name "Part A.A"} {:name "Part A.B"}]} - (d/pull test-db '[:name {:part [:name]}] 10))) + (d/pull @*test-db '[:name {:part [:name]}] 10))) (is (= {:name "Part A" :part [{:name "Part A.A"} {:name "Part A.B"}]} - (d/pull test-db '[:name {:part 1}] 10))))) + (d/pull @*test-db '[:name {:part 1}] 10))))) (deftest test-pull-recursion - (let [db (-> test-db + (let [db (-> @*test-db (d/db-with [[:db/add 4 :friend 5] [:db/add 5 :friend 6] [:db/add 6 :friend 7] @@ -448,18 +449,18 @@ (deftest test-lookup-ref-pull (is (= {:name "Petr" :aka ["Devil" "Tupen"]} - (d/pull test-db '[:name :aka] [:name "Petr"]))) + (d/pull @*test-db '[:name :aka] [:name "Petr"]))) (is (= nil - (d/pull test-db '[:name :aka] [:name "NotInDatabase"]))) + (d/pull @*test-db '[:name :aka] [:name "NotInDatabase"]))) (is (= [nil {:aka ["Devil" "Tupen"]} nil nil nil] - (d/pull-many test-db + (d/pull-many @*test-db '[:aka] [[:name "Elizabeth"] [:name "Petr"] [:name "Eunan"] [:name "Rebecca"] [:name "Unknown"]]))) - (is (nil? (d/pull test-db '[*] [:name "No such name"])))) + (is (nil? (d/pull @*test-db '[*] [:name "No such name"])))) (deftest test-xform (is (= {:db/id [1] @@ -467,7 +468,7 @@ :aka [["Devil" "Tupen"]] :child [[{:db/id [2], :name ["David"], :aka [nil], :child [nil]} {:db/id [3], :name ["Thomas"], :aka [nil], :child [nil]}]]} - (d/pull test-db + (d/pull @*test-db [[:db/id :xform vector] [:name :xform vector] [:aka :xform vector] @@ -476,35 +477,35 @@ (testing ":xform on cardinality/one ref issue-455" (is (= {:name "David" :father "Petr"} - (d/pull test-db [:name {[:father :xform #(:name %)] ['*]}] 2)))) + (d/pull @*test-db [:name {[:father :xform #(:name %)] ['*]}] 2)))) (testing ":xform on reverse ref" (is (= {:name "Petr" :_father ["David" "Thomas"]} - (d/pull test-db [:name {[:_father :xform #(mapv :name %)] [:name]}] 1)))) + (d/pull @*test-db [:name {[:_father :xform #(mapv :name %)] [:name]}] 1)))) (testing ":xform on reverse component ref" (is (= {:name "Part A.A" :_part "Part A"} - (d/pull test-db [:name {[:_part :xform #(:name %)] [:name]}] 11)))) + (d/pull @*test-db [:name {[:_part :xform #(:name %)] [:name]}] 11)))) (testing "missing attrs are processed by xform" (is (= {:normal [nil] :aka [nil] :child [nil]} - (d/pull test-db + (d/pull @*test-db '[[:normal :xform vector] [:aka :xform vector] {[:child :xform vector] ...}] 2)))) (testing "default takes precedence" (is (= {:unknown "[unknown]"} - (d/pull test-db '[[:unknown :default "[unknown]" :xform vector]] 1))))) + (d/pull @*test-db '[[:unknown :default "[unknown]" :xform vector]] 1))))) (deftest test-visitor (let [*trace (volatile! nil) opts {:visitor (fn [k e a v] (vswap! *trace conj [k e a v]))} test-fn (fn [pattern id] (vreset! *trace []) - (d/pull test-db pattern id opts) + (d/pull @*test-db pattern id opts) @*trace)] (is (= [[:db.pull/attr 1 :name nil]] (test-fn [:name] 1))) @@ -563,13 +564,13 @@ (test-fn [:name :_child] 2)))))) (deftest test-pull-other-dbs - (let [db (-> test-db + (let [db (-> @*test-db (d/filter (fn [_ datom] (not= "Tupen" (:v datom)))))] (is (= {:name "Petr" :aka ["Devil"]} (d/pull db '[:name :aka] 1)))) - (let [db (-> test-db d/serializable pr-str clojure.edn/read-string d/from-serializable)] + (let [db (-> @*test-db d/serializable pr-str clojure.edn/read-string d/from-serializable)] (is (= {:name "Petr" :aka ["Devil" "Tupen"]} (d/pull db '[:name :aka] 1)))) - (let [db (d/init-db (d/datoms test-db :eavt) test-schema)] + (let [db (d/init-db (d/datoms @*test-db :eavt) test-schema)] (is (= {:name "Petr" :aka ["Devil" "Tupen"]} (d/pull db '[:name :aka] 1))))) diff --git a/test/datascript/test/pull_parser.cljc b/test/datascript/test/pull_parser.cljc index 9a81b50a..c19b0ff7 100644 --- a/test/datascript/test/pull_parser.cljc +++ b/test/datascript/test/pull_parser.cljc @@ -6,20 +6,21 @@ [datascript.pull-parser :as dpp] [datascript.test.core :as tdc])) -(def db - (d/empty-db - {:ref {:db/valueType :db.type/ref} - :ref2 {:db/valueType :db.type/ref} - :ref3 {:db/valueType :db.type/ref} - :ns/ref {:db/valueType :db.type/ref} - :multival {:db/cardinality :db.cardinality/many} - :multiref {:db/valueType :db.type/ref - :db/cardinality :db.cardinality/many} - :component {:db/valueType :db.type/ref - :db/isComponent true} - :multicomponent {:db/valueType :db.type/ref - :db/isComponent true - :db/cardinality :db.cardinality/many}})) +(def *db + (delay + (d/empty-db + {:ref {:db/valueType :db.type/ref} + :ref2 {:db/valueType :db.type/ref} + :ref3 {:db/valueType :db.type/ref} + :ns/ref {:db/valueType :db.type/ref} + :multival {:db/cardinality :db.cardinality/many} + :multiref {:db/valueType :db.type/ref + :db/cardinality :db.cardinality/many} + :component {:db/valueType :db.type/ref + :db/isComponent true} + :multicomponent {:db/valueType :db.type/ref + :db/isComponent true + :db/cardinality :db.cardinality/many}}))) (defn pattern [& {:as args}] (let [attrs (filter #(not= :db/id (:name %)) (:attrs args))] @@ -29,11 +30,11 @@ (dpp/map->PullAttr (merge {:name name :xform identity :as name} - (when (db/ref? db name) {:pattern dpp/default-pattern-ref}) + (when (db/ref? @*db name) {:pattern dpp/default-pattern-ref}) args))) (deftest test-parse-pattern - (are [pattern expected] (= expected (dpp/parse-pattern db pattern)) + (are [pattern expected] (= expected (dpp/parse-pattern @*db pattern)) [:normal] (pattern :attrs [(attr :normal)]) ['(:normal)] (pattern :attrs [(attr :normal)]) [[:normal]] (pattern :attrs [(attr :normal)]) @@ -131,7 +132,7 @@ [{:_ref '...}] (pattern :reverse-attrs [(attr :ref, :as :_ref, :ref? true, :reverse? true, :pattern nil, :recursive? true, :recursion-limit nil)])) (testing "Error reporting" - (are [pattern msg] (thrown-msg? msg (dpp/parse-pattern db pattern)) + (are [pattern msg] (thrown-msg? msg (dpp/parse-pattern @*db pattern)) ; refs [:_normal] "Expected reverse attribute having :db.type/ref, got: :_normal" diff --git a/test/datascript/test/query_find_specs.cljc b/test/datascript/test/query_find_specs.cljc index e10e29dc..de064368 100644 --- a/test/datascript/test/query_find_specs.cljc +++ b/test/datascript/test/query_find_specs.cljc @@ -5,26 +5,27 @@ [datascript.db :as db] [datascript.test.core :as tdc])) -(def test-db - (d/db-with - (d/empty-db) - [[:db/add 1 :name "Petr"] - [:db/add 1 :age 44] - [:db/add 2 :name "Ivan"] - [:db/add 2 :age 25] - [:db/add 3 :name "Sergey"] - [:db/add 3 :age 11]])) +(def *test-db + (delay + (d/db-with + (d/empty-db) + [[:db/add 1 :name "Petr"] + [:db/add 1 :age 44] + [:db/add 2 :name "Ivan"] + [:db/add 2 :age 25] + [:db/add 3 :name "Sergey"] + [:db/add 3 :age 11]]))) (deftest test-find-specs (is (= (set (d/q '[:find [?name ...] - :where [_ :name ?name]] test-db)) + :where [_ :name ?name]] @*test-db)) #{"Ivan" "Petr" "Sergey"})) (is (= (d/q '[:find [?name ?age] :where [1 :name ?name] - [1 :age ?age]] test-db) + [1 :age ?age]] @*test-db) ["Petr" 44])) (is (= (d/q '[:find ?name . - :where [1 :name ?name]] test-db) + :where [1 :name ?name]] @*test-db) "Petr")) (testing "Multiple results get cut" @@ -32,19 +33,19 @@ #{["Petr" 44] ["Ivan" 25] ["Sergey" 11]} (d/q '[:find [?name ?age] :where [?e :name ?name] - [?e :age ?age]] test-db))) + [?e :age ?age]] @*test-db))) (is (contains? #{"Ivan" "Petr" "Sergey"} (d/q '[:find ?name . - :where [_ :name ?name]] test-db)))) + :where [_ :name ?name]] @*test-db)))) (testing "Aggregates work with find specs" (is (= (d/q '[:find [(count ?name) ...] - :where [_ :name ?name]] test-db) + :where [_ :name ?name]] @*test-db) [3])) (is (= (d/q '[:find [(count ?name)] - :where [_ :name ?name]] test-db) + :where [_ :name ?name]] @*test-db) [3])) (is (= (d/q '[:find (count ?name) . - :where [_ :name ?name]] test-db) + :where [_ :name ?name]] @*test-db) 3)))) diff --git a/test/datascript/test/query_not.cljc b/test/datascript/test/query_not.cljc index 7ca1f08f..2ed219ff 100644 --- a/test/datascript/test/query_not.cljc +++ b/test/datascript/test/query_not.cljc @@ -8,7 +8,7 @@ (:import [clojure.lang ExceptionInfo]))) -(def test-db +(def *test-db (delay (d/db-with (d/empty-db) [{:db/id 1 :name "Ivan" :age 10} @@ -19,7 +19,7 @@ {:db/id 6 :name "Ivan" :age 20}]))) (deftest test-not - (are [q res] (= (set (d/q (concat '[:find [?e ...] :where] (quote q)) @test-db)) + (are [q res] (= (set (d/q (concat '[:find [?e ...] :where] (quote q)) @*test-db)) res) [[?e :name] (not [?e :name "Ivan"])] @@ -65,7 +65,7 @@ #{2 4 6})) (deftest test-not-join - (are [q res] (= (d/q (concat '[:find ?e ?a :where] (quote q)) @test-db) + (are [q res] (= (d/q (concat '[:find ?e ?a :where] (quote q)) @*test-db) res) [[?e :name] [?e :age ?a] @@ -126,7 +126,7 @@ #{1}))) (deftest test-impl-edge-cases - (are [q res] (= (d/q (quote q) @test-db) + (are [q res] (= (d/q (quote q) @*test-db) res) ;; const \ empty [:find ?e @@ -174,7 +174,7 @@ (deftest test-insufficient-bindings (are [q msg] (thrown-msg? msg - (d/q (concat '[:find ?e :where] (quote q)) @test-db)) + (d/q (concat '[:find ?e :where] (quote q)) @*test-db)) [(not [?e :name "Ivan"]) [?e :name]] "Insufficient bindings: none of #{?e} is bound in (not [?e :name \"Ivan\"])" diff --git a/test/datascript/test/query_or.cljc b/test/datascript/test/query_or.cljc index fe05396d..38e51da3 100644 --- a/test/datascript/test/query_or.cljc +++ b/test/datascript/test/query_or.cljc @@ -8,7 +8,7 @@ (:import [clojure.lang ExceptionInfo]))) -(def test-db +(def *test-db (delay (d/db-with (d/empty-db) [{:db/id 1 :name "Ivan" :age 10} @@ -19,7 +19,7 @@ {:db/id 6 :name "Ivan" :age 20}]))) (deftest test-or - (are [q res] (= (d/q (concat '[:find ?e :where] (quote q)) @test-db) + (are [q res] (= (d/q (concat '[:find ?e :where] (quote q)) @*test-db) (into #{} (map vector) res)) ;; intersecting results @@ -77,7 +77,7 @@ #{2 6})) (deftest test-or-join - (are [q res] (= (d/q (concat '[:find ?e :where] (quote q)) @test-db) + (are [q res] (= (d/q (concat '[:find ?e :where] (quote q)) @*test-db) (into #{} (map vector) res)) [(or-join [?e] [?e :name ?n] @@ -108,7 +108,7 @@ :where (or [?e :age ?a] [?e :name "Oleg"])] - @test-db 10))) + @*test-db 10))) ;; issue-348 (is (= #{[1] [3] [4] [5]} @@ -117,7 +117,7 @@ :where (or-join [?e ?a] [?e :age ?a] [?e :name "Oleg"])] - @test-db 10))) + @*test-db 10))) ;; issue-348 (is (= #{[1] [3] [4] [5]} @@ -126,7 +126,7 @@ :where (or-join [[?a] ?e] [?e :age ?a] [?e :name "Oleg"])] - @test-db 10))) + @*test-db 10))) (is (= #{[:a1 :b1 :c1] [:a2 :b2 :c2]} @@ -224,10 +224,10 @@ (d/q '[:find ?e :where (or [?e :name _] [?e :age ?a])] - @test-db))) + @*test-db))) (is (thrown-msg? "Insufficient bindings: #{?e} not bound in (or-join [[?e]] [?e :name \"Ivan\"])" (d/q '[:find ?e :where (or-join [[?e]] [?e :name "Ivan"])] - @test-db)))) + @*test-db)))) diff --git a/test/datascript/test/query_pull.cljc b/test/datascript/test/query_pull.cljc index 6020d715..7518221a 100644 --- a/test/datascript/test/query_pull.cljc +++ b/test/datascript/test/query_pull.cljc @@ -5,17 +5,18 @@ [datascript.db :as db] [datascript.test.core :as tdc])) -(def test-db - (d/db-with (d/empty-db) - [{:db/id 1 :name "Petr" :age 44} - {:db/id 2 :name "Ivan" :age 25} - {:db/id 3 :name "Oleg" :age 11}])) +(def *test-db + (delay + (d/db-with (d/empty-db) + [{:db/id 1 :name "Petr" :age 44} + {:db/id 2 :name "Ivan" :age 25} + {:db/id 3 :name "Oleg" :age 11}]))) (deftest test-basics (are [find res] (= (set (d/q {:find find :where '[[?e :age ?a] [(>= ?a 18)]]} - test-db)) + @*test-db)) res) '[(pull ?e [:name])] #{[{:name "Ivan"}] [{:name "Petr"}]} @@ -37,7 +38,7 @@ :in '[$ ?pattern] :where '[[?e :age ?a] [(>= ?a 18)]]} - test-db pattern)) + @*test-db pattern)) res) '[(pull ?e ?pattern)] [:name] #{[{:name "Ivan"}] [{:name "Petr"}]} @@ -51,7 +52,7 @@ :in $ [?p ...] :where [?e :age ?a] [>= ?a 18]] - test-db [[:name] [:age]])) + @*test-db [[:name] [:age]])) #{[2 [:name] {:name "Ivan"}] [2 [:age] {:age 25}] [1 [:name] {:name "Petr"}] @@ -82,29 +83,29 @@ (deftest test-find-spec (is (= (d/q '[:find (pull ?e [:name]) . :where [?e :age 25]] - test-db) + @*test-db) {:name "Ivan"})) (is (= (set (d/q '[:find [(pull ?e [:name]) ...] :where [?e :age ?a]] - test-db)) + @*test-db)) #{{:name "Ivan"} {:name "Petr"} {:name "Oleg"}})) (is (= (d/q '[:find [?e (pull ?e [:name])] :where [?e :age 25]] - test-db) + @*test-db) [2 {:name "Ivan"}]))) (deftest test-find-spec-input (is (= (d/q '[:find (pull ?e ?p) . :in $ ?p :where [(ground 2) ?e]] - test-db [:name]) + @*test-db [:name]) {:name "Ivan"})) (is (= (d/q '[:find (pull ?e p) . :in $ p :where [(ground 2) ?e]] - test-db [:name]) + @*test-db [:name]) {:name "Ivan"}))) (deftest test-aggregates diff --git a/test/datascript/test/query_return_map.cljc b/test/datascript/test/query_return_map.cljc index 38cb9228..90ffa248 100644 --- a/test/datascript/test/query_return_map.cljc +++ b/test/datascript/test/query_return_map.cljc @@ -5,33 +5,34 @@ [datascript.db :as db] [datascript.test.core :as tdc])) -(def test-db - (d/db-with (d/empty-db) - [[:db/add 1 :name "Petr"] - [:db/add 1 :age 44] - [:db/add 2 :name "Ivan"] - [:db/add 2 :age 25] - [:db/add 3 :name "Sergey"] - [:db/add 3 :age 11]])) +(def *test-db + (delay + (d/db-with (d/empty-db) + [[:db/add 1 :name "Petr"] + [:db/add 1 :age 44] + [:db/add 2 :name "Ivan"] + [:db/add 2 :age 25] + [:db/add 3 :name "Sergey"] + [:db/add 3 :age 11]]))) (deftest test-find-specs (is (= (d/q '[:find ?name ?age :keys n a :where [?e :name ?name] [?e :age ?age]] - test-db) + @*test-db) #{{:n "Petr" :a 44} {:n "Ivan" :a 25} {:n "Sergey" :a 11}})) (is (= (d/q '[:find ?name ?age :syms n a :where [?e :name ?name] [?e :age ?age]] - test-db) + @*test-db) #{{'n "Petr" 'a 44} {'n "Ivan" 'a 25} {'n "Sergey" 'a 11}})) (is (= (d/q '[:find ?name ?age :strs n a :where [?e :name ?name] [?e :age ?age]] - test-db) + @*test-db) #{{"n" "Petr" "a" 44} {"n" "Ivan" "a" 25} {"n" "Sergey" "a" 11}})) (is (= (d/q '[:find [?name ?age] @@ -39,7 +40,7 @@ :where [?e :name ?name] [(= ?name "Ivan")] [?e :age ?age]] - test-db) + @*test-db) {:n "Ivan" :a 25}))) diff --git a/test/datascript/test/query_v3.cljc b/test/datascript/test/query_v3.cljc index 187b98e4..aca96359 100644 --- a/test/datascript/test/query_v3.cljc +++ b/test/datascript/test/query_v3.cljc @@ -9,7 +9,6 @@ (:import [clojure.lang ExceptionInfo]))) - (deftest test-validation (are [q ins msg] (thrown-with-msg? ExceptionInfo msg (apply dq/q q ins)) '[:find ?a :in $ ?a] [0] #"Wrong number of arguments for bindings \[\$ \?a\], 2 required, 1 provided" diff --git a/test/datascript/test/serialize.cljc b/test/datascript/test/serialize.cljc index 5ac00ee8..8a278135 100644 --- a/test/datascript/test/serialize.cljc +++ b/test/datascript/test/serialize.cljc @@ -81,9 +81,8 @@ :follows {:db/valueType :db.type/ref} :email {:db/unique :db.unique/identity} :avatar {:db/valueType :db.type/ref, :db/isComponent true} - :url {} ;; just a component prop - :attach {} ;; should skip index - }) + :url {} ;; just a component prop + :attach {}}) ;; should skip index (deftest test-init-db (let [db-init (d/init-db