Closed boxxxie closed 11 years ago
i wrote a little macro for helping me to test with datomic-simple. i'm going to do a PR on it soon.
I've been testing and using with ring and compojure for months. The testing setup you're having to do is pretty standard. You should also be tearing down the db after each run. You're welcome to use datomic.api directly but it will probably be more verbose. If it's any help, here's a macro I use to wrap a given test:
;;; dsb is datomic.simple/db
(defmacro with-latest-db [& body]
`(binding [datomic-simple.db/*db* (d/db datomic-simple.db/*connection*)]
~@body))
(defmacro with-db
"Evaluates body with var bound to a connection"
[& body]
`(let [~'_ (d/create-database datomic-uri)]
(binding [dsb/*uri* datomic-uri
dsb/*connection* (d/connect datomic-uri)]
(dsb/load-schemas your-schemas-here)
(try
(with-latest-db (do ~@body))
(finally (d/delete-database datomic-uri))))))
I'll be adding with-latest-db to master soon.
thanks a lot. i already implemented with-latest-db. it has come in very handy. i made a test macro that works with peridot as well
(defmacro deftest-datomic [deftest-name schemas & body]
`(clojure.test/deftest ~deftest-name
(start {:schemas ~schemas})
(binding [datomic-simple.db/*connection* (datomic.api/connect datomic-simple.db/*uri*)]
(do ~@body))))
Closing as it seems you're all set. I can reopen if you have more questions
i have been trying really hard to patch datomic-simple so that i could run my tests over or just plain out use it without ring. so far i have failed to be able to use it without ring (i still haven't tried ring, but i suspect that i could run into problems with that too)...
anyway, i was able to get a test working like this (i think this is a bit insane for testing, though)
it would be really nice if i didn't have to use binding or with-redefs or anything like that... i feel like i'm having to too too much low level stuff, and know too much about datomic-simple to get my test working. although i want to use this with the ring middleware, i don't want to have all my tests going through the ring middleware, i want unit tests.
I have a feeling that the dynamic binding of (uri, connection and db) have something to do with my headaches testing.