Contents

Test Factory

Jim C. Nasby <Jim.Nasby@BlueTreble.com> v0.0.1, January 2015: :toc: :toclevels: 3 :numbered: Test factory makes it easy to create and retrieve test data in a database.

Table of Contents

Register two Test Sets ("base", and "scratch") for the customer Test Object.

SELECT tf.register(
        table_name := 'customer'
        , test_sets :=
    array[
                row(
                        'base'
                        ,
$$INSERT INTO customer VALUES
        ( DEFAULT -- customer_id
                , 'email', 'first', 'middle', 'last', 'suffix'
                , 'address', 'city', 'state', 'postal'
        )
        RETURNING *
$$
                        )::tf.test_set
                , row(
                        'scratch'
                        ,
$$INSERT INTO customer VALUES
        ( DEFAULT
                , 'email2', 'first', 'middle', 'last', 'suffix'
                , 'address', 'city', 'state', 'postal'
        )
        RETURNING *
$$
                        )::tf.test_set
        ]
);

Retrieve test customer data (data will be inserted if it doesn’t already exist).

SELECT * FROM tf.get( NULL::customer, 'base' );

Register a customer invoice. Note that this test set uses the already registered customer test data.

SELECT tf.register(
    table_name := 'invoice'
    , test_sets :=
    array[
        row(
            'base'
            ,
$$INSERT INTO invoice VALUES
    ( DEFAULT -- invoice_id
        , (tf.get( NULL::customer, 'base' )).customer_id
        , current_date -- Invoice date
        , current_date + 30 -- Due Date
        , 'PO number'
    )
    RETURNING *
$$
            )::tf.test_set
    ]
);

A Test Table is a table that will contain test data.

Every Test Table has Test Sets associated with it. Test sets are defined as

CREATE TYPE tf.test_set AS (
        set_name                text
        , insert_sql    text
);

set_name is used to subsequently refer to the data created by insert_sql. insert_sql is a command that must return test data rows in the same form as the test object.

Note that insert_sql does not have to be an insert statement. It could be a function, for example. The only requirement is that it returns data in the form of table rows. A function defined as "RETURNS SETOF table_name" would work.

Copyright (c) 2015 Jim C. Nasby.