Skip to content

Conversation

@nmokaria27
Copy link
Contributor

@nmokaria27 nmokaria27 commented Oct 26, 2025

Docker Setup and Test Database #49

Closes #49

Overview

This PR implements a comprehensive Docker-based test database setup to enable isolated testing for the volunteer management backend. The implementation provides a complete testing environment that runs independently from the production database, ensuring test isolation and repeatability.

Key Changes:

  • Created Docker container configuration for PostgreSQL test database
  • Implemented complete database schema with 6 enums and 15 tables
  • Added test environment configuration and application integration
  • Created comprehensive testing scripts with 77 automated tests
  • Established repeatable test cycles for CI/CD integration

Testing

Automated Testing:

  • Created verify-test-db-setup.sh with 77 comprehensive tests across 14 phases
  • Tests cover file structure, SQL schema validation, Docker operations, database connectivity, data isolation, and full lifecycle management
  • All tests pass with 100% success rate

Manual Testing:

  • Verified complete test cycle: docker compose up -dnpm run testdocker compose down -v
  • Tested manual reseeding with npm run seed:test
  • Validated data isolation between test runs
  • Confirmed schema recreation after container restart

Test Coverage:

  • File existence and configuration validation
  • SQL schema integrity (6 enums, 15 tables, foreign keys)
  • Docker container lifecycle management
  • Database connectivity and operations
  • Application integration with test environment
  • Port configuration and network accessibility

Screenshots / Screencasts

Run ./verify-test-db-setup.sh --full to test the docker & seed.sql related tests.

Test Script Execution:

$ ./verify-test-db-setup.sh --full
========================================
  FULL DATABASE ROBUSTNESS TEST
  (80+ tests across 14 phases)
========================================

Total Tests Run:  77
Tests Passed:     77
Tests Failed:     0
Success Rate:     100%
✅ All checks passed!

Database Schema Validation:

$ docker compose exec test-db psql -U postgres -d test_db -c "\dt"
                    List of relations
 Schema |            Name             | Type  |  Owner   
--------+----------------------------+-------+----------
 public | chapters                   | table | postgres
 public | companies                  | table | postgres
 public | contacts                   | table | postgres
 public | locations                  | table | postgres
 public | nonprofit_chapter_project | table | postgres
 public | nonprofits                 | table | postgres
 public | operations_branches        | table | postgres
 public | projects                   | table | postgres
 public | roles                      | table | postgres
 public | sponsor_chapter            | table | postgres
 public | sponsors                   | table | postgres
 public | volunteer_assignment       | table | postgres
 public | volunteer_history          | table | postgres
 public | volunteer_role_project     | table | postgres
 public | volunteers                 | table | postgres
(15 rows)

Checklist

  • Code is neat, readable, and works
  • Code is commented where appropriate and well-documented
  • Commit messages follow our guidelines
  • Issue number is linked
  • Branch is linked
  • Reviewers are assigned (one of your tech leads)

Notes

Files Created:

  1. docker-compose.yml - Docker container configuration

    • PostgreSQL 16 Alpine image
    • Port mapping (5433:5432) to avoid conflicts
    • Volume mounting for auto-seeding
    • Health checks and networking
  2. db-init/seed.sql - Complete database schema (258 lines)

    • 6 enum types (action_type, level_type, status_type, volunteer_status, volunteer_type, project_type)
    • 15 tables in correct dependency order
    • All foreign key constraints and unique constraints
    • Idempotent reseeding (DROP/CREATE schema)
  3. .env.test - Test environment configuration

    • DATABASE_URL pointing to localhost:5433
    • NODE_ENV=test
    • Server port configurations
  4. verify-test-db-setup.sh - Comprehensive testing script (393 lines)

    • Quick mode (7 tests, 1 second)
    • Full mode (77 tests, 60 seconds)
    • 14 test phases covering all aspects
    • Color-coded output and detailed reporting

Files Modified:

  1. src/config/server.ts - Application configuration

    • Added conditional loading of .env.test when NODE_ENV=test
    • Enables automatic test database connection
  2. package.json - NPM scripts

    • Added seed:test script for manual database reseeding
    • Command: docker compose exec -T test-db psql -U postgres -d test_db -f /docker-entrypoint-initdb.d/seed.sql

How to Use:

Basic Workflow:

# Start test database
docker compose up -d

# Wait for initialization (5-10 seconds)
sleep 10

# Run application tests
npm run test

# Clean up
docker compose down -v

Verification:

# Quick check (1 second)
./verify-test-db-setup.sh

# Comprehensive testing (60 seconds)
./verify-test-db-setup.sh --full

Manual Reseeding:

# Reseed database without restarting container
npm run seed:test

Complete Test Cycle:

# Full cycle test
docker compose down -v
docker compose up -d
sleep 10
npm run test

Key Benefits:

  • ✅ Complete test isolation from production database
  • ✅ Repeatable test environment setup
  • ✅ Automated schema validation
  • ✅ CI/CD ready with comprehensive testing
  • ✅ Manual reseeding capability
  • ✅ Full lifecycle management
  • ✅ 100% test coverage validation

Technical Details:

  • Database runs on port 5433 to avoid conflicts
  • Uses PostgreSQL 16 Alpine for lightweight container
  • Schema includes all production tables and relationships
  • Idempotent seeding allows multiple runs without errors
  • Health checks ensure database readiness
  • Volume persistence for data between container restarts

This implementation provides a robust foundation for testing that can be easily integrated into CI/CD pipelines and used by all team members for consistent test environments.

nmokaria27 and others added 6 commits October 25, 2025 15:45
- Modified .env.test to include test database configuration and updated server ports.
- Updated .gitignore to ensure .env.test is not ignored.
- Enhanced package.json with additional scripts for building, formatting, linting, and seeding test data.

This commit improves the setup for testing and development workflows.
- Changed permissions of `verify-test-db-setup.sh` to make it executable.
- Modified the SQL command in `verify-test-db-setup.sh` to check for the pgcrypto extension using a more precise output format.
- Updated the insert statement in `verify-test-db-setup.sh` to reflect the correct column names in the contacts table.
- Ensured the pgcrypto extension is created in `seed.sql` for UUID generation.

These changes enhance the accuracy and reliability of the test database setup process.
Signed-off-by: Neel Mokaria <[email protected]>
echo -e " (Runs all 80+ tests including Docker interaction)"
echo -e " 2. Run: ${YELLOW}docker compose up -d${NC}"
echo -e " (Start your test database)"
echo -e " 3. Run: ${YELLOW}npm run test${NC}"
Copy link
Collaborator

@soramicha soramicha Oct 29, 2025

Choose a reason for hiding this comment

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

I know in the issue description I used npm run test, but npm run test would actually run the tests that connect to Supabase, and so currently even with docker container running, the supabase itself is getting changed. Can we create a new test command that would specifically run the tests but make sure it uses the local test database rather than the Supabase database itself? Basically right now, it doesn't seem to be using .env.test information

Copy link
Collaborator

Choose a reason for hiding this comment

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

I deleted all data in current database in supabase so you can easily check if any data is getting created in there when it shouldn't anymore

Copy link
Collaborator

@soramicha soramicha left a comment

Choose a reason for hiding this comment

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

I changed my .env NODE_ENV to test, but when I run npm run test:local, the database in Supabase itself is changed! Also please update the PR itself if there are any changes in commands, instructions, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants