Last active
February 23, 2023 17:31
-
-
Save floscr/4af4fa5cb79d4538d558b0f685ffdf97 to your computer and use it in GitHub Desktop.
Biff Testing submit-tx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns com.test-biff-tx | |
(:require | |
[com.biffweb :as biff] | |
[malli.core :as malc] | |
[malli.registry :as malr] | |
[xtdb.api :as xt])) | |
;; We use this to convert keys to ids, for easy access and without having to change the malli uuid schema | |
(defn string-to-uid [s] | |
(let [bytes (.getBytes (str s))] | |
(java.util.UUID/nameUUIDFromBytes bytes))) | |
(def user-schema | |
{:user/id :uuid | |
:user/email :string | |
:user/joined-at inst? | |
:user [:map {:closed true} | |
[:xt/id :user/id] | |
:user/email | |
:user/joined-at]}) | |
(def malli-opts {:registry (malr/composite-registry malc/default-registry user-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) | |
(update :xt/id string-to-uid))]) | |
(for [[k f] biff/tx-fns] | |
[::xt/put {:xt/id k :xt/fn f}])))))) | |
node)) | |
(defn get-sys [node] | |
{:biff/db (xt/db node) | |
:biff.xtdb/retry false | |
:biff.xtdb/node node | |
:biff/malli-opts #'malli-opts | |
:biff/now #inst "1970"}) | |
(def test-docs [{:xt/id :user/alice | |
:user/email "alice@example.com" | |
:user/joined-at #inst "2000"}]) | |
;; Update the email for :user/alice and check the db result | |
(with-open [node (test-node test-docs)] | |
(let [sys (get-sys node) | |
new-email "test"] | |
(xt/await-tx | |
node | |
(biff/submit-tx sys [{:db/doc-type :user | |
:db/op :update | |
:xt/id (string-to-uid :user/alice) | |
:user/email new-email}])) | |
;; Here's the actual test | |
(= (-> (biff/lookup (:biff/db (get-sys node)) :xt/id (string-to-uid :user/alice)) | |
(:user/email)) | |
new-email))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment