Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions __fixtures__/seed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,71 @@ Composable SQL seed layers for integration testing. Each layer builds on the pre

| Layer | Files | What it provides |
|-------|-------|-----------------|
| **services** | `services/setup.sql` | Roles, extensions, stamps, metaschema tables, services tables, settings tables, modules tables, grants |
| **base** | `base/setup.sql` | `uuid-ossp` extension, `stamps` schema + `timestamps()` trigger |
| **services** | `services/setup.sql` | Everything in base + `citext`, metaschema tables, services tables, settings tables, modules tables, grants (self-contained) |
| **services data** | `services/test-data.sql` | Example database (`simple-pets`), 3 schemas, 5 APIs, 2 domains, API→schema linkage, animals metaschema entries |
| **app-schemas** | `app-schemas/simple-pets/schema.sql` | `simple-pets-*` schemas, animals table with constraints/indexes/triggers |
| **app data** | `app-schemas/simple-pets/test-data.sql` | 5 test animals (Buddy, Max, Whiskers, Mittens, Tweety) |

> **Note:** Roles (`administrator`, `authenticated`, `anonymous`) are created upstream by `pgsql-test`'s `createBaseRoles()` — seed SQL should never create roles.

## Usage with pgsql-test

```typescript
import { getConnections } from 'pgsql-test';
import path from 'path';
### Non-services tests (no metaschema)

```typescript
const SEED = path.resolve(__dirname, '../../../__fixtures__/seed');

const { db, teardown } = await getConnections({
seed: seed.sqlfile([
`${SEED}/services/setup.sql`,
`${SEED}/services/test-data.sql`,
`${SEED}/app-schemas/simple-pets/schema.sql`,
`${SEED}/app-schemas/simple-pets/test-data.sql`,
])
});
seed.sqlfile([
`${SEED}/base/setup.sql`, // extensions, stamps
// ... your app-specific schema + data
])
```

### Services-enabled tests (metaschema + domain resolution)

```typescript
seed.sqlfile([
`${SEED}/services/setup.sql`, // metaschema + services DDL
`${SEED}/app-schemas/simple-pets/schema.sql`, // app tables
`${SEED}/services/test-data.sql`, // API + domain rows
`${SEED}/app-schemas/simple-pets/test-data.sql`, // test animals
])
```

## Composition

Pick only the layers you need:

- **Base only** (extensions + stamps, no metaschema): `base/setup.sql` + your own schema/data
- **Metaschema + services only** (no app tables): `services/setup.sql` + `services/test-data.sql`
- **Full stack with app data**: all four files in order
- **Full stack with app data**: `services/setup.sql` + `app-schemas/*` + `services/test-data.sql` + `app-schemas/*/test-data.sql`
- **Custom app schema**: `services/setup.sql` + `services/test-data.sql` + your own schema/data SQL

> **Note:** `services/setup.sql` is self-contained — it includes everything from `base/setup.sql` plus metaschema and services DDL. You do NOT need to load both `base/setup.sql` and `services/setup.sql`.

## Consumers

These test files use the shared fixtures (no local duplicates):
These test files use the shared fixtures:

| Test file | Seed files used |
|-----------|----------------|
| `graphql/server-test/__tests__/server.integration.test.ts` | `services/*` + `app-schemas/simple-pets/*` (services scenarios) |
| `graphql/server-test/__tests__/express-context.integration.test.ts` | `services/*` + `app-schemas/simple-pets/*` |
| Test file | Shared fixtures used |
|-----------|---------------------|
| `graphql/server-test/__tests__/server.integration.test.ts` | `base/*` (simple-seed), `services/*` + `app-schemas/*` (services scenarios) |
| `graphql/server-test/__tests__/express-context.integration.test.ts` | `services/*` + `app-schemas/*` |
| `graphql/server-test/__tests__/upload.integration.test.ts` | `services/setup.sql` (DDL only, storage data is local) |
| `graphql/server-test/__tests__/cli-e2e.test.ts` | `base/*` + `app-schemas/*` (animals), `base/*` (search) |
| `graphql/server-test/__tests__/search.integration.test.ts` | `base/*` |
| `graphql/server-test/__tests__/schema-snapshot.test.ts` | `base/*` |

## Local Fixtures (not shared)

Some test scenarios have unique content that stays in `graphql/server-test/__fixtures__/seed/`:

| Directory | What's unique |
|-----------|--------------|
| `search-seed/` | `extensions.sql` (pg_trgm + pgvector), `schema.sql` (articles table with tsvector/trigram/vector columns), `test-data.sql` |
| `schema-snapshot/` | `schema.sql` (5-table blog schema: users, posts, tags, post_tags, comments), `test-data.sql` |
| `simple-seed-storage/` | `setup.sql` (storage module, JWT functions), `schema.sql` (3-tenant storage schemas), `test-data.sql` |

## Well-Known IDs

Expand Down
38 changes: 38 additions & 0 deletions __fixtures__/seed/base/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- Shared fixture: minimal base setup
--
-- Creates extensions and the stamps schema needed by all test scenarios.
-- Roles (administrator, authenticated, anonymous) are created upstream by
-- pgsql-test's createUserRole() — do NOT recreate them here.
--
-- This is the smallest common denominator — tests that also need
-- metaschema / services should load services/setup.sql instead
-- (which is self-contained and includes everything here).
--
-- Usage:
-- seed.sqlfile([
-- shared('base', 'setup.sql'),
-- // ... then your app-specific schema + data
-- ])

-- =====================================================
-- EXTENSIONS
-- =====================================================

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- =====================================================
-- STAMPS (timestamp triggers)
-- =====================================================

CREATE SCHEMA IF NOT EXISTS stamps;

CREATE OR REPLACE FUNCTION stamps.timestamps()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'INSERT' THEN
NEW.created_at = COALESCE(NEW.created_at, now());
END IF;
NEW.updated_at = now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
22 changes: 2 additions & 20 deletions __fixtures__/seed/services/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
--
-- Creates the minimum tables needed to emulate a production Constructive
-- database with API resolution, domain routing, and RLS module support.
-- Roles (administrator, authenticated, anonymous) are created upstream by
-- pgsql-test's createUserRole() — do NOT recreate them here.
--
-- Usage (via pgsql-test seed adapter):
-- seed.sqlfile([
Expand All @@ -17,26 +19,6 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "citext";

DO $$
BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'administrator') THEN
CREATE ROLE administrator;
END IF;
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'authenticated') THEN
CREATE ROLE authenticated;
END IF;
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'anonymous') THEN
CREATE ROLE anonymous;
END IF;
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'app_user') THEN
CREATE ROLE app_user;
END IF;
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'app_admin') THEN
CREATE ROLE app_admin;
END IF;
END
$$;

CREATE SCHEMA IF NOT EXISTS stamps;

CREATE OR REPLACE FUNCTION stamps.timestamps()
Expand Down
35 changes: 0 additions & 35 deletions graphql/server-test/__fixtures__/seed/schema-snapshot/setup.sql

This file was deleted.

14 changes: 14 additions & 0 deletions graphql/server-test/__fixtures__/seed/search-seed/extensions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Search-specific extensions (loaded after base/setup.sql)
--
-- Adds pg_trgm for fuzzy matching and optionally pgvector for similarity search.

CREATE EXTENSION IF NOT EXISTS "pg_trgm";

-- pgvector may not be available in all environments
DO $$
BEGIN
CREATE EXTENSION IF NOT EXISTS "vector";
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'pgvector extension not available, skipping';
END
$$;
45 changes: 0 additions & 45 deletions graphql/server-test/__fixtures__/seed/search-seed/setup.sql

This file was deleted.

54 changes: 0 additions & 54 deletions graphql/server-test/__fixtures__/seed/simple-seed/schema.sql

This file was deleted.

35 changes: 0 additions & 35 deletions graphql/server-test/__fixtures__/seed/simple-seed/setup.sql

This file was deleted.

11 changes: 0 additions & 11 deletions graphql/server-test/__fixtures__/seed/simple-seed/test-data.sql

This file was deleted.

Loading
Loading