Part 2: Add ISQLiteConnectionFactory and CLJC test that uses it.

This commit is contained in:
Nick Alexander 2016-07-13 16:17:07 -07:00
parent cca5010671
commit bf080ced3c
5 changed files with 60 additions and 0 deletions

View file

@ -38,3 +38,12 @@
(j/get-connection spec)
(assoc spec :connection)
(->JDBCSQLiteConnection)))))
(extend-protocol s/ISQLiteConnectionFactory
String
(<sqlite-connection [path]
(open path))
java.io.File
(<sqlite-connection [path]
(open path)))

View file

@ -35,3 +35,12 @@
(->
(.open sqlite.DB path (clj->js {:mode mode}))
(.then ->SQLite3Connection))))
(extend-protocol s/ISQLiteConnectionFactory
string
(<sqlite-connection [path]
(open path))
object ;; TODO: narrow this to the result of node-tempfile/tempfile.
(<sqlite-connection [tempfile]
(open (.-name tempfile))))

View file

@ -33,6 +33,11 @@
[db]
"Close this SQLite connection. Returns a pair channel of [nil error]."))
(defprotocol ISQLiteConnectionFactory
(<sqlite-connection
[path]
"Open an ISQLiteConnection to `path`. Returns a pair channel of `[sqlite-connection error]`"))
(defn execute!
[db [sql & bindings]]
(-execute! db sql bindings))

View file

@ -0,0 +1,35 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
(ns datomish.sqlite-user-version-test
#?(:cljs
(:require-macros
[datomish.pair-chan :refer [go-pair <?]]
[datomish.node-tempfile-macros :refer [with-tempfile]]
[cljs.core.async.macros :as a :refer [go]]))
(:require
[datomish.util :as util :refer [raise]]
[datomish.sqlite :as s]
#?@(:clj [[datomish.pair-chan :refer [go-pair <?]]
[tempfile.core :refer [tempfile with-tempfile]]
[datomish.test-macros :refer [deftest-async]]
[clojure.test :as t :refer [is are deftest testing]]
[clojure.core.async :refer [go <! >!]]])
#?@(:cljs [[datomish.pair-chan]
[datomish.test-macros :refer-macros [deftest-async]]
[datomish.node-tempfile :refer [tempfile]]
[cljs.test :as t :refer-macros [is are deftest testing async]]
[cljs.core.async :as a :refer [<! >!]]])))
(deftest-async test-all-rows
(with-tempfile [t (tempfile)]
(let [db (<? (s/<sqlite-connection 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)))))))

View file

@ -3,6 +3,7 @@
[doo.runner :refer-macros [doo-tests doo-all-tests]]
[cljs.test :as t :refer-macros [is are deftest testing]]
datomish.promise-sqlite-test
datomish.sqlite-user-version-test
datomish.test.util
datomish.test.transforms
datomish.test.query
@ -10,6 +11,7 @@
(doo-tests
'datomish.promise-sqlite-test
'datomish.sqlite-user-version-test
'datomish.test.util
'datomish.test.transforms
'datomish.test.query