Skip to content

Commit

Permalink
Use Datomic abstraction in repl and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-stuttaford committed Mar 17, 2018
1 parent 51d732a commit eecc314
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
20 changes: 15 additions & 5 deletions dev/bridge/dev/repl.clj
@@ -1,14 +1,24 @@
(ns bridge.dev.repl
(:require [bridge.config :as config]
[datomic.api :as d]))
[bridge.data.datomic :as datomic]
[integrant.core :as ig]))

;; uses Datomic Peer library

(defn uri []
(get-in (config/system) [:datomic/connection :uri]))
(defn init-datomic []
(ig/init-key :datomic/connection
(:datomic/connection (config/system))))

(defn set-datomic-mode! [mode]
(alter-var-root datomic/*DATOMIC-MODE* (constantly mode)))

(defn conn []
(d/connect (uri)))
(when (nil? datomic/*DATOMIC-MODE*)
(throw (ex-info (str "Set `bridge.data.datomic/*DATOMIC-MODE*` first."
"You can use `bridge.dev.repl/set-datomic-mode!` "
"to do so at the repl.")
{})))
(:datomic/conn (init-datomic)))

(defn db []
(d/db (conn)))
(datomic/db (conn)))
5 changes: 5 additions & 0 deletions src/bridge/data/datomic.clj
Expand Up @@ -44,6 +44,11 @@
:peer @(d/transact conn tx)
:client (dc/transact conn {:tx-data tx})))

(defn with [conn tx]
(case *DATOMIC-MODE*
:peer (d/with (d/db conn) tx)
:client (dc/with (dc/with-db conn) {:tx-data tx})))

(defn entid [db id]
(case *DATOMIC-MODE*
:peer (d/entid db id)
Expand Down
12 changes: 5 additions & 7 deletions test/bridge/person/data_test.clj
@@ -1,10 +1,9 @@
(ns bridge.person.data-test
(:require [bridge.data.datomic :as datomic]
[bridge.person.data :as person.data]
[bridge.test.util :refer [db test-setup with-database]]
[bridge.test.util :refer [conn test-setup with-database]]
[clojure.spec.alpha :as s]
[clojure.test :refer [deftest is use-fixtures]]
[datomic.api :as d])
[clojure.test :refer [deftest is use-fixtures]])
(:import clojure.lang.ExceptionInfo))

(def db-name (str *ns*))
Expand All @@ -13,7 +12,7 @@
(use-fixtures :each (with-database db-name person.data/schema))

(defn db-after-tx [tx]
(:db-after (d/with (db db-name) tx)))
(:db-after (datomic/with (conn db-name) tx)))

(deftest new-person-tx
(is (thrown-with-msg? ExceptionInfo #"did not conform to spec"
Expand All @@ -29,9 +28,8 @@
(is (= :status/active (:person/status tx)))

(let [db (db-after-tx [tx])
person (d/entity db [:person/email TEST-EMAIL])
password (datomic/attr db (:db/id (d/entity db [:person/email TEST-EMAIL]))
:person/password)]
person (datomic/entid db [:person/email TEST-EMAIL])
password (datomic/attr db [:person/email TEST-EMAIL] :person/password)]

(is (some? person))
(is (person.data/correct-password? password TEST-PASSWORD))
Expand Down

0 comments on commit eecc314

Please sign in to comment.