https://supabase.com/ logo
#sql
Title
# sql
c

chipilov

01/03/2022, 6:11 PM
I am trying to use pgTAP using the xUnit-style way of writing tests (i.e. write test functions as stored procedures and then invoke them with SELECT * from runtests('mytestschema'::name)
The tests run, however, the output from the tests is out of order
Here is an example:
Copy code
1..2
    # Subtest: private.test_1()
    ok 1 - Should have no logs
    ok 2 - Should have haha 1
    1..2
ok 1 - private.test_1
    # Subtest: private.test_2()
    ok 1 - Should have no logs
    ok 2 - Should have haha 2
    1..2
ok 2 - private.test_2
I don't see anything in the docs about how to get it in the right order - it just says "The TAP results will be sent to your client." - anyone knows how to fix this?
t

tourdownunder

01/05/2022, 5:55 AM
Without seeing your code I don't quite know what you mean. Though my team now uses this for the more complex tests that we want to use within a plpgsql function in pgtap. Note we need to
return next
for it to work
Copy code
sql
BEGIN;
select *
from plan(2);
CREATE OR REPLACE FUNCTION tests() RETURNS SETOF TEXT AS
$do$
declare
    my_name  text;
BEGIN

    SET client_min_messages TO notice;

    my_name := 'tourdownunder';

    return next is((select 'done'::text),
                   'done', 'check job ran successfully');
    
    return next results_eq($$select 'tourdownunder' $$, 'select ''tourdownunder''', 'test');

end

$do$ language plpgsql;

select *
from tests();

ROLLBACK;
c

chipilov

01/05/2022, 8:09 AM
I have 2 test functions - test_invalid_user_creation_request() and test_valid_user_creation_request(). Here is what the first test looks like, the second one is similar:
Copy code
CREATE OR REPLACE FUNCTION test.test_invalid_user_creation_request()
RETURNS SETOF TEXT
AS $$
BEGIN
  RETURN NEXT
    throws_ok(
      'SELECT public.create_user_profile(''{ "haha": 1 }'')',
      'P0001',
      'Invalid request data for create_user_profile',
      'Profile creation should throw on malformed JSON'
    );
END
$$
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = private, extensions, pg_temp;
I run both tests with the supplied test running from pgTAP like this:
Copy code
SELECT * from runtests('test'::name); -- my test functions are in the test schema
The problem is that the output from this runtests() function is out of order, regardless of what SQL client I use to run the tests.
I get this (notice how the subtest details about test 2 are the first thing in the output)
Copy code
# Subtest: test.test_invalid_user_creation_request()
    ok 1 - Profile creation should throw on malformed JSON
    1..1
ok 1 - test.test_invalid_user_creation_request
    # Subtest: test.test_valid_user_creation_request()
    ok 1 - user profile should contain the newly created user profile
    1..1
ok 2 - test.test_valid_user_creation_request
1..2
When I should get be getting something like this:
Copy code
1..2
ok 1 - test.test_invalid_user_creation_request
    # Subtest: test.test_valid_user_creation_request()
    ok 1 - user profile should contain the newly created user profile
    1..1
ok 2 - test.test_valid_user_creation_request
    # Subtest: test.test_invalid_user_creation_request()
    ok 1 - Profile creation should throw on malformed JSON
    1..1
Since you don't run the tests using the built-in runtests() and instead invoke them manually, your output looks a bit better because each test run is in a separate output window, so the messages for each particular test are no longer split, but they are still out of order