|
10 | 10 | (def Throwable js/Error)) |
11 | 11 |
|
12 | 12 | (deftest test-upsert |
13 | | - (let [db (d/db-with (d/empty-db {:name { :db/unique :db.unique/identity } |
14 | | - :email { :db/unique :db.unique/identity } |
15 | | - :slugs { :db/unique :db.unique/identity |
16 | | - :db/cardinality :db.cardinality/many }}) |
17 | | - [{:db/id 1 :name "Ivan" :email "@1"} |
18 | | - {:db/id 2 :name "Petr" :email "@2"}]) |
19 | | - touched (fn [tx e] (into {} (d/touch (d/entity (:db-after tx) e)))) |
20 | | - tempids (fn [tx] (dissoc (:tempids tx) :db/current-tx))] |
| 13 | + (let [ivan {:db/id 1 :name "Ivan" :email "@1"} |
| 14 | + petr {:db/id 2 :name "Petr" :email "@2" :ref 3} |
| 15 | + dima {:db/id 3 :name "Dima" :email "@3" :ref 4} |
| 16 | + olga {:db/id 4 :name "Olga" :email "@4" :ref 1} |
| 17 | + db (d/db-with (d/empty-db {:name {:db/unique :db.unique/identity} |
| 18 | + :email {:db/unique :db.unique/identity} |
| 19 | + :slugs {:db/unique :db.unique/identity |
| 20 | + :db/cardinality :db.cardinality/many} |
| 21 | + :ref {:db/unique :db.unique/identity |
| 22 | + :db/type :db.type/ref}}) |
| 23 | + [ivan petr dima olga]) |
| 24 | + pull (fn [tx e] |
| 25 | + (d/pull (:db-after tx) ['* {[:ref :xform #(:db/id %)] [:db/id]}] e)) |
| 26 | + tempids (fn [tx] |
| 27 | + (dissoc (:tempids tx) :db/current-tx))] |
21 | 28 | (testing "upsert, no tempid" |
22 | 29 | (let [tx (d/with db [{:name "Ivan" :age 35}])] |
23 | | - (is (= (touched tx 1) |
24 | | - {:name "Ivan" :email "@1" :age 35})) |
| 30 | + (is (= {:db/id 1 :name "Ivan" :email "@1" :age 35} |
| 31 | + (pull tx 1))) |
25 | 32 | (is (= (tempids tx) |
26 | | - {})))) |
| 33 | + {})))) |
27 | 34 |
|
28 | 35 | (testing "upsert by 2 attrs, no tempid" |
29 | 36 | (let [tx (d/with db [{:name "Ivan" :email "@1" :age 35}])] |
30 | | - (is (= (touched tx 1) |
31 | | - {:name "Ivan" :email "@1" :age 35})) |
| 37 | + (is (= {:db/id 1 :name "Ivan" :email "@1" :age 35} |
| 38 | + (pull tx 1))) |
32 | 39 | (is (= (tempids tx) |
33 | | - {})))) |
| 40 | + {})))) |
34 | 41 |
|
35 | 42 | (testing "upsert with tempid" |
36 | 43 | (let [tx (d/with db [{:db/id -1 :name "Ivan" :age 35}])] |
37 | | - (is (= (touched tx 1) |
38 | | - {:name "Ivan" :email "@1" :age 35})) |
| 44 | + (is (= {:db/id 1 :name "Ivan" :email "@1" :age 35} |
| 45 | + (pull tx 1))) |
39 | 46 | (is (= (tempids tx) |
40 | | - {-1 1})))) |
| 47 | + {-1 1})))) |
41 | 48 |
|
42 | 49 | (testing "upsert with string tempid" |
43 | 50 | (let [tx (d/with db [{:db/id "1" :name "Ivan" :age 35} |
44 | 51 | [:db/add "2" :name "Oleg"] |
45 | 52 | [:db/add "2" :email "@2"]])] |
46 | | - (is (= (touched tx 1) |
47 | | - {:name "Ivan" :email "@1" :age 35})) |
48 | | - (is (= (touched tx 2) |
49 | | - {:name "Oleg" :email "@2"})) |
| 53 | + (is (= {:db/id 1 :name "Ivan" :email "@1" :age 35} |
| 54 | + (pull tx 1))) |
| 55 | + (is (= {:db/id 2 :name "Oleg" :email "@2" :ref 3} |
| 56 | + (pull tx 2))) |
50 | 57 | (is (= (tempids tx) |
51 | | - {"1" 1 |
52 | | - "2" 2})))) |
| 58 | + {"1" 1 |
| 59 | + "2" 2})))) |
53 | 60 |
|
54 | 61 | (testing "upsert by 2 attrs with tempid" |
55 | 62 | (let [tx (d/with db [{:db/id -1 :name "Ivan" :email "@1" :age 35}])] |
56 | | - (is (= (touched tx 1) |
57 | | - {:name "Ivan" :email "@1" :age 35})) |
| 63 | + (is (= {:db/id 1 :name "Ivan" :email "@1" :age 35} |
| 64 | + (pull tx 1))) |
58 | 65 | (is (= (tempids tx) |
59 | | - {-1 1})))) |
| 66 | + {-1 1})))) |
60 | 67 |
|
61 | 68 | (testing "upsert to two entities, resolve to same tempid" |
62 | 69 | (let [tx (d/with db [{:db/id -1 :name "Ivan" :age 35} |
63 | 70 | {:db/id -1 :name "Ivan" :age 36}])] |
64 | | - (is (= (touched tx 1) |
65 | | - {:name "Ivan" :email "@1" :age 36})) |
| 71 | + (is (= {:db/id 1 :name "Ivan" :email "@1" :age 36} |
| 72 | + (pull tx 1))) |
66 | 73 | (is (= (tempids tx) |
67 | | - {-1 1})))) |
| 74 | + {-1 1})))) |
68 | 75 |
|
69 | 76 | (testing "upsert to two entities, two tempids" |
70 | 77 | (let [tx (d/with db [{:db/id -1 :name "Ivan" :age 35} |
71 | 78 | {:db/id -2 :name "Ivan" :age 36}])] |
72 | | - (is (= (touched tx 1) |
73 | | - {:name "Ivan" :email "@1" :age 36})) |
| 79 | + (is (= {:db/id 1 :name "Ivan" :email "@1" :age 36} |
| 80 | + (pull tx 1))) |
74 | 81 | (is (= (tempids tx) |
75 | | - {-1 1, -2 1})))) |
| 82 | + {-1 1, -2 1})))) |
76 | 83 |
|
77 | 84 | (testing "upsert with existing id" |
78 | 85 | (let [tx (d/with db [{:db/id 1 :name "Ivan" :age 35}])] |
79 | | - (is (= (touched tx 1) |
80 | | - {:name "Ivan" :email "@1" :age 35})) |
| 86 | + (is (= {:db/id 1 :name "Ivan" :email "@1" :age 35} |
| 87 | + (pull tx 1))) |
81 | 88 | (is (= (tempids tx) |
82 | | - {})))) |
| 89 | + {})))) |
83 | 90 |
|
84 | 91 | (testing "upsert by 2 attrs with existing id" |
85 | 92 | (let [tx (d/with db [{:db/id 1 :name "Ivan" :email "@1" :age 35}])] |
86 | | - (is (= (touched tx 1) |
87 | | - {:name "Ivan" :email "@1" :age 35})) |
| 93 | + (is (= {:db/id 1 :name "Ivan" :email "@1" :age 35} |
| 94 | + (pull tx 1))) |
88 | 95 | (is (= (tempids tx) |
89 | | - {})))) |
| 96 | + {})))) |
90 | 97 |
|
91 | 98 | (testing "upsert by 2 attrs with existing id as lookup ref" |
92 | 99 | (let [tx (d/with db [{:db/id [:name "Ivan"] :name "Ivan" :email "@1" :age 35}])] |
93 | | - (is (= (touched tx 1) |
94 | | - {:name "Ivan" :email "@1" :age 35})) |
| 100 | + (is (= {:db/id 1 :name "Ivan" :email "@1" :age 35} |
| 101 | + (pull tx 1))) |
95 | 102 | (is (= (tempids tx) |
96 | | - {})))) |
| 103 | + {})))) |
97 | 104 |
|
98 | 105 | (testing "upsert conflicts with existing id" |
99 | 106 | (is (thrown-with-msg? Throwable #"Conflicting upsert: \[:name \"Ivan\"\] resolves to 1, but entity already has :db/id 2" |
100 | | - (d/with db [{:db/id 2 :name "Ivan" :age 36}])))) |
| 107 | + (d/with db [{:db/id 2 :name "Ivan" :age 36}])))) |
101 | 108 |
|
102 | 109 | (testing "upsert conflicts with non-existing id" |
103 | | - (is (thrown-with-msg? Throwable #"Conflicting upsert: \[:name \"Ivan\"\] resolves to 1, but entity already has :db/id 3" |
104 | | - (d/with db [{:db/id 3 :name "Ivan" :age 36}])))) |
| 110 | + (is (thrown-with-msg? Throwable #"Conflicting upsert: \[:name \"Ivan\"\] resolves to 1, but entity already has :db/id 5" |
| 111 | + (d/with db [{:db/id 5 :name "Ivan" :age 36}])))) |
105 | 112 |
|
106 | 113 | (testing "upsert by non-existing value resolves as update" |
107 | | - (let [tx (d/with db [{:name "Ivan" :email "@3" :age 35}])] |
108 | | - (is (= (touched tx 1) |
109 | | - {:name "Ivan" :email "@3" :age 35})) |
| 114 | + (let [tx (d/with db [{:name "Ivan" :email "@5" :age 35}])] |
| 115 | + (is (= {:db/id 1 :name "Ivan" :email "@5" :age 35} |
| 116 | + (pull tx 1))) |
110 | 117 | (is (= (tempids tx) |
111 | | - {})))) |
| 118 | + {})))) |
112 | 119 |
|
113 | 120 | (testing "upsert by 2 conflicting fields" |
114 | 121 | (is (thrown-with-msg? Throwable #"Conflicting upserts: \[:name \"Ivan\"\] resolves to 1, but \[:email \"@2\"\] resolves to 2" |
115 | | - (d/with db [{:name "Ivan" :email "@2" :age 35}])))) |
| 122 | + (d/with db [{:name "Ivan" :email "@2" :age 35}])))) |
116 | 123 |
|
117 | 124 | (testing "upsert over intermediate db" |
118 | 125 | (let [tx (d/with db [{:name "Igor" :age 35} |
119 | 126 | {:name "Igor" :age 36}])] |
120 | | - (is (= (touched tx 3) |
121 | | - {:name "Igor" :age 36})) |
| 127 | + (is (= {:db/id 5 :name "Igor" :age 36} |
| 128 | + (pull tx 5))) |
122 | 129 | (is (= (tempids tx) |
123 | | - {3 3})))) |
| 130 | + {5 5})))) |
124 | 131 |
|
125 | 132 | (testing "upsert over intermediate db, tempids" |
126 | 133 | (let [tx (d/with db [{:db/id -1 :name "Igor" :age 35} |
127 | 134 | {:db/id -1 :name "Igor" :age 36}])] |
128 | | - (is (= (touched tx 3) |
129 | | - {:name "Igor" :age 36})) |
| 135 | + (is (= {:db/id 5 :name "Igor" :age 36} |
| 136 | + (pull tx 5))) |
130 | 137 | (is (= (tempids tx) |
131 | | - {-1 3})))) |
| 138 | + {-1 5})))) |
132 | 139 |
|
133 | 140 | (testing "upsert over intermediate db, different tempids" |
134 | 141 | (let [tx (d/with db [{:db/id -1 :name "Igor" :age 35} |
135 | 142 | {:db/id -2 :name "Igor" :age 36}])] |
136 | | - (is (= (touched tx 3) |
137 | | - {:name "Igor" :age 36})) |
| 143 | + (is (= {:db/id 5 :name "Igor" :age 36} |
| 144 | + (pull tx 5))) |
138 | 145 | (is (= (tempids tx) |
139 | | - {-1 3, -2 3})))) |
| 146 | + {-1 5, -2 5})))) |
140 | 147 |
|
141 | 148 | (testing "upsert and :current-tx conflict" |
142 | 149 | (is (thrown-with-msg? Throwable #"Conflicting upsert: \[:name \"Ivan\"\] resolves to 1, but entity already has :db/id \d+" |
143 | | - (d/with db [{:db/id :db/current-tx :name "Ivan" :age 35}])))) |
| 150 | + (d/with db [{:db/id :db/current-tx :name "Ivan" :age 35}])))) |
144 | 151 |
|
145 | 152 | (testing "upsert of unique, cardinality-many values" |
146 | 153 | (let [tx (d/with db [{:name "Ivan" :slugs "ivan1"} |
147 | 154 | {:name "Petr" :slugs "petr1"}]) |
148 | 155 | tx2 (d/with (:db-after tx) [{:name "Ivan" :slugs ["ivan1" "ivan2"]}])] |
149 | | - (is (= (touched tx 1) |
150 | | - {:name "Ivan" :email "@1" :slugs #{"ivan1"}})) |
151 | | - (is (= (touched tx2 1) |
152 | | - {:name "Ivan" :email "@1" :slugs #{"ivan1" "ivan2"}})) |
| 156 | + (is (= {:db/id 1 :name "Ivan" :email "@1" :slugs ["ivan1"]} |
| 157 | + (pull tx 1))) |
| 158 | + (is (= {:db/id 1 :name "Ivan" :email "@1" :slugs ["ivan1" "ivan2"]} |
| 159 | + (pull tx2 1))) |
153 | 160 | (is (thrown-with-msg? Throwable #"Conflicting upserts:" |
154 | 161 | (d/with (:db-after tx) [{:slugs ["ivan1" "petr1"]}]))))) |
| 162 | + |
| 163 | + (testing "upsert by ref" |
| 164 | + (let [tx (d/with db [{:ref 3 :age 36}])] |
| 165 | + (is (= {:db/id 2 :name "Petr" :email "@2" :ref 3 :age 36} |
| 166 | + (pull tx 2)))) |
| 167 | + (let [tx (d/with db [{:ref 4 :age 37}])] |
| 168 | + (is (= {:db/id 3 :name "Dima" :email "@3" :ref 4 :age 37} |
| 169 | + (pull tx 3)))) |
| 170 | + (let [tx (d/with db [{:ref 1 :age 38}])] |
| 171 | + (is (= {:db/id 4 :name "Olga" :email "@4" :ref 1 :age 38} |
| 172 | + (pull tx 4))))) |
| 173 | + |
| 174 | + (testing "upsert by lookup ref" |
| 175 | + (let [tx (d/with db [{:ref [:name "Dima"] :age 36}])] |
| 176 | + (is (= {:db/id 2 :name "Petr" :email "@2" :ref 3 :age 36} |
| 177 | + (pull tx 2)))) |
| 178 | + (let [tx (d/with db [{:ref [:name "Olga"] :age 37}])] |
| 179 | + (is (= {:db/id 3 :name "Dima" :email "@3" :ref 4 :age 37} |
| 180 | + (pull tx 3)))) |
| 181 | + (let [tx (d/with db [{:ref [:name "Ivan"] :age 38}])] |
| 182 | + (is (= {:db/id 4 :name "Olga" :email "@4" :ref 1 :age 38} |
| 183 | + (pull tx 4))))) |
| 184 | + |
| 185 | + ;; https://github.com/tonsky/datascript/issues/464 |
| 186 | + (testing "not upsert by ref" |
| 187 | + (let [tx (d/with db [{:db/id -1 :name "Igor"} |
| 188 | + {:db/id -2 :name "Anna" :ref -1}])] |
| 189 | + (is (= {:db/id 5 :name "Igor"} (pull tx 5))) |
| 190 | + (is (= {:db/id 6 :name "Anna" :ref 5} (pull tx 6)))) |
| 191 | + |
| 192 | + (let [tx (d/with db [{:db/id "A" :name "Igor"} |
| 193 | + {:db/id "B" :name "Anna" :ref "A"}])] |
| 194 | + (is (= {:db/id 5 :name "Igor"} (pull tx 5))) |
| 195 | + (is (= {:db/id 6 :name "Anna" :ref 5} (pull tx 6))))) |
| 196 | + |
155 | 197 | )) |
156 | 198 |
|
157 | 199 |
|
158 | 200 | (deftest test-redefining-ids |
159 | 201 | (let [db (-> (d/empty-db {:name { :db/unique :db.unique/identity }}) |
160 | | - (d/db-with [{:db/id -1 :name "Ivan"}]))] |
| 202 | + (d/db-with [{:db/id -1 :name "Ivan"}]))] |
161 | 203 | (let [tx (d/with db [{:db/id -1 :age 35} |
162 | 204 | {:db/id -1 :name "Ivan" :age 36}])] |
163 | 205 | (is (= #{[1 :age 36] [1 :name "Ivan"]} |
164 | | - (tdc/all-datoms (:db-after tx)))) |
| 206 | + (tdc/all-datoms (:db-after tx)))) |
165 | 207 | (is (= {-1 1, :db/current-tx (+ d/tx0 2)} |
166 | | - (:tempids tx))))) |
| 208 | + (:tempids tx))))) |
167 | 209 |
|
168 | 210 | (let [db (-> (d/empty-db {:name { :db/unique :db.unique/identity }}) |
169 | | - (d/db-with [{:db/id -1 :name "Ivan"} |
170 | | - {:db/id -2 :name "Oleg"}]))] |
| 211 | + (d/db-with [{:db/id -1 :name "Ivan"} |
| 212 | + {:db/id -2 :name "Oleg"}]))] |
171 | 213 | (is (thrown-with-msg? Throwable #"Conflicting upsert: -1 resolves both to 1 and 2" |
172 | 214 | (d/with db [{:db/id -1 :name "Ivan" :age 35} |
173 | 215 | {:db/id -1 :name "Oleg" :age 36}]))))) |
174 | 216 |
|
175 | 217 | ;; https://github.com/tonsky/datascript/issues/285 |
176 | 218 | (deftest test-retries-order |
177 | 219 | (let [db (-> (d/empty-db {:name {:db/unique :db.unique/identity}}) |
178 | | - (d/db-with [[:db/add -1 :age 42] |
179 | | - [:db/add -2 :likes "Pizza"] |
180 | | - [:db/add -1 :name "Bob"] |
181 | | - [:db/add -2 :name "Bob"]]))] |
| 220 | + (d/db-with [[:db/add -1 :age 42] |
| 221 | + [:db/add -2 :likes "Pizza"] |
| 222 | + [:db/add -1 :name "Bob"] |
| 223 | + [:db/add -2 :name "Bob"]]))] |
182 | 224 | (is (= {:db/id 1, :name "Bob", :likes "Pizza", :age 42} |
183 | | - (tdc/entity-map db 1)))) |
| 225 | + (tdc/entity-map db 1)))) |
184 | 226 |
|
185 | 227 | (let [db (-> (d/empty-db {:name {:db/unique :db.unique/identity}}) |
186 | | - (d/db-with [[:db/add -1 :age 42] |
187 | | - [:db/add -2 :likes "Pizza"] |
188 | | - [:db/add -2 :name "Bob"] |
189 | | - [:db/add -1 :name "Bob"]]))] |
| 228 | + (d/db-with [[:db/add -1 :age 42] |
| 229 | + [:db/add -2 :likes "Pizza"] |
| 230 | + [:db/add -2 :name "Bob"] |
| 231 | + [:db/add -1 :name "Bob"]]))] |
190 | 232 | (is (= {:db/id 2, :name "Bob", :likes "Pizza", :age 42} |
191 | | - (tdc/entity-map db 2))))) |
| 233 | + (tdc/entity-map db 2))))) |
192 | 234 |
|
193 | 235 | ;; https://github.com/tonsky/datascript/issues/403 |
194 | 236 | (deftest test-upsert-string-tempid-ref |
195 | 237 | (let [db (-> (d/empty-db {:name {:db/unique :db.unique/identity} |
196 | 238 | :ref {:db/valueType :db.type/ref}}) |
197 | | - (d/db-with [{:name "Alice"}])) |
| 239 | + (d/db-with [{:name "Alice"}])) |
198 | 240 | expected #{[1 :name "Alice"] |
199 | 241 | [2 :age 36] |
200 | 242 | [2 :ref 1]}] |
|
213 | 255 |
|
214 | 256 | (deftest test-vector-upsert |
215 | 257 | (let [db (-> (d/empty-db {:name {:db/unique :db.unique/identity}}) |
216 | | - (d/db-with [{:db/id -1, :name "Ivan"}]))] |
| 258 | + (d/db-with [{:db/id -1, :name "Ivan"}]))] |
217 | 259 | (are [tx res] (= res (tdc/all-datoms (d/db-with db tx))) |
218 | 260 | [[:db/add -1 :name "Ivan"] |
219 | 261 | [:db/add -1 :age 12]] |
|
224 | 266 | #{[1 :age 12] [1 :name "Ivan"]})) |
225 | 267 |
|
226 | 268 | (let [db (-> (d/empty-db {:name { :db/unique :db.unique/identity }}) |
227 | | - (d/db-with [[:db/add -1 :name "Ivan"] |
228 | | - [:db/add -2 :name "Oleg"]]))] |
| 269 | + (d/db-with [[:db/add -1 :name "Ivan"] |
| 270 | + [:db/add -2 :name "Oleg"]]))] |
229 | 271 | (is (thrown-with-msg? Throwable #"Conflicting upsert: -1 resolves both to 1 and 2" |
230 | 272 | (d/with db [[:db/add -1 :name "Ivan"] |
231 | 273 | [:db/add -1 :age 35] |
|
0 commit comments