Part 1: Fix testing errors.
Some of these were just typos, but `with-open` was fatally flawed on CLJS (we couldn't call `.close` at all), and `deftest-async` was hiding all failures (due to a typo).
This commit is contained in:
parent
ef0454dd43
commit
cca5010671
6 changed files with 79 additions and 99 deletions
|
@ -25,7 +25,7 @@
|
||||||
:optimizations :advanced
|
:optimizations :advanced
|
||||||
:source-map "target/advanced/datomish.js.map"
|
:source-map "target/advanced/datomish.js.map"
|
||||||
:pretty-print true
|
:pretty-print true
|
||||||
:recompile-dependents false
|
:recompile-dependents true
|
||||||
:parallel-build true
|
:parallel-build true
|
||||||
}}
|
}}
|
||||||
:test {
|
:test {
|
||||||
|
@ -35,7 +35,7 @@
|
||||||
:main datomish.test
|
:main datomish.test
|
||||||
:optimizations :none
|
:optimizations :none
|
||||||
:source-map true
|
:source-map true
|
||||||
:recompile-dependents false
|
:recompile-dependents true
|
||||||
:parallel-build true
|
:parallel-build true
|
||||||
:target :nodejs
|
:target :nodejs
|
||||||
}}
|
}}
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
(.close (.-db db)))))
|
(.close (.-db db)))))
|
||||||
|
|
||||||
(defn open
|
(defn open
|
||||||
[path & {:keys [mode] :or {:mode 6}}]
|
[path & {:keys [mode] :or {mode 6}}]
|
||||||
(cljs-promises.async/pair-port
|
(cljs-promises.async/pair-port
|
||||||
(->
|
(->
|
||||||
(.open sqlite.DB path (clj->js {:mode mode}))
|
(.open sqlite.DB path (clj->js {:mode mode}))
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
(ns datomish.test-macros
|
(ns datomish.test-macros
|
||||||
(:refer-clojure :exclude [with-open])
|
#?(:cljs
|
||||||
|
(:require-macros [datomish.test-macros]))
|
||||||
(:require
|
(:require
|
||||||
[datomish.pair-chan]))
|
[datomish.pair-chan]))
|
||||||
|
|
||||||
|
@ -29,42 +30,10 @@
|
||||||
(cljs.test/async done#
|
(cljs.test/async done#
|
||||||
(->
|
(->
|
||||||
(datomish.pair-chan/go-pair ~@body)
|
(datomish.pair-chan/go-pair ~@body)
|
||||||
(cljs.core.async/take! (fn [v# e#]
|
(cljs.core.async/take! (fn [[v# e#]]
|
||||||
(cljs.test/is (= e# nil))
|
(cljs.test/is (= e# nil))
|
||||||
(done#))))))
|
(done#))))))
|
||||||
(clojure.test/deftest
|
(clojure.test/deftest
|
||||||
~(with-meta name {:async true})
|
~(with-meta name {:async true})
|
||||||
(let [[v# e#] (clojure.core.async/<!! (datomish.pair-chan/go-pair ~@body))]
|
(let [[v# e#] (clojure.core.async/<!! (datomish.pair-chan/go-pair ~@body))]
|
||||||
(clojure.test/is (= e# nil))))))
|
(clojure.test/is (= e# nil))))))
|
||||||
|
|
||||||
;; CLJS doesn't expose `with-open`, for reasons unknown. Duplicate the definition of
|
|
||||||
;; `with-open` (and `assert-args`) here.
|
|
||||||
;; See https://github.com/clojure/clojure/blob/clojure-1.7.0/src/clj/clojure/core.clj#L3679-L3698.
|
|
||||||
(defmacro ^{:private true} assert-args
|
|
||||||
[& pairs]
|
|
||||||
`(do (when-not ~(first pairs)
|
|
||||||
(throw (IllegalArgumentException.
|
|
||||||
(str (first ~'&form) " requires " ~(second pairs) " in " ~'*ns* ":" (:line (meta ~'&form))))))
|
|
||||||
~(let [more (nnext pairs)]
|
|
||||||
(when more
|
|
||||||
(list* `assert-args more)))))
|
|
||||||
|
|
||||||
(defmacro with-open
|
|
||||||
"bindings => [name init ...]
|
|
||||||
Evaluates body in a try expression with names bound to the values
|
|
||||||
of the inits, and a finally clause that calls (.close name) on each
|
|
||||||
name in reverse order."
|
|
||||||
{:added "1.0"}
|
|
||||||
[bindings & body]
|
|
||||||
(assert-args
|
|
||||||
(vector? bindings) "a vector for its binding"
|
|
||||||
(even? (count bindings)) "an even number of forms in binding vector")
|
|
||||||
(cond
|
|
||||||
(= (count bindings) 0) `(do ~@body)
|
|
||||||
(symbol? (bindings 0)) `(let ~(subvec bindings 0 2)
|
|
||||||
(try
|
|
||||||
(with-open ~(subvec bindings 2) ~@body)
|
|
||||||
(finally
|
|
||||||
(. ~(bindings 0) close))))
|
|
||||||
:else (throw (java.lang.IllegalArgumentException.
|
|
||||||
"with-open only allows Symbols in bindings"))))
|
|
||||||
|
|
|
@ -14,35 +14,40 @@
|
||||||
|
|
||||||
(deftest-async test-all-rows
|
(deftest-async test-all-rows
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(with-open [db (<? (j/open t))]
|
(let [db (<? (j/open t))]
|
||||||
(<? (s/execute! db ["CREATE TABLE test (a INTEGER)"]))
|
(try
|
||||||
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 1]))
|
(<? (s/execute! db ["CREATE TABLE test (a INTEGER)"]))
|
||||||
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 2]))
|
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 1]))
|
||||||
(let [rows (<? (s/all-rows db ["SELECT * FROM test ORDER BY a ASC"]))]
|
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 2]))
|
||||||
(is (= rows [{:a 1} {:a 2}]))))))
|
(let [rows (<? (s/all-rows db ["SELECT * FROM test ORDER BY a ASC"]))]
|
||||||
|
(is (= rows [{:a 1} {:a 2}])))
|
||||||
|
(finally
|
||||||
|
(<? (s/close db)))))))
|
||||||
|
|
||||||
(deftest-async test-in-transaction!
|
(deftest-async test-in-transaction!
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(with-open [db (<? (j/open t))]
|
(let [db (<? (j/open t))]
|
||||||
(<? (s/execute! db ["CREATE TABLE ta (a INTEGER)"]))
|
(try
|
||||||
(<? (s/execute! db ["CREATE TABLE tb (b INTEGER)"]))
|
(<? (s/execute! db ["CREATE TABLE ta (a INTEGER)"]))
|
||||||
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 1]))
|
(<? (s/execute! db ["CREATE TABLE tb (b INTEGER)"]))
|
||||||
(let [[v e] (<! (s/in-transaction! db #(s/execute! db ["INSERT INTO tb VALUES (?)" 2])))]
|
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 1]))
|
||||||
(is (not e)))
|
(let [[v e] (<! (s/in-transaction! db #(s/execute! db ["INSERT INTO tb VALUES (?)" 2])))]
|
||||||
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
|
(is (not e)))
|
||||||
(is (= rows [{:a 1}])))
|
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
|
||||||
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
|
(is (= rows [{:a 1}])))
|
||||||
(is (= rows [{:b 2}])))
|
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
|
||||||
(println "a")
|
(is (= rows [{:b 2}])))
|
||||||
(let [f #(go-pair
|
(let [f #(go-pair
|
||||||
;; The first succeeds ...
|
;; The first succeeds ...
|
||||||
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 3]))
|
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 3]))
|
||||||
;; ... but will get rolled back by the second failing.
|
;; ... but will get rolled back by the second failing.
|
||||||
(<? (s/execute! db ["INSERT INTO tb VALUES (?)" 4 "bad parameter"])))
|
(<? (s/execute! db ["INSERT INTO tb VALUES (?)" 4 "bad parameter"])))
|
||||||
[v e] (<! (s/in-transaction! db f))]
|
[v e] (<! (s/in-transaction! db f))]
|
||||||
(is (some? e)))
|
(is (some? e)))
|
||||||
;; No changes, since the transaction as a whole failed.
|
;; No changes, since the transaction as a whole failed.
|
||||||
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
|
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
|
||||||
(is (= rows [{:a 1}])))
|
(is (= rows [{:a 1}])))
|
||||||
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
|
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
|
||||||
(is (= rows [{:b 2}]))))))
|
(is (= rows [{:b 2}])))
|
||||||
|
(finally
|
||||||
|
(<? (s/close db)))))))
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
(ns datomish.promise-sqlite-test
|
(ns datomish.promise-sqlite-test
|
||||||
(:require-macros
|
(:require-macros
|
||||||
[datomish.pair-chan :refer [go-pair <?]]
|
[datomish.pair-chan :refer [go-pair <?]]
|
||||||
[datomish.test-macros :refer [deftest-async with-open]]
|
[datomish.test-macros :refer [deftest-async]]
|
||||||
[datomish.node-tempfile-macros :refer [with-tempfile]]
|
[datomish.node-tempfile-macros :refer [with-tempfile]]
|
||||||
[cljs.core.async.macros])
|
[cljs.core.async.macros])
|
||||||
(:require
|
(:require
|
||||||
|
@ -18,35 +18,40 @@
|
||||||
|
|
||||||
(deftest-async test-all-rows
|
(deftest-async test-all-rows
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(with-open [db (<? (ps/open (.name t) :mode 6))]
|
(let [db (<? (ps/open (.-name t)))]
|
||||||
(<? (s/execute! db ["CREATE TABLE test (a INTEGER)"]))
|
(try
|
||||||
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 1]))
|
(<? (s/execute! db ["CREATE TABLE test (a INTEGER)"]))
|
||||||
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 2]))
|
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 1]))
|
||||||
(let [rows (<? (s/all-rows db ["SELECT * FROM test ORDER BY a ASC"]))]
|
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 2]))
|
||||||
(is (= rows [{:a 1} {:a 2}]))))))
|
(let [rows (<? (s/all-rows db ["SELECT * FROM test ORDER BY a ASC"]))]
|
||||||
|
(is (= rows [{:a 1} {:a 2}])))
|
||||||
|
(finally
|
||||||
|
(<? (s/close db)))))))
|
||||||
|
|
||||||
(deftest-async test-in-transaction!
|
(deftest-async test-in-transaction!
|
||||||
(with-tempfile [t (tempfile)]
|
(with-tempfile [t (tempfile)]
|
||||||
(with-open [db (<? (ps/open (.name t) :mode 6))]
|
(let [db (<? (ps/open (.-name t)))]
|
||||||
(<? (s/execute! db ["CREATE TABLE ta (a INTEGER)"]))
|
(try
|
||||||
(<? (s/execute! db ["CREATE TABLE tb (b INTEGER)"]))
|
(<? (s/execute! db ["CREATE TABLE ta (a INTEGER)"]))
|
||||||
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 1]))
|
(<? (s/execute! db ["CREATE TABLE tb (b INTEGER)"]))
|
||||||
(let [[v e] (<! (s/in-transaction! db #(s/execute! db ["INSERT INTO tb VALUES (?)" 2])))]
|
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 1]))
|
||||||
(is (not e)))
|
(let [[v e] (<! (s/in-transaction! db #(s/execute! db ["INSERT INTO tb VALUES (?)" 2])))]
|
||||||
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
|
(is (not e)))
|
||||||
(is (= rows [{:a 1}])))
|
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
|
||||||
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
|
(is (= rows [{:a 1}])))
|
||||||
(is (= rows [{:b 2}])))
|
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
|
||||||
(println "a")
|
(is (= rows [{:b 2}])))
|
||||||
(let [f #(go-pair
|
(let [f #(go-pair
|
||||||
;; The first succeeds ...
|
;; The first succeeds ...
|
||||||
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 3]))
|
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 3]))
|
||||||
;; ... but will get rolled back by the second failing.
|
;; ... but will get rolled back by the second failing.
|
||||||
(<? (s/execute! db ["INSERT INTO tb VALUES (?)" 4 "bad parameter"])))
|
(<? (s/execute! db ["INSERT INTO tb VALUES (?)" 4 "bad parameter"])))
|
||||||
[v e] (<! (s/in-transaction! db f))]
|
[v e] (<! (s/in-transaction! db f))]
|
||||||
(is (some? e)))
|
(is (some? e)))
|
||||||
;; No changes, since the transaction as a whole failed.
|
;; No changes, since the transaction as a whole failed.
|
||||||
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
|
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
|
||||||
(is (= rows [{:a 1}])))
|
(is (= rows [{:a 1}])))
|
||||||
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
|
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
|
||||||
(is (= rows [{:b 2}]))))))
|
(is (= rows [{:b 2}])))
|
||||||
|
(finally
|
||||||
|
(<? (s/close db)))))))
|
||||||
|
|
|
@ -5,10 +5,11 @@
|
||||||
(ns datomish.test-macros-test
|
(ns datomish.test-macros-test
|
||||||
(:require-macros
|
(:require-macros
|
||||||
[datomish.pair-chan :refer [go-pair]]
|
[datomish.pair-chan :refer [go-pair]]
|
||||||
[datomish.test-macros :refer [deftest-async]]
|
|
||||||
[cljs.core.async.macros])
|
[cljs.core.async.macros])
|
||||||
(:require [cljs.core.async]
|
(:require
|
||||||
[cljs.test :refer-macros [is are deftest testing async]]))
|
[datomish.test-macros :refer-macros [deftest-async]]
|
||||||
|
[cljs.core.async]
|
||||||
|
[cljs.test :refer-macros [is are deftest testing async]]))
|
||||||
|
|
||||||
(deftest sync-test
|
(deftest sync-test
|
||||||
(is (= 1 1)))
|
(is (= 1 1)))
|
||||||
|
|
Loading…
Reference in a new issue