--
--
You received this message because you are subscribed to the Google Groups "pgTAP Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pgtap-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pgtap-users/88640c97-b7ef-4363-a315-34449ff71095n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pgtap-users/091b0b35-5bc4-4d97-afbc-c8a1d748dd7fn%40googlegroups.com.
If your specific concern is the availability of specific test data in your database, the way I’ve handled that in the past was to create a small framework that made it fairly easy to create test data that had a named identifier, and then allowed you to easily retrieve that test data later.
Unfortuanetly this was developed internally at a company, so I don’t have code I could share, but the interface looked something like this:
Test_data__create(
Data_name text
, table anyelement
, creation_command
) RETURNS SETOF anyelement
Test_data__get(
Data_name text
, table anyelement
) RETURNS SETOF anyelement
The fun part here is `anyelement`. That pseudotype is used to tell these function what table the test data is in, *and* because they’re polymorphic allow the functions to *return* data from any table in the database. The easiest way to specify the table was to do `NULL::schema_name.table_name`; that creates a NULL that is cast as the table’s composite type. With some catalog trolling, that can be converted into the regclass for the table.
Test_data__create() ran `creation_command` in order to generate test data. That command had to be written in such a way that it did the equivalent of `INSERT INTO table … RETURNING *`. The data returned was stored in an internal table by doing something along the lines of
INSERT INTO test_data VALUES( data_name, regclass of table, array( <results of creation_command casted to table> )::text );
Test_data__get would then return
SELECT unnest( SELECT data::<composite array type of table> FROM test_data WHERE (data_name, table) = (…) )
Note that I had this setup in such a way that test_data__create would not re-create data that already existed. That made it safe to call those functions as many times as needed. That meant that in each of my test scripts, I could simply do a `\i` to include whataver data creation scripts I needed, regardless of the order tests were running in.
From: <pgtap...@googlegroups.com> on behalf of kevin <kbra...@pwhome.com>
Date: Wednesday, January 5, 2022 at 2:29 PM
To: "pgtap...@googlegroups.com" <pgtap...@googlegroups.com>
Subject: RE: [EXTERNAL] [pgtap-users] Re: Associate setup/startup functions with particular tests
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe. |
To view this discussion on the web visit https://groups.google.com/d/msgid/pgtap-users/ac2d6557-5a96-25b1-0e65-fb30ff6bce8a%40pwhome.com.