Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rod-glover committed Dec 19, 2024
1 parent b5aefa3 commit e84b9dd
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 112 deletions.
2 changes: 2 additions & 0 deletions tests/behavioural/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import pytest

Expand Down Expand Up @@ -113,6 +114,7 @@ def prepared_schema_from_migrations_left(
)
"""
logging.getLogger('alembic').setLevel(logging.CRITICAL)
engine, script = prepare_schema_from_migrations(
uri_left,
alembic_config_left,
Expand Down
138 changes: 138 additions & 0 deletions tests/behavioural/trigger_functions/history_tracking/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import pytest


@pytest.fixture()
def sesh_with_basics(schema_name, sesh_in_prepared_schema_left):
sesh = sesh_in_prepared_schema_left
sesh.execute(f"SET search_path TO {schema_name}, public")
yield sesh_in_prepared_schema_left


@pytest.fixture()
def sesh_with_test_tables(sesh_with_basics):
sesh = sesh_with_basics
sesh.execute(
"""
CREATE TABLE a (
-- Main attributes
a_id SERIAL PRIMARY KEY,
x INTEGER,
-- History-related attributes
mod_time timestamp WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
mod_user character varying(64) COLLATE pg_catalog."default" NOT NULL DEFAULT CURRENT_USER
);
CREATE TRIGGER t100_primary_control_hx_cols
BEFORE INSERT OR DELETE OR UPDATE
ON a
FOR EACH ROW
EXECUTE FUNCTION hxtk_primary_control_hx_cols();
CREATE TRIGGER t100_primary_ops_to_hx
AFTER INSERT OR DELETE OR UPDATE
ON a
FOR EACH ROW
EXECUTE FUNCTION hxtk_primary_ops_to_hx();
CREATE TABLE b (
-- Main attributes
b_id SERIAL PRIMARY KEY,
a_id INTEGER REFERENCES a (a_id),
y INTEGER,
-- History-related attributes
mod_time timestamp WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
mod_user character varying(64) COLLATE pg_catalog."default" NOT NULL DEFAULT CURRENT_USER
);
CREATE TRIGGER t100_primary_control_hx_cols
BEFORE INSERT OR DELETE OR UPDATE
ON b
FOR EACH ROW
EXECUTE FUNCTION hxtk_primary_control_hx_cols();
CREATE TRIGGER t100_primary_ops_to_hx
AFTER INSERT OR DELETE OR UPDATE
ON b
FOR EACH ROW
EXECUTE FUNCTION hxtk_primary_ops_to_hx();
CREATE TABLE c (
-- Main attributes
c_id SERIAL PRIMARY KEY,
a_id INTEGER REFERENCES a (a_id),
b_id INTEGER REFERENCES b (b_id),
z INTEGER,
-- History-related attributes
mod_time timestamp WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
mod_user character varying(64) COLLATE pg_catalog."default" NOT NULL DEFAULT CURRENT_USER
);
CREATE TRIGGER t100_primary_control_hx_cols
BEFORE INSERT OR DELETE OR UPDATE
ON c
FOR EACH ROW
EXECUTE FUNCTION hxtk_primary_control_hx_cols();
CREATE TRIGGER t100_primary_ops_to_hx
AFTER INSERT OR DELETE OR UPDATE
ON c
FOR EACH ROW
EXECUTE FUNCTION hxtk_primary_ops_to_hx();
-- History tables
CREATE TABLE a_hx (
a_id INTEGER,
x INTEGER,
mod_time timestamp WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
mod_user character varying(64) COLLATE pg_catalog."default" NOT NULL DEFAULT CURRENT_USER,
-- History columns
deleted BOOLEAN NOT NULL DEFAULT FALSE,
a_hx_id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY
-- Foreign key columns (none in this table)
);
CREATE TABLE b_hx (
-- Must parallel primary table cols: $1.* (NEW.*/OLD.*)
b_id INTEGER,
a_id INTEGER,
y INTEGER,
mod_time timestamp WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
mod_user character varying(64) COLLATE pg_catalog."default" NOT NULL DEFAULT CURRENT_USER,
-- History columns
deleted BOOLEAN NOT NULL DEFAULT FALSE,
b_hx_id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
-- Foreign key columns.
a_hx_id INTEGER REFERENCES a_hx (a_hx_id) -- inserted by trigger fn
);
CREATE TRIGGER t100_add_foreign_hx_keys
BEFORE INSERT
ON b_hx
FOR EACH ROW
EXECUTE FUNCTION hxtk_add_foreign_hx_keys('{{a, a_id}}');
CREATE TABLE c_hx (
-- Must parallel primary table cols: $1.* (NEW.*/OLD.*)
c_id INTEGER,
a_id INTEGER,
b_id INTEGER,
z INTEGER,
mod_time timestamp WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
mod_user character varying(64) COLLATE pg_catalog."default" NOT NULL DEFAULT CURRENT_USER,
-- History columns
deleted BOOLEAN NOT NULL DEFAULT FALSE,
c_hx_id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
-- Foreign key columns.
a_hx_id INTEGER REFERENCES a_hx (a_hx_id), -- inserted by trigger fn
b_hx_id INTEGER REFERENCES b_hx (b_hx_id) -- inserted by trigger fn
);
CREATE TRIGGER t100_add_foreign_hx_keys
BEFORE INSERT
ON c_hx
FOR EACH ROW
EXECUTE FUNCTION hxtk_add_foreign_hx_keys('{{a, a_id}, {b, b_id}}');
"""
)
yield sesh
Loading

0 comments on commit e84b9dd

Please sign in to comment.