-
-
Notifications
You must be signed in to change notification settings - Fork 93
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 #216 from yogthos/testcontainers-postgres
Add support for running integration tests using testcontainers - postgres
- Loading branch information
Showing
11 changed files
with
116 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,29 @@ | ||
version: 2.1 | ||
jobs: | ||
build: | ||
docker: | ||
- image: clojure:lein-2.8.0 | ||
working_directory: /root/migratus | ||
parameters: | ||
clojure-tag: | ||
type: string | ||
machine: | ||
image: ubuntu-2204:2022.04.2 | ||
working_directory: /home/circleci/migratus | ||
steps: | ||
- checkout | ||
- restore_cache: | ||
keys: | ||
- v1-dependency-jars-{{ checksum "project.clj" }} | ||
- v1-dependency-jars | ||
- run: lein test | ||
- run: docker run --rm -u root -v $PWD:/root/migratus -v /var/run/docker.sock:/var/run/docker.sock -v $PWD/.m2:/root/.m2 -v $PWD/.lein:/root/.lein -w=/root/migratus cimg/clojure:<< parameters.clojure-tag >> lein test | ||
- save_cache: | ||
key: v1-dependency-jars-{{ checksum "project.clj" }} | ||
paths: | ||
- /root/.m2 | ||
- /root/.lein | ||
|
||
workflows: | ||
version: 2 | ||
build: | ||
jobs: | ||
- build | ||
- build: | ||
matrix: | ||
parameters: | ||
clojure-tag: ["1.10-openjdk-8.0", "1.11-openjdk-8.0", "1.11-openjdk-11.0", "1.11-openjdk-17.0"] |
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
1 change: 1 addition & 0 deletions
1
test/migrations-postgres/20220820030400-create-table-quux.down.sql
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 @@ | ||
DROP TABLE IF EXISTS quux; |
3 changes: 3 additions & 0 deletions
3
test/migrations-postgres/20220820030400-create-table-quux.up.sql
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,3 @@ | ||
CREATE TABLE IF NOT EXISTS quux(id bigint, name varchar(255)); | ||
--;; | ||
CREATE INDEX quux_name on quux(name); |
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,2 @@ | ||
CREATE SCHEMA foo; | ||
CREATE TABLE IF NOT EXISTS foo(id bigint); |
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,65 @@ | ||
(ns migratus.testcontainers.postgres | ||
"Integration tests for postgresql using testcontainers.org" | ||
{:authors ["Eugen Stan"]} | ||
(:require [clj-test-containers.core :as tc] | ||
[clojure.tools.logging :as log] | ||
[clojure.set :as set] | ||
[clojure.test :refer [deftest is testing]] | ||
[migratus.test.migration.sql :as test-sql] | ||
[migratus.core :as migratus] | ||
[next.jdbc :as jdbc] | ||
[next.jdbc.result-set :as rs])) | ||
|
||
(def postgres-image (or (System/getenv "MIGRATUS_TESTCONTAINERS_POSTGRES_IMAGE") | ||
"postgres:14")) | ||
|
||
(def pg-container-spec {:image-name postgres-image | ||
:exposed-ports [5432] | ||
:env-vars {"POSTGRES_PASSWORD" "pw"} | ||
:wait-for {:wait-strategy :port}}) | ||
|
||
|
||
(deftest postgres-migrations-test | ||
|
||
(testing "Migrations are applied succesfully in PostgreSQL." | ||
(let [pg-container (tc/create pg-container-spec) | ||
initialized-pg-container (tc/start! pg-container) | ||
meta->table-names #(into #{} (map :pg_class/table_name %))] | ||
(Thread/sleep 1000) | ||
(let [ds (jdbc/get-datasource {:dbtype "postgresql" | ||
:dbname "postgres" | ||
:user "postgres" | ||
:password "pw" | ||
:host (:host initialized-pg-container) | ||
:port (get (:mapped-ports initialized-pg-container) 5432)}) | ||
config {:store :database | ||
:migration-dir "migrations-postgres" | ||
:init-script "init.sql" | ||
:migration-table-name "foo_bar" | ||
:db {:dbtype "postgresql" | ||
:dbname "postgres" | ||
:user "postgres" | ||
:password "pw" | ||
:host (:host initialized-pg-container) | ||
:port (get (:mapped-ports initialized-pg-container) 5432)}}] | ||
(is (= [] (test-sql/db-tables-and-views ds)) "db is empty before migrations") | ||
|
||
;; init | ||
(migratus/init config) | ||
(let [db-meta (test-sql/db-tables-and-views ds) | ||
table-names (meta->table-names db-meta)] | ||
(is (= #{"foo"} table-names) "db is initialized")) | ||
|
||
;; migrate | ||
(migratus/migrate config) | ||
(let [db-meta (test-sql/db-tables-and-views ds) | ||
table-names (meta->table-names db-meta) | ||
expected-tables #{"quux" "foo" "foo_bar"}] | ||
(log/info "Tables are" table-names) | ||
(is (= (count expected-tables) (count db-meta)) | ||
(str "expected table count is ok.")) | ||
|
||
(is (set/subset? expected-tables table-names) | ||
"contains some tables that we expect"))) | ||
|
||
(tc/stop! initialized-pg-container)))) |
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