Hi there, is there any way to test/mock a transaction rollback with clojure.java.jdbc
? or with Jdbc in general?
It depends on what you really mean. If you mean “I want to run a test that should rollback a transaction and verify the operations in the transaction were rolled back” then you should just be able to query the system afterward to verify what you expect. If you mean “I want to force a transaction to rollback so I can verify how the system behaves in that situation” then you can probably mock something called inside the TX and have it throw an exception (which will rollback the TX).
yeah agree, and the above would be akin to throw inside the transaction block
(but of course separating business logic from DB updates means you pretty much don’t need to test that “JDBC works” 🙂 )
thank you right it makes sense - I think I want the latter and try to understand if the code actually catches the right exceptions and so handles retries correctly
;; Embedded postgres
[com.opentable.components/otj-pg-embedded "0.13.3"]
@richiardiandrea relatedly, I highly encourage using the a "real" db inside your unit tests
this is what ive used in my personal projects for postgres if you happen to use that
using clj-test-containers
here 😄
thats new to me - looking at it now
let me know if you need some code for give it a go, I have come up with my own fixtures for duct
systems
we used to use that at work but it’s no longer maintained, so it doesn’t support newer PG versions
we switched to the PG module in https://www.testcontainers.org/, it works alright
ok the latter approach would not work because I would have to either mock with-db-transaction
or the connection...I guess I will have to go for an integration test
@schmee For next.jdbc
I use the Zonky version: https://github.com/seancorfield/next-jdbc/blob/develop/deps.edn#L22-L25 and it seems fairly well-maintained /cc @emccue
lovely. I dig embedded pg, but have an issue during unit tests on jenkins, and the error message is obscure (“closed by admin” or something to that effect. Nice to see a maintained alternative
It has PG 13.1.0 support (I’m a bit behind there with next.jdbc
).
@seancorfield would it be accurate to mimic a rollback by resetting this atom? https://github.com/clojure/java.jdbc/blob/2acbf2736a30634c9c75e6a1853e970ffa6bdbb4/src/main/clojure/clojure/java/jdbc.clj#L752
(if so, it might even be a good idea to add an API for testing, similar to https://www.baeldung.com/spring-test-programmatic-transactions#flag-transaction)
Oh well there db-set-rollback-only!
after all 😄
Pretty much everything you’re doing at this level is “not a good idea”. This is the wrong level to be trying to mock anything. And I’ve said that before.
You should not be trying to mock database methods in clojure.java.jdbc
at all.
Anyone play with the new reactive async driver Postgres stuff?