Fork me on GitHub
#sql
<
2021-03-17
>
richiardiandrea00:03:31

Hi there, is there any way to test/mock a transaction rollback with clojure.java.jdbc? or with Jdbc in general?

seancorfield00:03:39

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

richiardiandrea00:03:05

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

richiardiandrea00:03:21

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

richiardiandrea01:03:21

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

richiardiandrea01:03:07

Oh well there db-set-rollback-only! after all 😄

seancorfield02:03:33

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.

seancorfield02:03:03

You should not be trying to mock database methods in clojure.java.jdbc at all.

richiardiandrea22:03:16

yeah agree, and the above would be akin to throw inside the transaction block

seancorfield00:03:41

(but of course separating business logic from DB updates means you pretty much don’t need to test that “JDBC works” 🙂 )

👍 3
emccue00:03:44

;; Embedded postgres
                    [com.opentable.components/otj-pg-embedded "0.13.3"]

👍 3
emccue00:03:15

@richiardiandrea relatedly, I highly encourage using the a "real" db inside your unit tests

richiardiandrea00:03:50

using clj-test-containers here 😄

emccue00:03:30

thats new to me - looking at it now

richiardiandrea00:03:59

let me know if you need some code for give it a go, I have come up with my own fixtures for duct systems

emccue00:03:47

this is what ive used in my personal projects for postgres if you happen to use that

schmee00:03:53

we used to use that at work but it’s no longer maintained, so it doesn’t support newer PG versions

schmee00:03:08

we switched to the PG module in https://www.testcontainers.org/, it works alright

👍 3
seancorfield00:03:51

@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

👍 3
🙏 3
reefersleep16:03:29

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

seancorfield00:03:33

It has PG 13.1.0 support (I’m a bit behind there with next.jdbc).

John Conti14:03:29

Anyone play with the new reactive async driver Postgres stuff?