Files
threshold/internal/db/sqltest/initdb.d/01_wtt_load.sql
T
Timothy Messier 624bdbf0c2 test(db): Add baseline tests for wh_user_dimension (#1454)
This sets up the configuration for running a new suite of tests that are
written in sql, to test the sql logic implemented in plpgsql functions
and triggers. It is primarily designed to provides tests for the data
warehouse functionality that is implemented purely in sql.

The infrastructure to run these tests leverages docker. It creates one
docker container using the `library/postgres` image and provisions the
database with the boundary sql migrations and test helper functions.  A
separate docker container is run that has the pgtap extension and
pg_prove cli to execute the tests.

As noted, pgtap is used for the tests. This postgresql extension
provides a number of helpful assertion functions to make writing sql
tests easier. It also, along with the pg_prove cli, provides readable
test output.

Along with the new testing infrastructure this commit establishes a
baseline of tests for the wh_user_dimension:

- Unit tests for individual views and functions:
    - whx_user_dimension_source
    - whx_user_dimension_target
    - wh_upsert_user
- Integration tests for that verify that the wh_user_dimension gets
  populated correctly when sessions are inserted or updated. This
  ensures that:
    - the trigger runs on session insert
    - the trigger results in the correct changes to the
      wh_user_dimension.

This commit provides a `Makefile` to run the tests locally that provides
support for:

- Setting the postgres docker image to easily test multiple versions.
- Passing through options to pg_prove
- Passing the test list to run individual tests
- Additional targets to separately startup the database image to quickly
  re-run tests while developing new tests.

Lastly configures circleci to run this test suite as a matrix build
in parallel with the other tests. The matrix build tests multiple
versions of postgres:

- latest
- 13-alpine
- 12-alpine
- 11-alpine

See:
    https://pgtap.org/
2021-08-11 13:25:32 -04:00

31 lines
679 B
PL/PgSQL

begin;
-- wtt_load populates tables for the given test persona and set of aggregates.
-- Valid personas are:
-- * colors
-- * widgets
--
-- Valid aggregates:
-- * iam
-- * auth
-- * hosts
-- * targets
--
-- Note that some aggregates depend on data from other aggretates, so the order
-- that they are passed to this function matters.
create function wtt_load(persona text, variadic aggregates text[])
returns void
as $$
declare
agg text;
q text;
begin
foreach agg in array aggregates
loop
q = format('select _wtt_load_%I_%I()', persona, agg);
execute q;
end loop;
end;
$$ language plpgsql;
commit;