cli icon indicating copy to clipboard operation
cli copied to clipboard

feat: add db test command using pgtap

Open sweatybridge opened this issue 3 years ago • 1 comments

What kind of change does this PR introduce?

feature: supabase db test

What is the current behavior?

no cli native way to run tests

What is the new behavior?

  • adds pgtap extension to initial schema
  • installs pg_prove on supabase_db_cli container
  • copies supabase/tests into supabase_db_cli container
  • runs pg_prove via docker exec and prints out results

Additional context

Example order_test.pg

BEGIN;
SELECT plan(1);

SELECT has_column(
    'public',
    'order',
    'status',
    'status should exist'
);

SELECT * FROM finish();
ROLLBACK;

Output

$ supabase db test
supabase/tests/order_test.pg .. ok
supabase/tests/pet_test.pg .... ok
All tests successful.
Files=2, Tests=2,  1 wallclock secs ( 0.01 usr  0.00 sys +  0.04 cusr  0.02 csys =  0.07 CPU)
Result: PASS

sweatybridge avatar Aug 03 '22 09:08 sweatybridge

Pull Request Test Coverage Report for Build 3172017203

  • 30 of 78 (38.46%) changed or added relevant lines in 6 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.08%) to 40.536%

Changes Missing Coverage Covered Lines Changed/Added Lines %
cmd/db.go 2 8 25.0%
internal/db/test/test.go 23 65 35.38%
<!-- Total: 30 78
Files with Coverage Reduction New Missed Lines %
cmd/db.go 1 44.94%
<!-- Total: 1
Totals Coverage Status
Change from base Build 3166989322: -0.08%
Covered Lines: 2041
Relevant Lines: 5035

💛 - Coveralls

coveralls avatar Aug 03 '22 10:08 coveralls

this is awesome!! Are tests within nested directories also supported?

psteinroe avatar Sep 30 '22 07:09 psteinroe

I think instead of manually traversing the dirs, we can instead use one file as an entrypoint and use the \i/\include directive to include other files.

Or use the --recurse flag from pg_prove.

soedirgo avatar Sep 30 '22 09:09 soedirgo

I think instead of manually traversing the dirs, we can instead use one file as an entrypoint and use the \i/\include directive to include other files.

Or use the --recurse flag from pg_prove.

+1 for the recurse flag. The \include directive would require the user to manually include hundreds of files eventually.

psteinroe avatar Sep 30 '22 09:09 psteinroe

You can also use wildcards with the include directive iirc, so both approaches should be fine.

(Somehow I can't find where the reference is for the directives, so we should probably put this up somewhere on the CLI docs)

soedirgo avatar Sep 30 '22 10:09 soedirgo

Thanks for the suggestions! Added --recurse flag and undid changes to initial schema.

sweatybridge avatar Sep 30 '22 17:09 sweatybridge

:tada: This PR is included in version 1.6.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Oct 03 '22 06:10 github-actions[bot]

@sweatybridge why do we only search for .pg files? All our tests are .sql. I can fix my nvim config, but still curious since most editors do not recognise .pg files.

EDIT: Created issue https://github.com/supabase/cli/issues/571

psteinroe avatar Oct 31 '22 18:10 psteinroe