diff --git a/plpgsql/Dockerfile b/plpgsql/Dockerfile new file mode 100644 index 00000000..d0f42b3b --- /dev/null +++ b/plpgsql/Dockerfile @@ -0,0 +1,45 @@ +FROM postgres:12.1 as base +WORKDIR /app + +ENV PGHOST=localhost +ENV PGDATABASE=kata +ENV PGPASSWORD=admin +ENV PGUSER=postgres +ENV POSTGRES_PASSWORD=admin +ENV PGDATA /var/lib/postgresql/data_local + +RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" + +ADD ./*.sh /app/ +ADD ./src/item.sql /app/src/ +ADD ./src/new_item.sql /app/src/ + +# PGUNIT +FROM base as pgunit + +RUN apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates wget \ + && rm -rf /var/lib/apt/lists/* + +ADD ./pgunit/initialize.sh /app/ +ADD ./pgunit/*.sql /app/ +RUN chmod +x ./*.sh \ + && ./initializeDocker.sh + +# PGTAP +FROM base as pgtap + +RUN apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates build-essential git-core libv8-dev curl postgresql-server-dev-12 \ + && rm -rf /var/lib/apt/lists/* + +RUN mkdir -p /tmp/pgtap \ + && cd /tmp/pgtap \ + && git clone https://github.com/theory/pgtap.git /tmp/pgtap \ + && make \ + && make install \ + && cpan TAP::Parser::SourceHandler::pgTAP + +ADD ./pgtap/initialize.sh /app/ +RUN chmod +x ./*.sh \ + && ./initializeDocker.sh diff --git a/plpgsql/README.md b/plpgsql/README.md index 4ba245c2..93337289 100644 --- a/plpgsql/README.md +++ b/plpgsql/README.md @@ -6,36 +6,58 @@ You'll need: To use remote / local dockerized database, add ``` --host --port --username``` parameters to plsql invocation. # Setup +## With docker +Run `docker-compose up -d ` to start, and `docker-compose exec bash` to enter in container. +`` is the testing framework name. Two values are possible: `pgunit` or `pgtap` + +## Without docker In shell: -- create database: ```createdb gilded_rose``` -- create item table (structure): ```psql -d gilded_rose -f ./structure/create.sql``` -- load code into database: ```psql -d gilded_rose -f ./src/update_quality.sql ``` +- create database: ```createdb kata``` +- create item table (structure): ```psql -d kata -f ./src/item.sql``` +- create procedure to help to add item: ```psql -d kata -f ./src/new_item.sql``` +- load code into database: ```psql -d kata -f ./src/update_quality.sql ``` If you get this message```LINE 1: CREATE OR REPLACE PROCEDURE public.update_quality()```, your PostgreSQL version may under 11, consider upgrading. -# Run +# Run In shell: -- load manual test data into database: ```psql -d gilded_rose -f ./test/manual/load.sql``` -- connect to CLI: ```psql -d gilded_rose``` +- connect to CLI: ```psql -d kata``` +- add item: ```CALL new_item('+5 Dexterity Vest', 10, 20);``` - check item state: ```SELECT * FROM item;``` - execute item update: ```CALL update_quality();``` - check item state: ```SELECT * FROM item;``` - empty table : ```TRUNCATE TABLE item;``` +# Kata +`src/update_quality.sql` contains code to refactor. # Test ## Using pgTAP - ### Requirements -Install pgTAP [instructions here](https://pgtap.org/documentation.html#installation) +Install pgTAP [instructions here](https://pgtap.org/documentation.html#installation) +It's already installed with docker ```item``` table is supposed to be empty. If not, it would cause a (false positive)[https://en.wikipedia.org/wiki/False_positives_and_false_negatives] ### Execute -In shell, execute ```pg_prove --dbname gilded_rose test/pgtap/*.sql```. -You should get ```test/pgtap/template.sql .. ok All tests successful.``` +Run `docker-compose up -d pgtap` to start, and `docker-compose exec pgtap bash` to enter in container. +In shell, execute ```psql -d kata -f src/update_quality.sql && pg_prove pgtap/test_*.sql```. +You should get ```pgtap/test_case_update_quality.sql .. ok All tests successful.``` If you get this message ```ERROR: function plan(integer) does not exist LINE 1: SELECT PLAN(1);```, pgTAP is not working => check your pgTAP installation. If you get this message ```Failed test: 2 Parse errors: Bad plan. You planned 1 tests but ran 2.```, the item table contains data, interfering with the test => empty it, then run test again. + +`pgtap/test_case_update_quality.sql` contains test examples. + +## Using pgunit +### Requirement +Unit test framework used : pgunit (https://github.com/adrianandrei-ca/pgunit) +It's already installed with docker + +### Execute +Run `docker-compose up -d pgunit` to start, and `docker-compose exec pgunit bash` to enter in container. +You can run `cat src/update_quality.sql pgunit/run_tests.sql | psql -d kata -f -` + +`pgunit/run_tests.sql` contains test examples. diff --git a/plpgsql/docker-compose.yml b/plpgsql/docker-compose.yml new file mode 100644 index 00000000..b753b164 --- /dev/null +++ b/plpgsql/docker-compose.yml @@ -0,0 +1,22 @@ +version: '3.4' + +services: + pgunit: + # image: fpellet/gildedrose-refactoring-kata:pgunit + build: + context: . + target: pgunit + ports: + - "5432:5432" + volumes: + - .:/app/:z + + pgtap: + # image: fpellet/gildedrose-refactoring-kata:pgtap + build: + context: . + target: pgtap + ports: + - "5432:5432" + volumes: + - .:/app/:z diff --git a/plpgsql/initializeDatabase.sh b/plpgsql/initializeDatabase.sh new file mode 100644 index 00000000..09b6202a --- /dev/null +++ b/plpgsql/initializeDatabase.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -ex + +echo "Create database" +psql -d postgres -c 'DROP DATABASE IF EXISTS kata;' +psql -d postgres -c 'CREATE DATABASE kata;' + +./initialize.sh + +echo "Add current code" +psql -d kata -f src/item.sql +psql -d kata -f src/new_item.sql diff --git a/plpgsql/pgunit/initializeDocker.sh b/plpgsql/initializeDocker.sh similarity index 100% rename from plpgsql/pgunit/initializeDocker.sh rename to plpgsql/initializeDocker.sh diff --git a/plpgsql/pgtap/initialize.sh b/plpgsql/pgtap/initialize.sh new file mode 100644 index 00000000..7646180d --- /dev/null +++ b/plpgsql/pgtap/initialize.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +set -ex + +echo "Enable extension" +psql -d kata -c 'CREATE EXTENSION IF NOT EXISTS pgtap;' diff --git a/plpgsql/pgtap/test_case_update_quality.sql b/plpgsql/pgtap/test_case_update_quality.sql new file mode 100644 index 00000000..5d06a167 --- /dev/null +++ b/plpgsql/pgtap/test_case_update_quality.sql @@ -0,0 +1,18 @@ +BEGIN; +-- Plan count should match the number of tests. If it does not then pg_prove will fail the test +SELECT plan(1); + +-- Run the tests. +-- Given +TRUNCATE TABLE item; +CALL new_item('foo', 0, 0); + +-- When +CALL update_quality(); + +-- Then +SELECT is( name, 'fixme', 'name did change' ) FROM item; + +-- Finish the tests and clean up. +SELECT * FROM finish(); +ROLLBACK; diff --git a/plpgsql/pgunit/Dockerfile b/plpgsql/pgunit/Dockerfile deleted file mode 100644 index 4c3cc347..00000000 --- a/plpgsql/pgunit/Dockerfile +++ /dev/null @@ -1,20 +0,0 @@ -FROM postgres:12.1 -WORKDIR /app - -ENV PGHOST=localhost -ENV PGDATABASE=kata -ENV PGPASSWORD=admin -ENV PGUSER=postgres -ENV POSTGRES_PASSWORD=admin -ENV PGDATA /var/lib/postgresql/data_local - -RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" - -RUN apt-get update \ - && apt-get install -y --no-install-recommends ca-certificates wget \ - && rm -rf /var/lib/apt/lists/* - -ADD ./*.sh /app/ -ADD ./*.sql /app/ -RUN chmod +x ./*.sh \ - && ./initializeDocker.sh diff --git a/plpgsql/pgunit/docker-compose.yml b/plpgsql/pgunit/docker-compose.yml deleted file mode 100644 index 60183e85..00000000 --- a/plpgsql/pgunit/docker-compose.yml +++ /dev/null @@ -1,16 +0,0 @@ -version: '3' - -services: - database: - build: . - ports: - - "5432:5432" - volumes: - - .:/app/:z - - admin: - image: adminer - links: - - database - ports: - - "8081:8080" diff --git a/plpgsql/pgunit/initializeDatabase.sh b/plpgsql/pgunit/initialize.sh similarity index 59% rename from plpgsql/pgunit/initializeDatabase.sh rename to plpgsql/pgunit/initialize.sh index 12b1d33d..60557019 100644 --- a/plpgsql/pgunit/initializeDatabase.sh +++ b/plpgsql/pgunit/initialize.sh @@ -2,9 +2,7 @@ set -ex -echo "Create database" -psql -d postgres -c 'DROP DATABASE IF EXISTS kata;' -psql -d postgres -c 'CREATE DATABASE kata;' +echo "Enable DBLINK" psql -d kata -c 'CREATE EXTENSION DBLINK;' echo "Initialize test framework" @@ -14,8 +12,3 @@ wget https://raw.githubusercontent.com/adrianandrei-ca/pgunit/bc69dfc526ec3db55f echo "Initialize custom asserts" psql -d kata -f asserts.sql - -echo "Add current code" -psql -d kata -f item.sql -psql -d kata -f new_item.sql -psql -d kata -f update_quality.sql diff --git a/plpgsql/pgunit/readme.md b/plpgsql/pgunit/readme.md deleted file mode 100644 index 6421ed1c..00000000 --- a/plpgsql/pgunit/readme.md +++ /dev/null @@ -1,10 +0,0 @@ -## Requirement -Testing on postgres 12 -Unit test framework used : pgunit (https://github.com/adrianandrei-ca/pgunit) - -## Setup -Run `docker-compose up -d` to start, and `docker-compose exec database bash` to enter in container. -You can run `cat update_quality.sql run_tests.sql | psql -d kata -f -` - -## Kata -`update_quality.sql` contains code to refactor, and `run_tests.sql` contains test examples. diff --git a/plpgsql/pgunit/item.sql b/plpgsql/src/item.sql similarity index 100% rename from plpgsql/pgunit/item.sql rename to plpgsql/src/item.sql diff --git a/plpgsql/pgunit/new_item.sql b/plpgsql/src/new_item.sql similarity index 100% rename from plpgsql/pgunit/new_item.sql rename to plpgsql/src/new_item.sql diff --git a/plpgsql/pgunit/update_quality.sql b/plpgsql/src/update_quality.sql similarity index 100% rename from plpgsql/pgunit/update_quality.sql rename to plpgsql/src/update_quality.sql diff --git a/plpgsql/structure/create.sql b/plpgsql/structure/create.sql deleted file mode 100644 index 263b860d..00000000 --- a/plpgsql/structure/create.sql +++ /dev/null @@ -1,5 +0,0 @@ -CREATE TABLE item ( - name CHARACTER VARYING(100) NOT NULL, - sellIn INTEGER, - quality INTEGER NOT NULL -);