Skip to content

Commit

Permalink
update next jdbc adapter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
csummers committed Jul 30, 2023
1 parent a806bdf commit 920332b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ jobs:
run: clj-kondo --lint ./

- name: Test
run: bin/kaocha :core
run: bin/kaocha
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,37 @@
[hugsql.core :as hugsql]
[hugsql.adapter.next-jdbc :as next-jdbc]
[next.jdbc.result-set :as result-set]
[hugsql-adapter-next-jdbc.core_test.sql :as sql]))
[hugsql-adapter-next-jdbc.core-test.sql :as sql]))

(def db {:dbtype "h2:mem" :dbname "test"})

(hugsql/set-adapter! (next-jdbc/hugsql-adapter-next-jdbc))
(deftest adapter-tests
(hugsql/set-adapter! (next-jdbc/hugsql-adapter-next-jdbc))

(def conn {:dbtype "h2:mem" :dbname "test"})

(deftest create-table-test
(testing "Can create a table."
(is (not (nil? (sql/create-colors-table conn))))))

(deftest row-tests
(testing "Can insert a row."
(is (= (sql/insert-color conn {:name "ocher" :r 204 :g 119 :b 34})
(testing "create table"
(is (not (nil? (sql/create-colors-table db)))))
(testing "insert a row"
(is (= (sql/insert-color db {:name "ocher" :r 204 :g 119 :b 34})
[{:next.jdbc/update-count 1}])))
(testing "Can insert another row."
(is (= (sql/insert-color conn {:name "crimson" :r 220 :g 20 :b 60})
(testing "insert another row"
(is (= (sql/insert-color db {:name "crimson" :r 220 :g 20 :b 60})
[{:next.jdbc/update-count 1}])))
(testing ":insert command test"
(is (= (sql/insert-color-alt conn {:name "crimson" :r 220 :g 20 :b 60})
[{:COLORS/ID 3}])))
(testing "Can insert and return affected rows"
(is (= (sql/update-color-affected conn {:r 100})
(is (= (sql/insert-color-alt db {:name "crimson" :r 220 :g 20 :b 60})
[{:id 3}])))
(testing "insert and return affected rows"
(is (= (sql/update-color-affected db {:r 100})
3)))
(testing "Can select a row."
(is (= (:COLORS/ID (sql/select-color-by-id conn {:id 1}))
(testing "select a row"
(is (= (:color/id (sql/select-color-by-id db {:id 1}))
1)))
(testing "Can select multiple rows."
(is (= (count (sql/select-all-colors conn))
(testing "select multiple rows"
(is (= (count (sql/select-all-colors db))
3))))

(deftest custom-builder-fn-test
(hugsql/set-adapter! (next-jdbc/hugsql-adapter-next-jdbc {:builder-fn result-set/as-unqualified-maps}))
(testing "Can select a row w/ custom builder fn"
(is (= (:ID (sql/select-color-by-id conn {:id 1}))
(sql/create-colors-table db)
(testing "select a row w/ custom builder fn"
(is (= (:color/id (sql/select-color-by-id db {:id 1}))
1))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- queries.sql
-- test functions

-- :name create-colors-table
-- :command :execute
-- :result :raw
-- :doc create a test table of colors
create table if not exists colors (
id integer auto_increment primary key,
name varchar(32),
r integer,
g integer,
b integer
);

-- :name insert-color
-- :command :execute
-- :result :raw
-- :doc insert a color into the colors table
insert into colors (name, r, g, b)
values (:name, :r, :g, :b);

-- :name insert-color-alt
-- :command :insert
-- :result :raw
-- :doc insert a color into the colors table returning keys
insert into colors (name, r, g, b)
values (:name, :r, :g, :b);

-- :name update-color-affected :! :n
update colors
set r = :r;

-- :name delete-color-by-id
-- :command :execute
-- :result :raw
-- :doc delete a color by the given `id`
delete from colors where id = :id;

-- :name select-color-by-id :? :1
-- :command :query
-- :doc select a color by id
select id as "color/id" from colors where id = :id limit 1;

-- :name select-all-colors :? :*
-- :command :query
-- :doc select all colors
select id as "color/id" from colors;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns hugsql-adapter-next-jdbc.core-test.sql
(:require [hugsql.core :as hugsql]))

(hugsql/def-db-fns "hugsql_adapter_next_jdbc/queries.sql")
(hugsql/def-db-fns "hugsql_adapter_next_jdbc/core_test/queries.sql")
49 changes: 0 additions & 49 deletions hugsql-adapter-next-jdbc/test/hugsql_adapter_next_jdbc/queries.sql

This file was deleted.

0 comments on commit 920332b

Please sign in to comment.