           conn (dm/connection-with-db db)
           now  0xdeadbeef]
       (try
-        (is (not (= nil (<? (dm/<transact! conn test-schema now)))))
-
-        (let [txa (tx (<? (dm/<transact! conn [[:db/add     0 :x 123]] now)))
+        (let [tx0 (tx (<? (dm/<transact! conn test-schema now)))
+              txa (tx (<? (dm/<transact! conn [[:db/add     0 :x 123]] now)))
               txb (tx (<? (dm/<transact! conn [[:db/retract 0 :x 123]] now)))]
-          (is (= (<? (<datoms-after (dm/db conn) txa))
+          (is (= (<? (<datoms-after (dm/db conn) tx0))
                  #{}))
-          (is (= (<? (<transactions-after (dm/db conn) txa))
+          (is (= (<? (<transactions-after (dm/db conn) tx0))
                  [[0 :x 123 txa 1] ;; TODO: true, not 1.
                   [txa :db/txInstant now txa 1]
                   [0 :x 123 txb 0]

          conn (dm/connection-with-db db)
          now  0xdeadbeef]
      (try
        (is (not (= nil (<? (dm/<transact! conn test-schema now)))))

        (let [;; TODO: drop now, allow to set :db/txInstant.
              report (<? (dm/<transact! conn [[:db/add 0 :name "valuex"]] now))
              tx     (tx report)]
          (is (= (<? (<datoms-after (dm/db conn) tx))
                 #{[0 :name "valuex"]}))
          (is (= (<? (<transactions-after (dm/db conn) tx))
                 [[0 :name "valuex" tx 1] ;; TODO: true, not 1.
                  [tx :db/txInstant now tx 1]])))
        (finally
          (<? (dm/close-db db)))))))


        ;; Not having DB-as-value really hurts us here.  This test only works because all upserts
        ;; fail until the final one, so we never need to reset the underlying store.
        (<? (dm/<transact! conn test-schema now))
        (let [tx0 (tx (<? (dm/<transact! conn [{:db/id 101 :name "Ivan" :email "@1"}
                                               {:db/id 102 :name "Petr" :email "@2"}] now)))]

          ;; TODO: improve error message to refer to upsert inputs.
          (testing "upsert conficts with existing id"
            (is (thrown-with-msg? Throwable #"unique constraint"
                                  (<? (dm/<transact! conn [{:db/id 102 :name "Ivan" :age 36}] now)))))

          ;; TODO: improve error message to refer to upsert inputs.
          (testing "upsert conficts with non-existing id"
            (is (thrown-with-msg? Throwable #"unique constraint"
                                  (<? (dm/<transact! conn [{:db/id 103 :name "Ivan" :age 36}] now)))))

          ;; TODO: improve error message to refer to upsert inputs.
          (testing "upsert by 2 conflicting fields"
            (is (thrown-with-msg? Throwable #"Conflicting upsert: #datomish.db.TempId\{:part :db.part/user, :idx -\d+\} resolves both to \d+ and \d+"
                                  (<? (dm/<transact! conn [{:db/id (dm/id-literal :db.part/user -1) :name "Ivan" :email "@2" :age 35}] now)))))

          (testing "upsert by non-existing value resolves as update"
            (let [report (<? (dm/<transact! conn [{:db/id (dm/id-literal :db.part/user -1) :name "Ivan" :email "@3" :age 35}] now))]
              (is (= (<? (<shallow-entity (dm/db conn) 101))
                     {:name "Ivan" :email "@3" :age 35}))
              (is (= (tempids report)
                     {-1 101})))))

        (finally
          (<? (dm/close-db db)))))))
