Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read directly from the resource, no intermediate string #21

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))))))