Skip to content

Commit

Permalink
read directly from the resource, no intermediate string
Browse files Browse the repository at this point in the history
- uses less memory, not requiring the entire resource contents in memory
  before reading
- renamed to more clearly indicate what it does
  - there's nothing schema or norm specific about it
- clojure.edn/read is safer than read-string regarding read-eval
- the test provides an example of a more typical ensure-conforms use
  - from resources rather than Clojure source
  • Loading branch information
scgilardi committed Jun 23, 2015
1 parent 109999f commit 1d71206
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
20 changes: 20 additions & 0 deletions resources/sample4.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{:test4/norm1
{:txes [[{:db/ident :test/attribute1
:db/doc "test attribute 1"
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/id #db/id[:db.part/db]
:db.install/_attribute :db.part/db}
{:db/ident :test/user
:db/doc "test user"
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/id #db/id[:db.part/db]
:db.install/_attribute :db.part/db}
{:db/ident :test/transaction-metadata
:db/doc "annotates the transaction with metadata"
:db/id #db/id[:db.part/user]
:db/fn #db/fn
{:lang :clojure
:params [db metadata]
:code [(assoc metadata :db/id #db/id[:db.part/tx])]}}]]}}
17 changes: 10 additions & 7 deletions src/io/rkn/conformity.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
index-attr index}
tx))}))

(defn load-schema-rsc
"Load an edn schema resource file"
[resource-filename]
(-> resource-filename
io/resource
slurp
read-string))
(defn read-resource
"Reads and returns data from a resource containing edn text. An
optional argument allows specifying opts for clojure.edn/read"
([resource-name]
(read-resource {:readers *data-readers*} resource-name))
([opts resource-name]
(->> (io/resource resource-name)
(io/reader)
(java.io.PushbackReader.)
(clojure.edn/read opts))))

(defn index-attr
"Returns the index-attr corresponding to a conformity-attr"
Expand Down
22 changes: 22 additions & 0 deletions test/io/rkn/conformity_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,25 @@
(is false "ensure-conforms should have thrown an exception")
(catch Exception _
(is true "Blew up like it was supposed to."))))))

(deftest test-loads-norms-from-a-resource
(testing "loads a datomic schema from edn in a resource"
(let [sample-norms-map4 (read-resource "sample4.edn")
norm-name (key (first sample-norms-map4))
tx-count (count (:txes (sample-norms-map4 norm-name)))
conn (fresh-conn)]
(is (ensure-conforms conn sample-norms-map4))
(is (conforms-to? (db conn) norm-name tx-count))
(let [tx-meta {:test/user "bob"}
tx-result @(d/transact conn
[[:test/transaction-metadata tx-meta]
{:db/id (d/tempid :db.part/user)
:test/attribute1 "forty two"}])
rel (d/q '[:find ?user ?val
:where
[_ :test/attribute1 ?val ?tx]
[?tx :test/user ?user]]
(db conn))
[user val] (first rel)]
(is (= "bob" user))
(is (= "forty two" val))))))

0 comments on commit 1d71206

Please sign in to comment.