From 9f3987baaaf022f4b7c4b4fcafa72b36d05762c9 Mon Sep 17 00:00:00 2001 From: Akash Shakdwipeea Date: Thu, 14 Dec 2017 22:05:00 +0530 Subject: [PATCH] Add init-fn for datomic component test demonstrates the ability to init and check for schema --- project.clj | 2 +- src/system/components/datomic.clj | 12 +++++++---- test/system/components/datomic_test.clj | 28 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/project.clj b/project.clj index 085ebaa..c0739de 100644 --- a/project.clj +++ b/project.clj @@ -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"] diff --git a/src/system/components/datomic.clj b/src/system/components/datomic.clj index 911f20b..61b7201 100644 --- a/src/system/components/datomic.clj +++ b/src/system/components/datomic.clj @@ -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}))) diff --git a/test/system/components/datomic_test.clj b/test/system/components/datomic_test.clj index 7718fed..5ce8151 100644 --- a/test/system/components/datomic_test.clj +++ b/test/system/components/datomic_test.clj @@ -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 @@ -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)))