sql

All things SQL and JDBC...
richiardiandrea 2021-03-17T00:06:31.000800Z

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

seancorfield 2021-03-17T00:09:39.003400Z

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

richiardiandrea 2021-03-17T22:32:16.012200Z

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

seancorfield 2021-03-17T00:10:41.004100Z

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

👍 1
richiardiandrea 2021-03-17T00:11:05.004200Z

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

emccue 2021-03-17T00:12:44.004700Z

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

👍 1
emccue 2021-03-17T00:13:15.005400Z

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

emccue 2021-03-17T00:13:47.005900Z

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

richiardiandrea 2021-03-17T00:13:50.006Z

using clj-test-containers here 😄

emccue 2021-03-17T00:14:30.006200Z

thats new to me - looking at it now

richiardiandrea 2021-03-17T00:15:59.006500Z

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

schmee 2021-03-17T00:30:53.007100Z

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

schmee 2021-03-17T00:31:08.007500Z

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

👍 1
richiardiandrea 2021-03-17T00:32:21.008500Z

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

seancorfield 2021-03-17T00:42:51.009400Z

@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

👍 1
🙏 1
reefersleep 2021-03-24T16:54:29.060800Z

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

seancorfield 2021-03-17T00:43:10.009700Z

https://github.com/zonkyio/embedded-postgres

seancorfield 2021-03-17T00:44:33.010300Z

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

richiardiandrea 2021-03-17T01:46:40.010500Z

@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

richiardiandrea 2021-03-17T01:47:21.010800Z

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

richiardiandrea 2021-03-17T01:52:07.011100Z

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

seancorfield 2021-03-17T02:29:33.011300Z

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.

seancorfield 2021-03-17T02:30:03.011500Z

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

John Conti 2021-03-17T14:44:29.012100Z

Anyone play with the new reactive async driver Postgres stuff?