mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2025-12-12 12:22:12 +00:00
Merge pull request #143 from fpellet/plpgsql_pgtap
[plpgsql] Merge pgunit and pgtap
This commit is contained in:
commit
82a0083ead
45
plpgsql/Dockerfile
Normal file
45
plpgsql/Dockerfile
Normal file
@ -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
|
||||||
@ -6,36 +6,58 @@ You'll need:
|
|||||||
To use remote / local dockerized database, add ``` --host --port --username``` parameters to plsql invocation.
|
To use remote / local dockerized database, add ``` --host --port --username``` parameters to plsql invocation.
|
||||||
|
|
||||||
# Setup
|
# Setup
|
||||||
|
## With docker
|
||||||
|
Run `docker-compose up -d <TEST_FRAMEWORK>` to start, and `docker-compose exec <TEST_FRAMEWORK> bash` to enter in container.
|
||||||
|
`<TEST_FRAMEWORK>` is the testing framework name. Two values are possible: `pgunit` or `pgtap`
|
||||||
|
|
||||||
|
## Without docker
|
||||||
In shell:
|
In shell:
|
||||||
- create database: ```createdb gilded_rose```
|
- create database: ```createdb kata```
|
||||||
- create item table (structure): ```psql -d gilded_rose -f ./structure/create.sql```
|
- create item table (structure): ```psql -d kata -f ./src/item.sql```
|
||||||
- load code into database: ```psql -d gilded_rose -f ./src/update_quality.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.
|
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:
|
In shell:
|
||||||
- load manual test data into database: ```psql -d gilded_rose -f ./test/manual/load.sql```
|
- connect to CLI: ```psql -d kata```
|
||||||
- connect to CLI: ```psql -d gilded_rose```
|
- add item: ```CALL new_item('+5 Dexterity Vest', 10, 20);```
|
||||||
- check item state: ```SELECT * FROM item;```
|
- check item state: ```SELECT * FROM item;```
|
||||||
- execute item update: ```CALL update_quality();```
|
- execute item update: ```CALL update_quality();```
|
||||||
- check item state: ```SELECT * FROM item;```
|
- check item state: ```SELECT * FROM item;```
|
||||||
- empty table : ```TRUNCATE TABLE item;```
|
- empty table : ```TRUNCATE TABLE item;```
|
||||||
|
|
||||||
|
# Kata
|
||||||
|
`src/update_quality.sql` contains code to refactor.
|
||||||
|
|
||||||
# Test
|
# Test
|
||||||
|
|
||||||
## Using pgTAP
|
## Using pgTAP
|
||||||
|
|
||||||
### Requirements
|
### 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.
|
```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]
|
If not, it would cause a (false positive)[https://en.wikipedia.org/wiki/False_positives_and_false_negatives]
|
||||||
|
|
||||||
### Execute
|
### Execute
|
||||||
In shell, execute ```pg_prove --dbname gilded_rose test/pgtap/*.sql```.
|
Run `docker-compose up -d pgtap` to start, and `docker-compose exec pgtap bash` to enter in container.
|
||||||
You should get ```test/pgtap/template.sql .. ok All tests successful.```
|
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 ```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.
|
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.
|
||||||
|
|||||||
22
plpgsql/docker-compose.yml
Normal file
22
plpgsql/docker-compose.yml
Normal file
@ -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
|
||||||
13
plpgsql/initializeDatabase.sh
Normal file
13
plpgsql/initializeDatabase.sh
Normal file
@ -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
|
||||||
6
plpgsql/pgtap/initialize.sh
Normal file
6
plpgsql/pgtap/initialize.sh
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -ex
|
||||||
|
|
||||||
|
echo "Enable extension"
|
||||||
|
psql -d kata -c 'CREATE EXTENSION IF NOT EXISTS pgtap;'
|
||||||
18
plpgsql/pgtap/test_case_update_quality.sql
Normal file
18
plpgsql/pgtap/test_case_update_quality.sql
Normal file
@ -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;
|
||||||
@ -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
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
version: '3'
|
|
||||||
|
|
||||||
services:
|
|
||||||
database:
|
|
||||||
build: .
|
|
||||||
ports:
|
|
||||||
- "5432:5432"
|
|
||||||
volumes:
|
|
||||||
- .:/app/:z
|
|
||||||
|
|
||||||
admin:
|
|
||||||
image: adminer
|
|
||||||
links:
|
|
||||||
- database
|
|
||||||
ports:
|
|
||||||
- "8081:8080"
|
|
||||||
@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
set -ex
|
set -ex
|
||||||
|
|
||||||
echo "Create database"
|
echo "Enable DBLINK"
|
||||||
psql -d postgres -c 'DROP DATABASE IF EXISTS kata;'
|
|
||||||
psql -d postgres -c 'CREATE DATABASE kata;'
|
|
||||||
psql -d kata -c 'CREATE EXTENSION DBLINK;'
|
psql -d kata -c 'CREATE EXTENSION DBLINK;'
|
||||||
|
|
||||||
echo "Initialize test framework"
|
echo "Initialize test framework"
|
||||||
@ -14,8 +12,3 @@ wget https://raw.githubusercontent.com/adrianandrei-ca/pgunit/bc69dfc526ec3db55f
|
|||||||
|
|
||||||
echo "Initialize custom asserts"
|
echo "Initialize custom asserts"
|
||||||
psql -d kata -f asserts.sql
|
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
|
|
||||||
@ -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.
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
CREATE TABLE item (
|
|
||||||
name CHARACTER VARYING(100) NOT NULL,
|
|
||||||
sellIn INTEGER,
|
|
||||||
quality INTEGER NOT NULL
|
|
||||||
);
|
|
||||||
Loading…
Reference in New Issue
Block a user