forked from tonsky/datascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplode.cljc
135 lines (115 loc) · 5.48 KB
/
explode.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
(ns datascript.test.explode
(: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]))
#?(:cljs
(def Throwable js/Error))
(deftest test-explode
(doseq [coll [["Devil" "Tupen"]
#{"Devil" "Tupen"}
'("Devil" "Tupen")
(to-array ["Devil" "Tupen"])]]
(testing coll
(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-explode-ref
(let [db0 (d/empty-db { :children { :db/valueType :db.type/ref
:db/cardinality :db.cardinality/many } })]
(let [db (d/db-with db0 [{:db/id -1, :name "Ivan", :children [-2 -3]}
{:db/id -2, :name "Petr"}
{:db/id -3, :name "Evgeny"}])]
(is (= (d/q '[:find ?n
:where [_ :children ?e]
[?e :name ?n]] db)
#{["Petr"] ["Evgeny"]})))
(let [db (d/db-with db0 [{:db/id -1, :name "Ivan"}
{:db/id -2, :name "Petr", :_children -1}
{:db/id -3, :name "Evgeny", :_children -1}])]
(is (= (d/q '[:find ?n
:where [_ :children ?e]
[?e :name ?n]] db)
#{["Petr"] ["Evgeny"]})))
(is (thrown-msg? "Bad attribute :_parent: reverse attribute name requires {:db/valueType :db.type/ref} in schema"
(d/db-with db0 [{:name "Sergey" :_parent 1}])))))
(deftest test-explode-nested-maps
(let [schema { :profile { :db/valueType :db.type/ref }}
db (d/empty-db schema)]
(are [tx res] (= (d/q '[:find ?e ?a ?v
:where [?e ?a ?v]]
(d/db-with db tx)) res)
[ {:db/id 5 :name "Ivan" :profile {:db/id 7 :email "@2"}} ]
#{ [5 :name "Ivan"] [5 :profile 7] [7 :email "@2"] }
[ {:name "Ivan" :profile {:email "@2"}} ]
#{ [1 :name "Ivan"] [1 :profile 2] [2 :email "@2"] }
[ {:profile {:email "@2"}} ] ;; issue #59
#{ [1 :profile 2] [2 :email "@2"] }
[ {:email "@2" :_profile {:name "Ivan"}} ]
#{ [1 :email "@2"] [2 :name "Ivan"] [2 :profile 1] }
))
(testing "multi-valued"
(let [schema { :profile { :db/valueType :db.type/ref
:db/cardinality :db.cardinality/many }}
db (d/empty-db schema)]
(are [tx res] (= (d/q '[:find ?e ?a ?v
:where [?e ?a ?v]]
(d/db-with db tx)) res)
[ {:db/id 5 :name "Ivan" :profile {:db/id 7 :email "@2"}} ]
#{ [5 :name "Ivan"] [5 :profile 7] [7 :email "@2"] }
[ {:db/id 5 :name "Ivan" :profile [{:db/id 7 :email "@2"} {:db/id 8 :email "@3"}]} ]
#{ [5 :name "Ivan"] [5 :profile 7] [7 :email "@2"] [5 :profile 8] [8 :email "@3"] }
[ {:name "Ivan" :profile {:email "@2"}} ]
#{ [1 :name "Ivan"] [1 :profile 2] [2 :email "@2"] }
[ {:name "Ivan" :profile [{:email "@2"} {:email "@3"}]} ]
#{ [1 :name "Ivan"] [1 :profile 2] [2 :email "@2"] [1 :profile 3] [3 :email "@3"] }
[ {:email "@2" :_profile {:name "Ivan"}} ]
#{ [1 :email "@2"] [2 :name "Ivan"] [2 :profile 1] }
[ {:email "@2" :_profile [{:name "Ivan"} {:name "Petr"} ]} ]
#{ [1 :email "@2"] [2 :name "Ivan"] [2 :profile 1] [3 :name "Petr"] [3 :profile 1] }
))))
(deftest test-circular-refs
(let [schema {:comp {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many
:db/isComponent true}}
db (d/db-with (d/empty-db schema)
[{:db/id 1, :comp [{:name "C"}]}])]
(is (= (mapv (juxt :e :a :v) (d/datoms db :eavt))
[ [ 1 :comp 2 ]
[ 2 :name "C"] ])))
(let [schema {:comp {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}}
db (d/db-with (d/empty-db schema)
[{:db/id 1, :comp [{:name "C"}]}])]
(is (= (mapv (juxt :e :a :v) (d/datoms db :eavt))
[ [ 1 :comp 2 ]
[ 2 :name "C"] ])))
(let [schema {:comp {:db/valueType :db.type/ref
:db/isComponent true}}
db (d/db-with (d/empty-db schema)
[{:db/id 1, :comp {:name "C"}}])]
(is (= (mapv (juxt :e :a :v) (d/datoms db :eavt))
[ [ 1 :comp 2 ]
[ 2 :name "C"] ])))
(let [schema {:comp {:db/valueType :db.type/ref}}
db (d/db-with (d/empty-db schema)
[{:db/id 1, :comp {:name "C"}}])]
(is (= (mapv (juxt :e :a :v) (d/datoms db :eavt))
[ [ 1 :comp 2 ]
[ 2 :name "C"] ]))))