Skip to content

Commit

Permalink
Add com.biffweb/test-xtdb-node
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobobryant committed Apr 16, 2024
1 parent 2665a72 commit 1d92648
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 39 deletions.
18 changes: 18 additions & 0 deletions src/com/biffweb.clj
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,24 @@
[:xtdb.api/fn :biff/ensure-unique {:user/name \"example\"}]])"
bxt/tx-fns)

(defn test-xtdb-node
"Returns an in-memory node that's initialized with the given docs and with biff/tx-fns.
Each element of docs will be given a random UUID for :xt/id, unless you
supply one. The docs are inserted with :xtdb.api/put operations, i.e. they're
inserted verbatim without schema checking or any other biff/submit-tx
features.
Example:
(deftest my-test
(with-open [node (test-xtdb-node [{:user/email \"hello@example.com\"}
{:user/email \"there@example.com\"}])]
(let [db (xt/db node)]
...)))"
[docs]
(bxt/test-node docs))

;;;; Rum

(defn render
Expand Down
14 changes: 14 additions & 0 deletions src/com/biffweb/impl/xtdb.clj
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,17 @@
query
(map :v kvs))))
false)))})

(defn test-node [docs]
(let [node (xt/start-node {})]
(xt/await-tx
node
(xt/submit-tx node
(vec
(concat
(for [d docs]
[::xt/put (merge {:xt/id (random-uuid)}
d)])
(for [[k f] tx-fns]
[::xt/put {:xt/id k :xt/fn f}])))))
node))
63 changes: 24 additions & 39 deletions test/com/biffweb/impl/xtdb_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns com.biffweb.impl.xtdb-test
(:require [clojure.test :refer [deftest is]]
[xtdb.api :as xt]
[com.biffweb :as biff]
[com.biffweb :as biff :refer [test-xtdb-node]]
[malli.core :as malc]
[malli.registry :as malr]))

Expand All @@ -28,23 +28,8 @@

(def malli-opts {:registry (malr/composite-registry malc/default-registry schema)})

(defn test-node [docs]
(let [node (xt/start-node {})]
(when (not-empty docs)
(xt/await-tx
node
(xt/submit-tx node
(vec
(concat
(for [d docs]
[::xt/put (merge {:xt/id (random-uuid)}
d)])
(for [[k f] biff/tx-fns]
[::xt/put {:xt/id k :xt/fn f}]))))))
node))

(deftest ensure-unique
(with-open [node (test-node [{:foo "bar"}])]
(with-open [node (test-xtdb-node [{:foo "bar"}])]
(let [db (xt/db node)]
(is (nil? (xt/with-tx
db
Expand All @@ -58,7 +43,7 @@
[::xt/fn :biff/ensure-unique {:foo "bar"}]]))))))

(deftest tx-upsert
(with-open [node (test-node [{:xt/id :id/foo
(with-open [node (test-xtdb-node [{:xt/id :id/foo
:foo "bar"}])]
(is (= (biff/tx-xform-upsert
{:biff/db (xt/db node)}
Expand Down Expand Up @@ -116,10 +101,10 @@
:user/email "bob@example.com"}])

(deftest tx-default
(with-open [node (test-node (into test-docs
[{:xt/id :user/carol
:user/email "carol@example.com"
:user/foo "x"}]))]
(with-open [node (test-xtdb-node (into test-docs
[{:xt/id :user/carol
:user/email "carol@example.com"
:user/foo "x"}]))]
(is (= (biff/biff-tx->xt
(get-context node)
[{:db/doc-type :user
Expand All @@ -144,7 +129,7 @@
{:user/email "carol@example.com", :user/foo "x", :xt/id :user/carol}])))))

(deftest tx-all
(with-open [node (test-node test-docs)]
(with-open [node (test-xtdb-node test-docs)]
(is (= (biff/biff-tx->xt
(get-context node)
[{:db/doc-type :user
Expand All @@ -168,22 +153,22 @@
{:user/email "bob@example.com", :xt/id :user/bob, :user/bar "baz"}])))))

(deftest lookup
(with-open [node (test-node [{:xt/id :user/alice
:user/email "alice@example.com"
:user/foo "foo"}
{:xt/id :user/bob
:user/email "bob@example.com"
:user/foo "foo"}
{:xt/id :user/carol
:user/email "bob@example.com"}
{:xt/id :msg/a
:msg/user :user/alice
:msg/text "hello"
:msg/sent-at #inst "1970"}
{:xt/id :msg/b
:msg/user :user/alice
:msg/text "there"
:msg/sent-at #inst "1971"}])]
(with-open [node (test-xtdb-node [{:xt/id :user/alice
:user/email "alice@example.com"
:user/foo "foo"}
{:xt/id :user/bob
:user/email "bob@example.com"
:user/foo "foo"}
{:xt/id :user/carol
:user/email "bob@example.com"}
{:xt/id :msg/a
:msg/user :user/alice
:msg/text "hello"
:msg/sent-at #inst "1970"}
{:xt/id :msg/b
:msg/user :user/alice
:msg/text "there"
:msg/sent-at #inst "1971"}])]
(let [db (xt/db node)]
(is (= :user/alice (biff/lookup-id db :user/email "alice@example.com")))
(is (= '(:user/alice :user/bob) (sort (biff/lookup-id-all db :user/foo "foo"))))
Expand Down

0 comments on commit 1d92648

Please sign in to comment.