Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add basic e2e tests #18

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tests/output/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/output/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/output/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/output/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/output/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/output/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/output/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/output/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/output/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/output/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/output/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/output/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/output/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.
133 changes: 133 additions & 0 deletions tests/output/santas_warehouse.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
-- 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 --
--

SET check_function_bodies = false;
-- ddl-end --

-- object: warehouse | type: SCHEMA --
-- DROP SCHEMA IF EXISTS warehouse CASCADE;
CREATE SCHEMA warehouse;
-- ddl-end --
ALTER SCHEMA warehouse OWNER TO postgres;
-- ddl-end --

-- object: factory | type: SCHEMA --
-- DROP SCHEMA IF EXISTS factory CASCADE;
CREATE SCHEMA factory;
-- ddl-end --
ALTER SCHEMA factory OWNER TO postgres;
-- ddl-end --

SET search_path TO pg_catalog,public,warehouse,factory;
-- ddl-end --

-- object: warehouse.seq_storage_locations_id | type: SEQUENCE --
-- DROP SEQUENCE IF EXISTS warehouse.seq_storage_locations_id CASCADE;
CREATE SEQUENCE warehouse.seq_storage_locations_id
INCREMENT BY 1
MINVALUE 0
MAXVALUE 2147483647
START WITH 1
CACHE 1
NO CYCLE
OWNED BY NONE;

-- ddl-end --
ALTER SEQUENCE warehouse.seq_storage_locations_id OWNER TO postgres;
-- ddl-end --

-- object: factory.seq_machines_id | type: SEQUENCE --
-- DROP SEQUENCE IF EXISTS factory.seq_machines_id CASCADE;
CREATE SEQUENCE factory.seq_machines_id
INCREMENT BY 1
MINVALUE 0
MAXVALUE 2147483647
START WITH 1
CACHE 1
NO CYCLE
OWNED BY NONE;

-- ddl-end --
ALTER SEQUENCE factory.seq_machines_id OWNER TO postgres;
-- ddl-end --

-- object: factory.machines | type: TABLE --
-- DROP TABLE IF EXISTS factory.machines CASCADE;
CREATE TABLE factory.machines (
id bigint NOT NULL DEFAULT nextval('factory.seq_machines_id'::regclass),
name text NOT NULL,
toys_produced bigint NOT NULL,
CONSTRAINT machines_pk PRIMARY KEY (id)
);
-- ddl-end --
ALTER TABLE factory.machines OWNER TO postgres;
-- ddl-end --

-- object: warehouse.storage_locations | type: TABLE --
-- DROP TABLE IF EXISTS warehouse.storage_locations CASCADE;
CREATE TABLE warehouse.storage_locations (
id bigint NOT NULL DEFAULT nextval('warehouse.seq_storage_locations_id'::regclass),
shelf bigint NOT NULL,
total_capacity bigint NOT NULL,
used_capacity bigint NOT NULL,
current_toy_type text NOT NULL,
CONSTRAINT storage_locations_pk PRIMARY KEY (id),
CONSTRAINT ck_capacity CHECK (total_capacity >= used_capacity)
);
-- ddl-end --
ALTER TABLE warehouse.storage_locations OWNER TO postgres;
-- ddl-end --

-- object: factory.tr_machines_toys_produced_increase | type: FUNCTION --
-- DROP FUNCTION IF EXISTS factory.tr_machines_toys_produced_increase() CASCADE;
CREATE FUNCTION factory.tr_machines_toys_produced_increase ()
RETURNS trigger
LANGUAGE plpgsql
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
PARALLEL UNSAFE
COST 1
AS $$
BEGIN
IF NEW.toys_produced < OLD.toys_produced THEN
RAISE EXCEPTION 'Toys produced count can not be lowered';
END IF;
END;
$$;
-- ddl-end --
ALTER FUNCTION factory.tr_machines_toys_produced_increase() OWNER TO postgres;
-- ddl-end --

-- object: toys_produced_increase | type: TRIGGER --
-- DROP TRIGGER IF EXISTS toys_produced_increase ON factory.machines CASCADE;
CREATE TRIGGER toys_produced_increase
BEFORE UPDATE OF toys_produced
ON factory.machines
FOR EACH ROW
EXECUTE PROCEDURE factory.tr_machines_toys_produced_increase();
-- ddl-end --


Empty file.
5 changes: 5 additions & 0 deletions tests/output/trek.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
model_name: santas_warehouse
db_name: north_pole
db_users:
- santa
- worker
Loading
Loading