Skip to content

ComputeSoftware/dev-local-tu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dev-local-tu

Test utility for Datomic dev-local.

dev-local-tu {:mvn/version "0.2.2"}

Rationale

Datomic dev-local provides a way to create clean environments useful for testing. It does not provide a methodology for how to use it. This library provides an opinionated way to use dev-local in your tests.

Usage

The primary function exposed by the api is test-env. This function returns a test environment. A test environment will contain a Datomic client, created by dev local, that can be cleaned up by simply calling .close. A test environment is typically used within a with-open.

(require '[dev-local-tu.core :as dev-local-tu]
         '[datomic.client.api :as d])

(with-open [db-env (dev-local-tu/test-env)]
  (let [_ (d/create-database (:client db-env) {:db-name "test"})
        conn (d/connect (:client db-env) {:db-name "test"})
        _ (d/transact conn {:tx-data [{:db/ident       ::name
                                       :db/valueType   :db.type/string
                                       :db/cardinality :db.cardinality/one}]})
        {:keys [tempids]} (d/transact conn {:tx-data [{:db/id "a"
                                                       ::name "hi"}]})]
    (d/pull (d/db conn)
            [::name]
            (get tempids "a"))))
=> #:example1{:name "hi"}

If you would prefer to manage the lifecycle, use new-env to generate a new, random system and cleanup-env! to remove the associated resources.

Alternatively, you can use test-env with fixtures. See examples/example_fixture.clj for the complete example.

(def ^:dynamic *client* nil)

(defn client-fixture
  [f]
  (with-open [db-env (dev-local-tu/test-env)]
    (binding [*client* (:client db-env)]
      (f))))

(use-fixtures :each client-fixture)

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published