Skip to content

test: add database CI workflow and PGTap tests for invite_organization_member#1506

Merged
hoshinotsuyoshi merged 2 commits into
mainfrom
test-db-functions-2
Apr 25, 2025
Merged

test: add database CI workflow and PGTap tests for invite_organization_member#1506
hoshinotsuyoshi merged 2 commits into
mainfrom
test-db-functions-2

Conversation

@hoshinotsuyoshi
Copy link
Copy Markdown
Member

@hoshinotsuyoshi hoshinotsuyoshi commented Apr 25, 2025

Issue

  • resolve:

Why is this change needed?

Adding Database CI Workflow and PGTap Tests.

Purpose of the Changes

This change establishes an automated testing environment for database functions, specifically for the invite_organization_member RPC function. This enhancement strengthens quality assurance for database functions and enables database tests to be run as part of the continuous integration (CI) process.

Intent of the Changes

  1. Enhanced Quality Assurance: Automatically verifying the behavior of database functions enables early detection and correction of bugs.
  2. Regression Testing: Ensures that future changes do not break existing functionality.
  3. Improved Development Process: Simplifies test execution in local environments, increasing developer productivity.
  4. Documentation Role: Test code explicitly demonstrates the expected behavior of functions, aiding in code understanding.

CI and Local Testing Environment Setup

GitHub Actions Workflow

Added .github/workflows/database-ci.yml to automatically run database tests under the following conditions:

  • When PRs are created/updated and database-related files are changed
  • When merge group events occur
  • When manually triggered

This workflow starts a Supabase environment, performs test setup, and then runs PGTap tests.

Test Environment Setup

Created .github/workflows/setup-testing.sql to provide the following features needed for test execution:

  • Installation of the PGTap extension
  • Creation of a test schema
  • Functions for creating test users
  • Functions for retrieving user IDs

Local Testing Script

Added frontend/packages/db/supabase/tests/run-tests.sh to allow developers to easily run tests in their local environment. This script provides the following functionality:

  • Checks if Supabase is running and starts it if necessary
  • Sets up the test environment
  • Executes the tests

Testing Implementation for invite_organization_member Function (01-invite_organization_member.test.sql)

Implemented tests using PGTap in frontend/packages/db/supabase/tests/database/01-invite_organization_member.test.sql. This test file covers the following test cases:

  1. New User Invitation: Verifies that a new member can be invited to an organization

    • Validates that the invitation succeeds and returns the correct response
    • Confirms that the invitation record is correctly created in the database
  2. Duplicate Prevention: Verifies that an error is returned when attempting to invite a user who is already a member of the organization

    • Validates that an error message is returned when attempting to invite an existing member
  3. Re-invitation Functionality: Verifies the behavior when re-inviting a user who already has a pending invitation

    • Validates that the invitation succeeds and returns the correct response
    • Confirms that a new invitation record is not created, but the existing invitation is updated
  4. Case Insensitivity: Verifies that email addresses are processed correctly regardless of case

    • Validates that invitations can be sent correctly even with email addresses containing uppercase letters
  5. Timestamp Update: Verifies that the invited_at timestamp is appropriately updated during re-invitation

    • Validates that the invitation date is updated to the latest value when re-inviting

These tests ensure that the main functionality and edge cases of the invite_organization_member function are properly covered.

Future Outlook

Building on this infrastructure, we plan to gradually add tests for other database functions. This will improve the overall quality and reliability of the database layer.

What would you like reviewers to focus on?

  • Whether the test cases for invite_organization_member cover the expected edge cases and typical usage patterns appropriately
  • If the implementation of .github/workflows/database-ci.yml correctly integrates with GitHub Actions and runs the database tests as intended
  • Whether the setup-testing.sql script safely prepares the test environment without affecting production schema or data
  • Usability and clarity of the local test runner script run-tests.sh, including its robustness in various local setups
  • Suggestions on naming, organization, or documentation that could make the testing setup more maintainable or extensible in the future

Testing Verification

  • CI check only. 🥲
  • running ./frontend/packages/db/supabase/tests/run-tests.sh succeeds.

What was done

🤖 Generated by PR Agent at 94b3a41

  • Introduced automated database CI workflow using GitHub Actions and PGTap.
    • Runs on PRs, merge groups, or manual triggers.
    • Sets up Supabase and test environment automatically.
  • Added comprehensive PGTap tests for invite_organization_member function.
    • Covers new user invitation, duplicate prevention, case-insensitive checks, and timestamp updates.
  • Provided SQL setup for test users and helper functions.
    • Includes user creation and retrieval utilities for tests.
  • Added shell script for easy local test execution.
    • Handles Supabase startup, environment setup, and test execution.

Detailed Changes

Relevant files
Tests
database-ci.yml
Add GitHub Actions workflow for database CI and PGTap tests

.github/workflows/database-ci.yml

  • Introduces a GitHub Actions workflow for database CI.
  • Runs PGTap tests on PRs, merge groups, or manual triggers.
  • Automates Supabase startup and test environment setup.
  • +33/-0   
    01-invite_organization_member.test.sql
    Add PGTap tests for invite_organization_member RPC function

    frontend/packages/db/supabase/tests/database/01-invite_organization_member.test.sql

  • Implements 7 PGTap tests for invite_organization_member.
  • Tests new invitations, duplicate prevention, re-invites, and case
    insensitivity.
  • Sets up test users, organization, and context for realistic scenarios.
  • +119/-0 
    Enhancement
    setup-testing.sql
    Add SQL setup for PGTap and test user utilities                   

    .github/workflows/setup-testing.sql

  • Creates and configures PGTap extension and test schema.
  • Adds helper functions for test user creation and user ID retrieval.
  • Ensures idempotent setup for test users and schema.
  • +84/-0   
    run-tests.sh
    Add shell script for local database test automation           

    frontend/packages/db/supabase/tests/run-tests.sh

  • Adds shell script for local test execution.
  • Automates Supabase startup and test environment setup.
  • Runs database tests via Supabase CLI.
  • +45/-0   

    Additional Notes


    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @hoshinotsuyoshi hoshinotsuyoshi self-assigned this Apr 25, 2025
    @changeset-bot
    Copy link
    Copy Markdown

    changeset-bot Bot commented Apr 25, 2025

    ⚠️ No Changeset found

    Latest commit: 4164166

    Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

    This PR includes no changesets

    When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

    Click here to learn what changesets are, and how to add one.

    Click here if you're a maintainer who wants to add a changeset to this PR

    @vercel
    Copy link
    Copy Markdown

    vercel Bot commented Apr 25, 2025

    The latest updates on your projects. Learn more about Vercel for Git ↗︎

    Name Status Preview Comments Updated (UTC)
    liam-app ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 25, 2025 8:36am
    liam-erd-sample ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 25, 2025 8:36am
    1 Skipped Deployment
    Name Status Preview Comments Updated (UTC)
    liam-docs ⬜️ Ignored (Inspect) Visit Preview Apr 25, 2025 8:36am

    @supabase
    Copy link
    Copy Markdown

    supabase Bot commented Apr 25, 2025

    Updates to Preview Branch (test-db-functions-2) ↗︎

    Deployments Status Updated
    Database Fri, 25 Apr 2025 08:34:45 UTC
    Services Fri, 25 Apr 2025 08:34:45 UTC
    APIs Fri, 25 Apr 2025 08:34:45 UTC

    Tasks are run on every commit but only new migration files are pushed.
    Close and reopen this PR if you want to apply changes from existing seed or migration files.

    Tasks Status Updated
    Configurations Fri, 25 Apr 2025 08:34:45 UTC
    Migrations Fri, 25 Apr 2025 08:34:45 UTC
    Seeding Fri, 25 Apr 2025 08:34:45 UTC
    Edge Functions Fri, 25 Apr 2025 08:34:45 UTC

    View logs for this Workflow Run ↗︎.
    Learn more about Supabase for Git ↗︎.

    …n_member
    
    - Introduced a GitHub Actions workflow `.github/workflows/database-ci.yml` to run PGTap tests
    - Created setup-testing.sql to provision test users and support functions
    - Added initial test file `01-invite_organization_member.test.sql` for the `invite_organization_member` RPC function
    - Included a shell script `run-tests.sh` to streamline local testing
    - Tests cover new user invitation, duplicate prevention, case-insensitive matching, and timestamp updates
    @hoshinotsuyoshi hoshinotsuyoshi changed the title [ci skip] rpc function CI test: add database CI workflow and PGTap tests for invite_organization_member Apr 25, 2025
    @hoshinotsuyoshi hoshinotsuyoshi marked this pull request as ready for review April 25, 2025 07:13
    @hoshinotsuyoshi hoshinotsuyoshi requested a review from a team as a code owner April 25, 2025 07:13
    @hoshinotsuyoshi hoshinotsuyoshi requested review from FunamaYukina, MH4GF, NoritakaIkeda and junkisai and removed request for a team April 25, 2025 07:13
    @qodo-free-for-open-source-projects
    Copy link
    Copy Markdown
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Directory Navigation

    The script uses cd $(pwd) which is redundant as it navigates to the current directory. This could be removed without affecting functionality.

    cd $(pwd)
    while [ ! -d ".github" ] && [ "$(pwd)" != "/" ]; do
    Test Inconsistency

    Test 7 checks case insensitivity but uses a different email case ('NEW@example.com') than what was previously used for the same user ('new@example.com'). This might not accurately test the intended behavior if the function normalizes emails.

    SELECT is(
      (SELECT invite_organization_member('NEW@example.com', '11111111-1111-1111-1111-111111111111')),
      '{"success": true, "error": null}'::jsonb,
      'Should be case insensitive when checking existing invitations'
    );

    @qodo-free-for-open-source-projects
    Copy link
    Copy Markdown
    Contributor

    qodo-free-for-open-source-projects Bot commented Apr 25, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Fix incorrect command check

    The script is checking for pnpm when it should be checking for the Supabase CLI
    as indicated by the comment. This will cause the script to fail with a
    misleading error message.

    frontend/packages/db/supabase/tests/run-tests.sh [5-9]

     # Check if Supabase CLI is installed
    -if ! command -v pnpm &> /dev/null; then
    -    echo "pnpm is not installed. Please install it first."
    +if ! command -v supabase &> /dev/null; then
    +    echo "Supabase CLI is not installed. Please install it first."
         exit 1
     fi
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    __

    Why: The script comment indicates it should check for Supabase CLI, but it's actually checking for pnpm. This mismatch would cause the script to fail with a misleading error message, potentially causing confusion during test setup.

    High
    Fix case insensitivity test

    This test is likely to fail because you've already invited 'new@example.com' in
    Test 1, and now you're testing case insensitivity with 'NEW@example.com'. If the
    function is truly case-insensitive, it should return an error about an existing
    invitation, not success.

    frontend/packages/db/supabase/tests/database/01-invite_organization_member.test.sql [106-111]

     -- Test 7: Case insensitivity test
     SELECT is(
       (SELECT invite_organization_member('NEW@example.com', '11111111-1111-1111-1111-111111111111')),
    -  '{"success": true, "error": null}'::jsonb,
    +  '{"success": false, "error": "an invitation for this email already exists"}'::jsonb,
       'Should be case insensitive when checking existing invitations'
     );
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    __

    Why: The test has a logical error - if the function is truly case-insensitive, inviting 'NEW@example.com' after already inviting 'new@example.com' should return an error about an existing invitation, not success. This correction ensures the test properly validates case insensitivity.

    Medium
    • Update

    Copy link
    Copy Markdown
    Contributor

    @MH4GF MH4GF left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    LGTM, but I wrote a comment!

    Copy link
    Copy Markdown
    Contributor

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    I am uncomfortable with the placement of this file in .github/workflows, do you think it is frontend/packages/db/supabase/tests?

    Copy link
    Copy Markdown
    Member Author

    @hoshinotsuyoshi hoshinotsuyoshi Apr 25, 2025

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    frontend/packages/db/supabase/tests

    the original is https://github.com/usebasejump/basejump/tree/main/.github/workflows , but , I think frontend/packages/db/supabase/tests is more reasonable! 👍

    I'll fix it

    Copy link
    Copy Markdown
    Contributor

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    I see, Thanks!!

    @hoshinotsuyoshi hoshinotsuyoshi added this pull request to the merge queue Apr 25, 2025
    Merged via the queue into main with commit 8e1fb05 Apr 25, 2025
    19 checks passed
    @hoshinotsuyoshi hoshinotsuyoshi deleted the test-db-functions-2 branch April 25, 2025 10:10
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants