Skip to content

Commit

Permalink
feat: Add basic e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin committed Jan 7, 2024
1 parent 7dd69e6 commit adfc7f9
Show file tree
Hide file tree
Showing 22 changed files with 802 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash
set -euxo pipefail
cd "$(dirname "$0")"

rm -rf santas_warehouse
mkdir -p santas_warehouse

(
cd santas_warehouse

function trek {
go run ../.. "${@}"
}

TREK_VERSION=latest \
TREK_MODEL_NAME=santas_warehouse \
TREK_DATABASE_NAME=north_pole \
TREK_DATABASE_USERS=santa,worker \
trek init

trek check

for file in ../stages/*; do
cp "$file" santas_warehouse.dbm

trek generate "$(basename "$file" | cut -d "-" -f 2 | cut -d "." -f 1)"

trek check
done
)
5 changes: 5 additions & 0 deletions tests/santas_warehouse/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM ghcr.io/printeers/trek:latest
COPY ./trek.yaml /data
COPY ./testdata /data/testdata
COPY ./migrations /data/migrations
COPY ./hooks /data/hooks
34 changes: 34 additions & 0 deletions tests/santas_warehouse/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: "3.7"

services:

postgres:
image: postgres:14
environment:
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
restart: always

migrations:
build:
context: .
environment:
TREK_POSTGRES_HOST: postgres
TREK_POSTGRES_PORT: 5432
TREK_POSTGRES_USER: postgres
TREK_POSTGRES_PASSWORD: postgres
TREK_POSTGRES_DATABASE: postgres
TREK_POSTGRES_SSLMODE: disable
TREK_RESET_DATABASE: "false"
TREK_INSERT_TEST_DATA: "true"
volumes:
- ./:/data
depends_on:
- postgres
restart: on-failure

volumes:
postgres-data:
4 changes: 4 additions & 0 deletions tests/santas_warehouse/hooks/apply-reset-post.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -euxo pipefail

echo "This is apply-reset-post"
4 changes: 4 additions & 0 deletions tests/santas_warehouse/hooks/apply-reset-pre.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -euxo pipefail

echo "This is apply-reset-pre"
5 changes: 5 additions & 0 deletions tests/santas_warehouse/hooks/generate-migration-post.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -euxo pipefail

echo "This is generate-migration-post"
echo "Running on migration file $1"
26 changes: 26 additions & 0 deletions tests/santas_warehouse/migrations/001_init.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Database generated with pgModeler (PostgreSQL Database Modeler).
-- pgModeler version: 1.0.6
-- PostgreSQL version: 16.0
-- Project Site: pgmodeler.io
-- Model Author: ---
-- -- object: santa | type: ROLE --
-- -- DROP ROLE IF EXISTS santa;
-- CREATE ROLE santa WITH ;
-- -- ddl-end --
--
-- -- object: worker | type: ROLE --
-- -- DROP ROLE IF EXISTS worker;
-- CREATE ROLE worker WITH ;
-- -- ddl-end --
--

-- Database creation must be performed outside a multi lined SQL file.
-- These commands were put in this file only as a convenience.
--
-- -- object: north_pole | type: DATABASE --
-- -- DROP DATABASE IF EXISTS north_pole;
-- CREATE DATABASE north_pole;
-- -- ddl-end --
--


2 changes: 2 additions & 0 deletions tests/santas_warehouse/migrations/002_schemas.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
create schema if not exists "factory";
create schema if not exists "warehouse";
10 changes: 10 additions & 0 deletions tests/santas_warehouse/migrations/003_tables.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
create table "factory"."machines" (
"name" text not null,
"toys_produced" bigint not null
);
create table "warehouse"."storage_locations" (
"shelf" bigint not null,
"total_capacity" bigint not null,
"used_capacity" bigint not null,
"current_toy_type" text not null
);
8 changes: 8 additions & 0 deletions tests/santas_warehouse/migrations/004_sequences.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
create sequence "factory"."seq_machines_id";
create sequence "warehouse"."seq_storage_locations_id";
alter table "factory"."machines" add column "id" bigint not null default nextval('factory.seq_machines_id'::regclass);
alter table "warehouse"."storage_locations" add column "id" bigint not null default nextval('warehouse.seq_storage_locations_id'::regclass);
CREATE UNIQUE INDEX machines_pk ON factory.machines USING btree (id);
CREATE UNIQUE INDEX storage_locations_pk ON warehouse.storage_locations USING btree (id);
alter table "factory"."machines" add constraint "machines_pk" PRIMARY KEY using index "machines_pk";
alter table "warehouse"."storage_locations" add constraint "storage_locations_pk" PRIMARY KEY using index "storage_locations_pk";
1 change: 1 addition & 0 deletions tests/santas_warehouse/migrations/005_checks.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table "warehouse"."storage_locations" add constraint "ck_capacity" CHECK ((total_capacity >= used_capacity));
14 changes: 14 additions & 0 deletions tests/santas_warehouse/migrations/006_triggers.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set check_function_bodies = off;
CREATE OR REPLACE FUNCTION factory.tr_machines_toys_produced_increase()
RETURNS trigger
LANGUAGE plpgsql
COST 1
AS $function$
BEGIN
IF NEW.toys_produced < OLD.toys_produced THEN
RAISE EXCEPTION 'Toys produced count can not be lowered';
END IF;
END;
$function$
;
CREATE TRIGGER toys_produced_increase BEFORE UPDATE OF toys_produced ON factory.machines FOR EACH ROW EXECUTE FUNCTION factory.tr_machines_toys_produced_increase();
120 changes: 120 additions & 0 deletions tests/santas_warehouse/santas_warehouse.dbm
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
CAUTION: Do not modify this file unless you know what you are doing.
Unexpected results may occur if the code is changed deliberately.
-->
<dbmodel pgmodeler-ver="1.0.6" use-changelog="false" last-position="0,0" last-zoom="1" max-obj-count="4"
default-owner="postgres"
layers="Default layer"
active-layers="0"
layer-name-colors="#000000"
layer-rect-colors="#893ae4"
show-layer-names="false" show-layer-rects="false">
<role name="santa"
sql-disabled="true">
</role>

<role name="worker"
sql-disabled="true">
</role>

<database name="north_pole" is-template="false" allow-conns="true" sql-disabled="true">
</database>

<schema name="public" layers="0" rect-visible="true" fill-color="#e1e1e1" sql-disabled="true">
<role name="postgres"/>
</schema>

<schema name="warehouse" layers="0" rect-visible="true" fill-color="#e1e1e1">
<role name="postgres"/>
</schema>

<schema name="factory" layers="0" rect-visible="true" fill-color="#e1e1e1">
<role name="postgres"/>
</schema>

<sequence name="seq_storage_locations_id" cycle="false" start="1" increment="1" min-value="0" max-value="2147483647" cache="1">
<schema name="warehouse"/>
<role name="postgres"/>
</sequence>

<sequence name="seq_machines_id" cycle="false" start="1" increment="1" min-value="0" max-value="2147483647" cache="1">
<schema name="factory"/>
<role name="postgres"/>
</sequence>

<table name="machines" layers="0" collapse-mode="2" max-obj-count="3" z-value="0">
<schema name="factory"/>
<role name="postgres"/>
<position x="860" y="520"/>
<column name="id" not-null="true" sequence="factory.seq_machines_id">
<type name="bigint" length="0"/>
</column>
<column name="name" not-null="true">
<type name="text" length="0"/>
</column>
<column name="toys_produced" not-null="true">
<type name="bigint" length="0"/>
</column>
<constraint name="machines_pk" type="pk-constr" table="factory.machines">
<columns names="id" ref-type="src-columns"/>
</constraint>
</table>

<table name="storage_locations" layers="0" collapse-mode="2" max-obj-count="6" z-value="0">
<schema name="warehouse"/>
<role name="postgres"/>
<position x="1200" y="540"/>
<column name="id" not-null="true" sequence="warehouse.seq_storage_locations_id">
<type name="bigint" length="0"/>
</column>
<column name="shelf" not-null="true">
<type name="bigint" length="0"/>
</column>
<column name="total_capacity" not-null="true">
<type name="bigint" length="0"/>
</column>
<column name="used_capacity" not-null="true">
<type name="bigint" length="0"/>
</column>
<column name="current_toy_type" not-null="true">
<type name="text" length="0"/>
</column>
<constraint name="storage_locations_pk" type="pk-constr" table="warehouse.storage_locations">
<columns names="id" ref-type="src-columns"/>
</constraint>
<constraint name="ck_capacity" type="ck-constr" table="warehouse.storage_locations">
<expression> <![CDATA[total_capacity >= used_capacity]]> </expression>
</constraint>
</table>

<function name="tr_machines_toys_produced_increase"
window-func="false"
returns-setof="false"
behavior-type="CALLED ON NULL INPUT"
function-type="VOLATILE"
security-type="SECURITY INVOKER"
parallel-type="PARALLEL UNSAFE"
execution-cost="1"
row-amount="0">
<schema name="factory"/>
<role name="postgres"/>
<language name="plpgsql"/>
<return-type>
<type name="trigger" length="0"/>
</return-type>
<definition> <![CDATA[BEGIN
IF NEW.toys_produced < OLD.toys_produced THEN
RAISE EXCEPTION 'Toys produced count can not be lowered';
END IF;
END;]]> </definition>
</function>

<trigger name="toys_produced_increase" firing-type="BEFORE" per-line="true" constraint="false"
ins-event="false" del-event="false" upd-event="true" trunc-event="false"
table="factory.machines">
<function signature="factory.tr_machines_toys_produced_increase()"/>
<columns names="toys_produced"/>
</trigger>

</dbmodel>
Binary file added tests/santas_warehouse/santas_warehouse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit adfc7f9

Please sign in to comment.