Fork me on GitHub
#biff
<
2023-02-22
>
floscr21:02:00

Is it possible to unit test biff/submit-tx ? I'm trying this

(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))

(defn get-sys [node]
  {:biff/db (xt/db node)
   :biff.xtdb/retry false
   :biff.xtdb/node node
   :biff/now #inst "1970"})

(def test-docs [{:xt/id :user/alice
                 :user/email ""}])

(with-open [node (test-node test-docs)]
  (let [sys (get-sys node)]
    (biff/submit-tx sys [{:db/doc-type :user
                          :db/op :upsert
                          :xt/id :user/alice
                          :user/email "[email protected]"}])))
But I'm getting this error:
=> Execution error (NullPointerException) at com.biffweb.impl.xtdb/biff-op->xt$valid? (xtdb.clj:231).
Cannot invoke "java.util.concurrent.Future.get()" because "fut" is null

2
Jacob O'Bryant22:02:55

yes, this general approach works. It looks like you're just missing the :biff/malli-opts key. See the XT unit tests in the biff repo: https://github.com/jacobobryant/biff/blob/master/test/com/biffweb/impl/xtdb_test.clj#L109. Also note the (poetically named) biff/biff-tx->xt function, which you can use to see the underlying XT transaction that Biff transactions get converted to, https://github.com/jacobobryant/biff/blob/master/test/com/biffweb/impl/xtdb_test.clj#L124. I really need to get add some parameter validation to Biff's functions so you get a better error message than Cannot invoke "java.util.concurrent.Future.get()" because "fut" is null :face_palm:

Jacob O'Bryant22:02:28

(That snippet from the biff repo doesn't set :biff.xtdb/node since none of my tests actually call submit-tx, but assuming you are planning to call submit-tx, you should leave that in as you've done)

floscr11:02:58

Ah, thank you! I've got it working now! I also got tripped up previously by passing the malli registry without #'

👍 2
Jacob O'Bryant16:02:40

great! I should probably make that work whether you pass it a var or just the data