This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-05-31
Channels
- # announcements (6)
- # babashka (40)
- # beginners (6)
- # calva (1)
- # cider (1)
- # clerk (43)
- # clj-kondo (3)
- # clojure (93)
- # clojure-denver (8)
- # clojure-europe (52)
- # clojure-norway (20)
- # clojure-sweden (7)
- # community-development (5)
- # datascript (15)
- # datomic (30)
- # emacs (24)
- # events (15)
- # fulcro (23)
- # graalvm (12)
- # gratitude (1)
- # helix (4)
- # honeysql (4)
- # hoplon (39)
- # hyperfiddle (7)
- # introduce-yourself (1)
- # jobs (1)
- # jobs-discuss (26)
- # lambdaisland (3)
- # lsp (6)
- # matcher-combinators (2)
- # matrix (5)
- # meander (39)
- # nrepl (4)
- # nyc (1)
- # off-topic (5)
- # portal (73)
- # practicalli (1)
- # re-frame (2)
- # reitit (22)
- # releases (1)
- # remote-jobs (4)
- # shadow-cljs (5)
- # sql (17)
- # testing (1)
- # tools-deps (15)
Hi all, i am looking to implement integration test case for the services, i need to implement db operation testings with the embedded postgres db (inline db) , is there any library available to implement these? Or any example code would be much helpfull thankyou !
The next.jdbc
project uses the embedded PostgreSQL libraries for testing: https://github.com/seancorfield/next-jdbc/blob/develop/test/next/jdbc/test_fixtures.clj
At work, we use MySQL (with next.jdbc
) and we have our own homegrown migration code to create our test database from scratch in CI, but I would highly recommend https://github.com/yogthos/migratus for that, so you don't have to roll your own.
Migrations in QA/production just apply the new changes that are needed, but in dev/test/CI you can start from a fresh database (or no database at all) and run all the migrations to bring things up to the current level.
For tests, we have fixtures (as in clojure.test
fixtures) that setup database content as needed and/or tear it down.
One thing I will say: use the same DB for dev/test/CI that you plan to use for QA/production! Do not use a different DB just because you think it will be easier to setup/teardown.
I'm struggling with the PreparedStatement
API a bit. Does the 3-arity execute-batch
assume that the parameters are provided sequentially, i.e. params like [1 "Ben"] [2 "Bob"]
expands to something like (.setObject ps 0 [1 "Ben"])
usw? My problem is that the data I'm working with has a ton of nullable fields. Is it necessary to massage the data from hash maps that simply don't include the keys when the value is nil into "filled-in" seqs? That's what I've already done but I suspect I'm hugely overcomplicating the process.
You have a sequence of hash maps? Then you can do
(jdbc/execute-batch! ps (map (juxt :the :columns :i :want) my-hash-map-sequence))
If you don't know the keys in advance, you can get them with
(into #{} (comp (map keys) cat) my-hash-map-sequence)
But it is necessary to provide the keys in order, yes? (hence the use of a vector, right?) I think where I'm a little fuzzy is the necessity of explicitly declaring a null row value. What I already did is this
(->> test-data
(apply merge {})
(map (comp
#(apply hash-map %)
#(assoc % 1 nil)))
(apply merge (sorted-map)))
in order to have an intersection of all the possible keys.You could get the column names and use them in the SQL creation...
(I'd probably use HoneySQL to build the SQL here)
You don't need to add nil
entries yourself if you use my approach: the juxt
call will produce nil
for each key that is missing from the hash map.
I see. I'll give that a try. I know when I'm overthinking things (often). Thank you!
user=> (require '[honey.sql :as sql])
nil
user=> (def data [{:a 1} {:b 2 :a 3} {:a 4 :c 4} {:a 2}])
#'user/data
user=> (def cols (into #{} (comp (map keys) cat) data))
#'user/cols
user=> (def sql (take 1 (sql/format {:insert-into :my-table :columns (seq cols) :values [(repeat (count cols) 0)]})))
#'user/sql
user=> sql
("INSERT INTO my_table (c, b, a) VALUES (?, ?, ?)")
user=> (map (apply juxt cols) data)
([nil nil 1] [nil 2 3] [4 nil 4] [nil nil 2])
user=>
☝️:skin-tone-2: something like thatIt's a bit of a futz to generate the correct SQL but then throw away the generated parameters so you get the correct thing for jdbc/prepare
but at least it is all generic.
I wanted to follow up on this: do you have a working solution now?
I had to put it by for the time bc of other work but I'll be back to it on Monday. I was moving in the right direction though.