-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcore.clj
More file actions
113 lines (100 loc) · 3.9 KB
/
core.clj
File metadata and controls
113 lines (100 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(ns datomic-local-tu.core
"Test utility for Datomic Local"
(:require
[clojure.string :as str]
[datomic.client.api :as d]
[datomic.local]
[clojure.java.io :as io]
[datomic-local-tu.internal.impl :as impl])
(:import (java.io Closeable File)))
(defn gen-name!
[prefix]
(str prefix "-" (impl/rand-str! 7)))
(def default-datomic-local-storage-dir
(.getAbsolutePath (io/file (System/getProperty "user.home") ".datomic" "data")))
(defn datomic-local-dir
^File [{:keys [system-name db-name storage-dir]
:or {storage-dir default-datomic-local-storage-dir}}]
(let [file-args (cond-> [storage-dir]
system-name (conj system-name)
db-name (conj db-name))]
(apply io/file file-args)))
(defn -new-env-map
[{:keys [prefix system storage-dir]
:or {prefix "datomic-local"}}]
(let [system (or system (gen-name! prefix))
storage-dir (if (or
(= storage-dir :mem)
(and storage-dir
(str/starts-with? storage-dir "/")))
storage-dir
(.getAbsolutePath
(datomic-local-dir
(cond-> {}
storage-dir
(assoc ::storage-dir storage-dir)))))
client-map {:server-type :datomic-local
:system system
:storage-dir storage-dir}]
{:client (d/client client-map)
:client-map client-map
:system system
:storage-dir storage-dir}))
(defn delete-datomic-local-system!
"Deletes a `:datomic-local` system's data. Always returns true. Throws on failure."
([system-name]
(delete-datomic-local-system! system-name default-datomic-local-storage-dir))
([system-name storage-dir]
(let [f (datomic-local-dir {:system-name system-name
:storage-dir storage-dir})]
(when (.exists ^File f)
(impl/delete-directory! f))
true)))
(comment
(-new-env-map {})
)
(defn release-dbs
"Releases all DBs."
[client system]
(doseq [db-name (d/list-databases client {})]
;; See "https://docs.datomic.com/api/datomic-local.html#release-db"
(datomic.local/release-db
{:system system :db-name db-name})))
(defn cleanup-env!
"Releases resources used by client and deletes the data directory for the system."
[{:keys [client system client-map]}]
(release-dbs client system)
(when (string? (:storage-dir client-map))
(delete-datomic-local-system! system)))
(defrecord TestEnv [system client]
Closeable
(close [env]
(cleanup-env! env)))
(defn test-env
"Returns a Closable test environment. Optionally takes a map with the following
keys.
:prefix - The prefix for the generated system name.
:system - Force a specific system name. Will override prefix.
:storage-dir - The storage directory for data passed to the d/client.
Defaults to ~/.datomic/data. Pass :mem to use a memory only database.
The returned test environment has the following keys.
:client - The generated Datomic client.
:system - The name of the system, dynamically generated unless ::system was
specified.
:client-map - The map passed to the d/client call.
When closed, the test environment will release the resources used by the client
and delete the data directory."
(^TestEnv [] (test-env {}))
(^TestEnv [env-argm]
(let [env (-new-env-map env-argm)]
(map->TestEnv env))))
(comment
(def e (test-env {:system "test"}))
(.close e)
(d/create-database (:client e) {:db-name "test"})
(d/list-databases (:client e) {})
(def conn (d/connect (:client e) {:db-name "test"}))
(d/transact conn {:tx-data [{:db/ident ::foo
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}]})
)