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:
Nick Alexander 2016-07-13 16:16:31 -07:00
parent ef0454dd43
commit cca5010671
6 changed files with 79 additions and 99 deletions

View file

@ -25,7 +25,7 @@
:optimizations :advanced
:source-map "target/advanced/datomish.js.map"
:pretty-print true
:recompile-dependents false
:recompile-dependents true
:parallel-build true
}}
:test {
@ -35,7 +35,7 @@
:main datomish.test
:optimizations :none
:source-map true
:recompile-dependents false
:recompile-dependents true
:parallel-build true
:target :nodejs
}}

View file

@ -30,7 +30,7 @@
(.close (.-db db)))))
(defn open
[path & {:keys [mode] :or {:mode 6}}]
[path & {:keys [mode] :or {mode 6}}]
(cljs-promises.async/pair-port
(->
(.open sqlite.DB path (clj->js {:mode mode}))

View file

@ -3,7 +3,8 @@
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
(ns datomish.test-macros
(:refer-clojure :exclude [with-open])
#?(:cljs
(:require-macros [datomish.test-macros]))
(:require
[datomish.pair-chan]))
@ -29,42 +30,10 @@
(cljs.test/async done#
(->
(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))
(done#))))))
(clojure.test/deftest
~(with-meta name {:async true})
(let [[v# e#] (clojure.core.async/<!! (datomish.pair-chan/go-pair ~@body))]
(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"))))

View file

@ -14,35 +14,40 @@
(deftest-async test-all-rows
(with-tempfile [t (tempfile)]
(with-open [db (<? (j/open t))]
(<? (s/execute! db ["CREATE TABLE test (a INTEGER)"]))
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 1]))
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 2]))
(let [rows (<? (s/all-rows db ["SELECT * FROM test ORDER BY a ASC"]))]
(is (= rows [{:a 1} {:a 2}]))))))
(let [db (<? (j/open t))]
(try
(<? (s/execute! db ["CREATE TABLE test (a INTEGER)"]))
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 1]))
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 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!
(with-tempfile [t (tempfile)]
(with-open [db (<? (j/open t))]
(<? (s/execute! db ["CREATE TABLE ta (a INTEGER)"]))
(<? (s/execute! db ["CREATE TABLE tb (b INTEGER)"]))
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 1]))
(let [[v e] (<! (s/in-transaction! db #(s/execute! db ["INSERT INTO tb VALUES (?)" 2])))]
(is (not e)))
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
(is (= rows [{:a 1}])))
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
(is (= rows [{:b 2}])))
(println "a")
(let [f #(go-pair
;; The first succeeds ...
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 3]))
;; ... but will get rolled back by the second failing.
(<? (s/execute! db ["INSERT INTO tb VALUES (?)" 4 "bad parameter"])))
[v e] (<! (s/in-transaction! db f))]
(is (some? e)))
;; No changes, since the transaction as a whole failed.
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
(is (= rows [{:a 1}])))
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
(is (= rows [{:b 2}]))))))
(let [db (<? (j/open t))]
(try
(<? (s/execute! db ["CREATE TABLE ta (a INTEGER)"]))
(<? (s/execute! db ["CREATE TABLE tb (b INTEGER)"]))
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 1]))
(let [[v e] (<! (s/in-transaction! db #(s/execute! db ["INSERT INTO tb VALUES (?)" 2])))]
(is (not e)))
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
(is (= rows [{:a 1}])))
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
(is (= rows [{:b 2}])))
(let [f #(go-pair
;; The first succeeds ...
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 3]))
;; ... but will get rolled back by the second failing.
(<? (s/execute! db ["INSERT INTO tb VALUES (?)" 4 "bad parameter"])))
[v e] (<! (s/in-transaction! db f))]
(is (some? e)))
;; No changes, since the transaction as a whole failed.
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
(is (= rows [{:a 1}])))
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
(is (= rows [{:b 2}])))
(finally
(<? (s/close db)))))))

View file

@ -5,7 +5,7 @@
(ns datomish.promise-sqlite-test
(:require-macros
[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]]
[cljs.core.async.macros])
(:require
@ -18,35 +18,40 @@
(deftest-async test-all-rows
(with-tempfile [t (tempfile)]
(with-open [db (<? (ps/open (.name t) :mode 6))]
(<? (s/execute! db ["CREATE TABLE test (a INTEGER)"]))
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 1]))
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 2]))
(let [rows (<? (s/all-rows db ["SELECT * FROM test ORDER BY a ASC"]))]
(is (= rows [{:a 1} {:a 2}]))))))
(let [db (<? (ps/open (.-name t)))]
(try
(<? (s/execute! db ["CREATE TABLE test (a INTEGER)"]))
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 1]))
(<? (s/execute! db ["INSERT INTO test VALUES (?)" 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!
(with-tempfile [t (tempfile)]
(with-open [db (<? (ps/open (.name t) :mode 6))]
(<? (s/execute! db ["CREATE TABLE ta (a INTEGER)"]))
(<? (s/execute! db ["CREATE TABLE tb (b INTEGER)"]))
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 1]))
(let [[v e] (<! (s/in-transaction! db #(s/execute! db ["INSERT INTO tb VALUES (?)" 2])))]
(is (not e)))
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
(is (= rows [{:a 1}])))
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
(is (= rows [{:b 2}])))
(println "a")
(let [f #(go-pair
;; The first succeeds ...
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 3]))
;; ... but will get rolled back by the second failing.
(<? (s/execute! db ["INSERT INTO tb VALUES (?)" 4 "bad parameter"])))
[v e] (<! (s/in-transaction! db f))]
(is (some? e)))
;; No changes, since the transaction as a whole failed.
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
(is (= rows [{:a 1}])))
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
(is (= rows [{:b 2}]))))))
(let [db (<? (ps/open (.-name t)))]
(try
(<? (s/execute! db ["CREATE TABLE ta (a INTEGER)"]))
(<? (s/execute! db ["CREATE TABLE tb (b INTEGER)"]))
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 1]))
(let [[v e] (<! (s/in-transaction! db #(s/execute! db ["INSERT INTO tb VALUES (?)" 2])))]
(is (not e)))
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
(is (= rows [{:a 1}])))
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
(is (= rows [{:b 2}])))
(let [f #(go-pair
;; The first succeeds ...
(<? (s/execute! db ["INSERT INTO ta VALUES (?)" 3]))
;; ... but will get rolled back by the second failing.
(<? (s/execute! db ["INSERT INTO tb VALUES (?)" 4 "bad parameter"])))
[v e] (<! (s/in-transaction! db f))]
(is (some? e)))
;; No changes, since the transaction as a whole failed.
(let [rows (<? (s/all-rows db ["SELECT * FROM ta ORDER BY a ASC"]))]
(is (= rows [{:a 1}])))
(let [rows (<? (s/all-rows db ["SELECT * FROM tb ORDER BY b ASC"]))]
(is (= rows [{:b 2}])))
(finally
(<? (s/close db)))))))

View file

@ -5,10 +5,11 @@
(ns datomish.test-macros-test
(:require-macros
[datomish.pair-chan :refer [go-pair]]
[datomish.test-macros :refer [deftest-async]]
[cljs.core.async.macros])
(:require [cljs.core.async]
[cljs.test :refer-macros [is are deftest testing async]]))
(:require
[datomish.test-macros :refer-macros [deftest-async]]
[cljs.core.async]
[cljs.test :refer-macros [is are deftest testing async]]))
(deftest sync-test
(is (= 1 1)))