Testing Philosophy
From CLAUDE.md:
“When making changes, make sure to test the services individually before committing using cargo test -p {my_service} from the cloud-storage folder.”
Test Environment Setup
Initial Setup
Before running tests for the first time:Database URL Configuration
Thejust setup_test_envs command creates .env files in all database client crates with:
.env files include:
macro_db_clientcomms_db_clientemail_db_clientcontacts_db_clientnotification_db_clientproperties_db_client- And all services that use these databases
Running Tests
Run All Tests
SQLX_OFFLINE=true when running tests. Tests need a live database connection.
Test Individual Services
Test Specific Modules or Functions
Test File Organization
Pattern: Place tests in a separatetest.rs file within the module directory.
Implementation File (mod.rs or documents.rs)
Test File (test.rs)
“This keeps implementation files focused and makes tests easier to locate and maintain.”
Database Testing Patterns
Test Database Setup
Test Fixtures
Many tests use fixtures for test data:Important: Update Fixtures After Schema Changes
From CLAUDE.md case study:“When changing table structures, update test fixtures accordingly”Example: When renaming
message_mentions to entity_mentions, all fixture references need updating.
Testing After Database Changes
Critical Workflow
From CLAUDE.md:“Always run tests between changes that involve changes to db queries”When you modify database queries:
- Update the query in code
- Run
just prepare_dbto update SQLx cache - Run tests:
cargo test -p <crate_name> - Fix any failures
- Commit changes
After Schema Migrations
Testing Lambda Functions
Lambda functions uselambda_runtime for testing:
Service Integration Tests
Integration tests live inintegration_tests/ directory:
Pre-Commit Checks
Before committing, run:Mock Dependencies
Usemockall for mocking dependencies:
Test Cleanup
If your test database gets corrupted or out of sync:Common Test Patterns
Testing Error Cases
Testing Async Streams
Test Coverage
While not enforced, aim for:- Unit tests for business logic
- Integration tests for API endpoints
- Database tests for all query functions
- Error path testing
Next Steps
- Overview - Understand the architecture
- Services - Learn about service structure
- Database - Database testing specifics
- API Design - API testing patterns