-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from macielti/migrate-postgresql-to-integrant
Migrate PostgreSQL component to Integrant
- Loading branch information
Showing
7 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE IF NOT EXISTS pessoa | ||
( | ||
apelido TEXT UNIQUE NOT NULL PRIMARY KEY, | ||
nome TEXT NOT NULL, | ||
nascimento DATE NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
(ns common-clj.integrant-components.postgresql | ||
(:require [integrant.core :as ig] | ||
[pg.core :as pg] | ||
[pg.migration.core :as mig] | ||
[pg.pool :as pool] | ||
[schema.core :as s] | ||
[taoensso.timbre :as log]) | ||
(:import (org.testcontainers.containers PostgreSQLContainer))) | ||
|
||
(defmethod ig/init-key ::postgresql | ||
[_ {:keys [components]}] | ||
(log/info :starting ::postgresql) | ||
(let [postgresql-config (-> components :config :postgresql) | ||
pool (pool/pool postgresql-config)] | ||
(mig/migrate-all postgresql-config) | ||
pool)) | ||
|
||
(defmethod ig/halt-key! ::postgresql | ||
[_ pool] | ||
(log/info :stopping ::postgresql) | ||
(pool/close pool)) | ||
|
||
(defmethod ig/init-key ::postgresql-mock | ||
[_ _] | ||
(log/info :starting ::postgresql-mock) | ||
(let [postgresql-container (doto (PostgreSQLContainer. "postgres:16-alpine") .start) | ||
postgresql-config {:host (.getHost postgresql-container) | ||
:port (.getMappedPort postgresql-container PostgreSQLContainer/POSTGRESQL_PORT) | ||
:user (.getUsername postgresql-container) | ||
:password (.getPassword postgresql-container) | ||
:database (.getDatabaseName postgresql-container)} | ||
pool (pool/pool postgresql-config)] | ||
(mig/migrate-all postgresql-config) | ||
pool)) | ||
|
||
(defmethod ig/halt-key! ::postgresql-mock | ||
[_ pool] | ||
(log/info :stopping ::postgresql-mock) | ||
(pool/close pool)) | ||
|
||
(s/defn mocked-postgresql-conn | ||
"Intended to be used for unit testing" | ||
[] | ||
(let [postgresql-container (doto (PostgreSQLContainer. "postgres:16-alpine") | ||
.start) | ||
postgresql-config {:host (.getHost postgresql-container) | ||
:port (.getMappedPort postgresql-container PostgreSQLContainer/POSTGRESQL_PORT) | ||
:user (.getUsername postgresql-container) | ||
:password (.getPassword postgresql-container) | ||
:database (.getDatabaseName postgresql-container)}] | ||
(mig/migrate-all postgresql-config) | ||
(pg/connect postgresql-config))) | ||
|
||
|
26 changes: 26 additions & 0 deletions
26
test/integration/integration/integrant_components/postgresql_test.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
(ns integration.integrant-components.postgresql-test | ||
(:require [clojure.test :refer :all] | ||
[common-clj.integrant-components.postgresql :as postgresql] | ||
[integrant.core :as ig] | ||
[java-time.api :as jt] | ||
[pg.core :as pg] | ||
[pg.pool :as pool] | ||
[schema.test :as s])) | ||
|
||
(def postgresql-mock-config | ||
{::postgresql/postgresql-mock {}}) | ||
|
||
(s/deftest postgresql-mock-config-test | ||
(testing "That we can define endpoints" | ||
(let [system (ig/init postgresql-mock-config) | ||
now (jt/local-date)] | ||
(is (= [{:apelido "brunão" | ||
:nascimento now | ||
:nome "nascimento"}] | ||
(pool/with-connection | ||
[conn (:common-clj.integrant-components.postgresql/postgresql-mock system)] | ||
(pg/execute conn | ||
"INSERT INTO pessoa (apelido, nome, nascimento) VALUES ($1, $2, $3) | ||
returning *" | ||
{:params ["brunão" "nascimento" now]})))) | ||
(ig/halt! system)))) |
18 changes: 18 additions & 0 deletions
18
test/unit/common_clj/integrant_components/postgresql_test.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(ns common-clj.integrant-components.postgresql-test | ||
(:require [clojure.test :refer :all] | ||
[common-clj.integrant-components.postgresql :as postgresql] | ||
[java-time.api :as jt] | ||
[pg.core :as pg] | ||
[schema.test :as s])) | ||
|
||
(s/deftest mocked-postgresql-conn-test | ||
(testing "That we can connect to a mocked postgresql container" | ||
(let [conn (postgresql/mocked-postgresql-conn) | ||
now (jt/local-date)] | ||
(is (= [{:nome "nascimento" | ||
:apelido "brunão" | ||
:nascimento now}] | ||
(pg/execute conn | ||
"INSERT INTO pessoa (apelido, nome, nascimento) VALUES ($1, $2, $3) | ||
returning *" | ||
{:params ["brunão" "nascimento" now]})))))) |