Testing
Testing
Relevant source files
The following files were used as context for generating this wiki page:
This section provides a high-level overview of the testing strategy and infrastructure for the @primebrick/dal-pg package. The test suite is designed to ensure correctness, performance, and type safety across all layers of the DAL, using real PostgreSQL instances to validate behavior.
Test Strategy and Infrastructure
The DAL uses Vitest as its primary test runner docs/ai/dal-testing.md:7-9. The testing philosophy rejects mocks in favor of integration tests against a live database to ensure that SQL generation and type coercion match PostgreSQL's actual behavior docs/ai/dal-testing.md:8-8.
Core Infrastructure Components
The test environment relies on several key utilities and configurations:
- Environment Configuration: Tests require a
DATABASE_URLenvironment variable, typically loaded viadotenvfrom a.envfile docs/ai/dal-testing.md:13-19. - Shared Connection Pool: The
getTestPool()helper manages a singletonpg.Poolinstance for the test process test/helpers/setup.ts:28-38. - Idempotent Schema Setup: The
setupTestSchema()function usesCREATE TABLE IF NOT EXISTSto prepare the database without destructive changes test/helpers/setup.ts:52-152. - Test Isolation: Isolation is maintained via
truncateTestTables(), which executesTRUNCATE ... RESTART IDENTITY CASCADEbefore every test case test/helpers/setup.ts:158-163.
For details on the setup and entity definitions, see Test Infrastructure and Entities.
Testing Architecture Diagram
The following diagram illustrates the relationship between the test runner, the helper utilities, and the database.
Test Execution Flow
Code
Sources: docs/ai/dal-testing.md:5-34, test/helpers/setup.ts:1-46
Test Suite Coverage
The test suite is divided into specialized files targeting specific functionality of the Repository and Dal classes.
Functional Areas
- CRUD and Finders: Validates standard operations like
add,findById, andupdatedocs/ai/dal-testing.md:114-114. - Bulk Operations: Tests high-volume writes using the
TEMP TABLEstrategy and batched inserts docs/ai/dal-testing.md:115-115. - Type Mapping: Verifies the matrix of JS ↔ PG types, including native
bigintandnumericprecision handling docs/ai/dal-testing.md:116-116. - Streaming: Ensures
AsyncIterableresults work correctly for large datasets viapg-query-streamdocs/ai/dal-testing.md:117-117. - Negative Paths: Confirms that the system throws appropriate errors like
NotFoundErrororValidationErrorinstead of failing silently docs/ai/dal-testing.md:118-118. - Performance: Benchmarks operations at scales of 100 to 1M rows docs/ai/dal-testing.md:119-119.
For a detailed breakdown of specific test files and their coverage, see Test Suite Coverage.
Code-to-Test Mapping
The following diagram maps core DAL classes to their corresponding test suites.
DAL Entity to Test Suite Mapping
Code
Sources: docs/ai/dal-testing.md:112-120, test/helpers/setup.ts:52-152
Test Entities
The infrastructure uses four primary entities to cover different database scenarios:
| Entity Name | Code Identifier | Purpose |
|---|---|---|
| Simple Test | SimpleTestEntity | Basic CRUD and audit field validation docs/ai/dal-testing.md:37-46. |
| Type Matrix | TypeTestEntity | Comprehensive PG type mapping (JSONB, Timestamps, etc.) docs/ai/dal-testing.md:47-61. |
| Simple Bench | BenchSimpleEntity | High-speed bulk operation benchmarks test/helpers/setup.ts:102-117. |
| Primitive Bench | BenchPrimitivesEntity | Wide-row performance testing with 20+ columns test/helpers/setup.ts:121-151. |
For the full property definitions and mapping matrix, see Test Infrastructure and Entities.
Sources: docs/ai/dal-testing.md:35-61, test/helpers/setup.ts:52-152