Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

adds pgtap to the docs

Copple448497a457e0
diff --git a/apps/reference/docs/guides/database/extensions/pgtap.mdx b/apps/reference/docs/guides/database/extensions/pgtap.mdx
index ab1104e517d..b80b7171e3c 100644
--- a/apps/reference/docs/guides/database/extensions/pgtap.mdx
+++ b/apps/reference/docs/guides/database/extensions/pgtap.mdx
@@ -33,15 +33,6 @@ Let's cover some basic concepts:
2. Click on **Extensions** in the sidebar.
3. Search for "pgtap" and enable the extension.
-<video width="99%" muted playsInline controls={true}>
- <source
- src="/docs/videos/toggle-extensions.mp4"
- type="video/mp4"
- muted
- playsInline
- />
-</video>
-
</TabItem>
<TabItem value="sql">
@@ -62,37 +53,79 @@ It's good practice to create the extension within a separate schema (like `exten
</Tabs>
-### Managing tests
+### Testing tables
+
+```sql
+begin;
+select plan( 1 );
-It's a good practice to keep all your tests in a separate schema.
+select has_table( 'profiles' );
+
+select * from finish();
+rollback;
+```
+
+API:
+
+- [`has_table()`](https://pgtap.org/documentation.html#has_table): Tests whether or not a table exists in the database
+- [`has_index()`](https://pgtap.org/documentation.html#has_index): Checks for the existence of a named index associated with the named table.
+- [`has_relation()`](https://pgtap.org/documentation.html#has_relation): Tests whether or not a relation exists in the database.
+
+### Testing columns
```sql
-create schema tests;
+begin;
+select plan( 2 );
+
+select has_column( 'profiles', 'id' ); # test that the "id" column exists in the "profiles" table
+select col_is_pk( 'profiles', 'id' ); # test that the "id" column is a primary key
+
+select * from finish();
+rollback;
```
-### Creating a test
+API:
-@TODO
+- [`has_column()`](https://pgtap.org/documentation.html#has_column): Tests whether or not a column exists in a given table, view, materialized view or composite type.
+- [`col_is_pk()`](https://pgtap.org/documentation.html#col_is_pk): Tests whether the specified column or columns in a table is/are the primary key for that table.
-- Create a plan
-- We should come up with a recommendation on how to run the tests. Via a function? External scripts?
-- Eventually this can be done via our CLI
+### Testing RLS Policies
-### Running a test
+```sql
+begin;
+select plan( 1 );
-@TODO
+select policies_are(
+ 'public',
+ 'profiles',
+ ARRAY [
+ 'Profiles are public', # Test that there is a policy called "Profiles are public" on the "profiles" table.
+ 'Profiles can only be updated by the owner' # Test that there is a policy called "Profiles can only be updated by the owner" on the "profiles" table.
+ ]
+);
+
+select * from finish();
+rollback;
+```
-## Examples
+API:
-Let's look at a few different tests which could be helpful in your project.
+- [`policies_are()`](https://pgtap.org/documentation.html#policies_are): Tests that all of the policies on the named table are only the policies that should be on that table.
+- [`policy_roles_are()`](https://pgtap.org/documentation.html#policy_roles_are): Tests whether the roles to which policy applies are only the roles that should be on that policy.
+- [`policy_cmd_is()`](https://pgtap.org/documentation.html#policy_cmd_is): Tests whether the command to which policy applies is same as command that is given in function arguments.
-### Testing tables
+You can also use the `results_eq()` method to test that a Policy returns the correct data:
```sql
begin;
select plan( 1 );
-select has_table( 'profiles' );
+select results_eq(
+ 'select * from profiles()',
+ $$VALUES ( 1, 'Anna'), (2, 'Bruce'), (3, 'Caryn')$$
+ 'profiles() should return all users'
+);
+
select * from finish();
rollback;
@@ -100,16 +133,18 @@ rollback;
API:
-- [`has_table()`](https://pgtap.org/documentation.html#has_table)
+- [`results_eq()`](https://pgtap.org/documentation.html#results_eq)
+- [`results_ne()`](https://pgtap.org/documentation.html#results_ne)
-### Testing columns
+### Testing Functions
```sql
begin;
-select plan( 2 );
+select plan( 1 );
-select has_column( 'profiles', 'id' );
-select col_is_pk( 'profiles', 'id' );
+select function_returns( 'hello_world', 'text' ); # test if the function "hello_world" returns text
+select function_returns( 'is_even', ARRAY['integer'], 'boolean' ); # test if the function "is_even" returns a boolean
+select results_eq('select * from hello_world()', 'hello'); # test if the function "hello_world" returns "hello"
select * from finish();
rollback;
@@ -117,8 +152,8 @@ rollback;
API:
-- [`has_column()`](https://pgtap.org/documentation.html#has_column)
-- [`col_is_pk()`](https://pgtap.org/documentation.html#col_is_pk)
+- [`function_returns()`](https://pgtap.org/documentation.html#function_returns): Tests that a particular function returns a particular data type
+- [`is_definer()`](https://pgtap.org/documentation.html#is_definer): Tests that a function is a security definer (i.e., a “setuid” function).
## Resources
diff --git a/apps/reference/nav/_referenceSidebars.js b/apps/reference/nav/_referenceSidebars.js
index c988dcc7cfe..dff79340fb5 100644
--- a/apps/reference/nav/_referenceSidebars.js
+++ b/apps/reference/nav/_referenceSidebars.js
@@ -135,9 +135,9 @@ const sidebars = {
collapsed: true,
items: [
'guides/database/extensions',
- // 'guides/database/extensions/pgtap',
'guides/database/extensions/plv8',
'guides/database/extensions/http',
+ 'guides/database/extensions/pgtap',
'guides/database/extensions/uuid-ossp',
],
},