Skip to content

Commit

Permalink
Add init-fn for datomic component
Browse files Browse the repository at this point in the history
test demonstrates the ability to init and check for schema
  • Loading branch information
shakdwipeea committed Dec 14, 2017
1 parent 4f4b876 commit 77d8b7b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[im.chit/hara.io.watch "2.1.7"]
[im.chit/hara.io.scheduler "2.3.6"]
[im.chit/adi "0.3.2"]
[com.datomic/datomic-free "0.9.4815.12"]
[com.datomic/datomic-free "0.9.5656"]
[com.novemberain/monger "3.0.2"]
[org.clojure/java.jdbc "0.3.5"]
[com.h2database/h2 "1.4.181"]
Expand Down
12 changes: 8 additions & 4 deletions src/system/components/datomic.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
(:require [com.stuartsierra.component :as component]
[datomic.api :as d]))

(defrecord Datomic [uri conn]
(defrecord Datomic [uri conn init-fn]
component/Lifecycle
(start [component]
(let [db (d/create-database uri)
conn (d/connect uri)]
conn (d/connect uri)
_ (when init-fn (init-fn conn))]
(assoc component :conn conn)))
(stop [component]
(when conn (d/release conn))
(assoc component :conn nil)))

(defn new-datomic-db [uri]
(map->Datomic {:uri uri}))
(defn new-datomic-db
([uri]
(map->Datomic {:uri uri}))
([uri init-fn]
(map->Datomic {:uri uri :init-fn init-fn})))
28 changes: 28 additions & 0 deletions test/system/components/datomic_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[clojure.test :refer [deftest testing is]]))

(def uri "datomic:mem://localhost:4334/framework-test")

(def datomic-db (new-datomic-db uri))

(deftest datomic-lifecycle
Expand All @@ -14,3 +15,30 @@
datomic.peer.LocalConnection))
(is (d/delete-database uri))
(alter-var-root #'datomic-db component/stop)))

(def schema '[{:db/ident ::name
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "a test attr"}])

(defn init-schema [conn]
@(d/transact conn schema))

(def datomic-db-with-schema (new-datomic-db uri
init-schema))

(defn has-attribute?
"Does database have an attribute named attr-name?"
[db attr-name]
(-> (d/entity db attr-name)
:db.install/_attribute
boolean))

(deftest datomic-lifecycle-with-schema
(testing "Datomic lifecycle operations."
(alter-var-root #'datomic-db-with-schema component/start)
(is (= (type (:conn datomic-db-with-schema))
datomic.peer.LocalConnection))
(is (has-attribute? (d/db (:conn datomic-db-with-schema)) ::name))
(is (d/delete-database uri))
(alter-var-root #'datomic-db-with-schema component/stop)))

0 comments on commit 77d8b7b

Please sign in to comment.