Skip to content

Commit

Permalink
Attributes with :db.cardinality/many should accept all collection v…
Browse files Browse the repository at this point in the history
…alues (tonsky#16)

Before this commit, they only accepted sequential values.
Now they accept everything that implements IPersistentCollection.
  • Loading branch information
lynaghk authored and tonsky committed Aug 26, 2014
1 parent 9293e5c commit c6c59ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/datascript/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
(defn- explode [db entity]
(let [eid (:db/id entity)]
(for [[a vs] (dissoc entity :db/id)
v (if (and (sequential? vs)
v (if (and (coll? vs)
(multival? db a))
vs [vs])]
[:db/add eid a v])))
Expand Down
38 changes: 21 additions & 17 deletions test/test/datascript.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -231,23 +231,27 @@


(deftest test-explode
(let [conn (d/create-conn { :aka { :db/cardinality :db.cardinality/many }
:also { :db/cardinality :db.cardinality/many} })]
(d/transact! conn [{:db/id -1
:name "Ivan"
:age 16
:aka ["Devil" "Tupen"]
:also "ok"}])
(is (= (d/q '[:find ?n ?a
:where [1 :name ?n]
[1 :age ?a]] @conn)
#{["Ivan" 16]}))
(is (= (d/q '[:find ?v
:where [1 :also ?v]] @conn)
#{["ok"]}))
(is (= (d/q '[:find ?v
:where [1 :aka ?v]] @conn)
#{["Devil"] ["Tupen"]}))))
;;Test that explode works properly with vectors, sets, and lists.
(doseq [coll [["Devil" "Tupen"]
#{"Devil" "Tupen"}
'("Devil" "Tupen")]]
(let [conn (d/create-conn { :aka { :db/cardinality :db.cardinality/many }
:also { :db/cardinality :db.cardinality/many} })]
(d/transact! conn [{:db/id -1
:name "Ivan"
:age 16
:aka coll
:also "ok"}])
(is (= (d/q '[:find ?n ?a
:where [1 :name ?n]
[1 :age ?a]] @conn)
#{["Ivan" 16]}))
(is (= (d/q '[:find ?v
:where [1 :also ?v]] @conn)
#{["ok"]}))
(is (= (d/q '[:find ?v
:where [1 :aka ?v]] @conn)
#{["Devil"] ["Tupen"]})))))


(deftest test-joins
Expand Down

0 comments on commit c6c59ad

Please sign in to comment.