cldwalker / datomico

Use datomic with intention revealing names. Ease of use and sticking to datomic's principles are encouraged.
MIT License
49 stars 7 forks source link

testing #4

Closed boxxxie closed 11 years ago

boxxxie commented 11 years ago

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)

(def user-to-add {
     :name "sharedadmin"
     :email "enginuity@enginuity.com"
     :display_name "En Ginuity"
     :password "somepass"
                  })

(deftest add-and-retrieve-user 
  (ds/start {:schemas user/schema})
  (binding [*connection* (d/connect *uri*)]
    (let [added-user (user/create user-to-add)
          looked-up-user (user/find-first {:name (:name user-to-add)})]               
      (pprint [*uri* added-user looked-up-user])
      (testing "did we actually add a user to the db and can we find them by name?"
        (is (= added-user looked-up-user))))))

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.

boxxxie commented 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.

cldwalker commented 11 years ago

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.

boxxxie commented 11 years ago

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))))
cldwalker commented 11 years ago

Closing as it seems you're all set. I can reopen if you have more questions