In my unit tests, I have a fixture that creates a rollback transaction in order to clean up the database after my tests have run. It looks something like this:
(defn with-rollback [test-fn]
(let [db-conn (db/connect db/test-pg-db)]
(jdbc/with-db-transaction [txn db-conn {:isolation :serializable}]
(jdbc/db-set-rollback-only! txn)
(-> (mount/only [#'db/db])
(mount/swap {#'db/db txn})
(mount/start))
(test-fn)
(mount/stop))
(db/disconnect db-conn)))
Then in the tests I do (use-fixtures :each fixtures/with-rollback).
This works great, except in tests where I want to test that error conditions actually rolls back transactions. For example I want to test that when a request to an external service fails I don't write anything to the db.
Is there any way to get behaviour similar to nested transactions for my use case? I had an idea to override the jdbc/db-transaction* function with something that creates savepoints when a transaction is opened and rolls back to the nearest one when an exception is caught - but I could never get it to work. Appriciate any help!