HB_DB_DRIVER=mongo|postgres, versionierte Migrationen beim Start, Mongo-Backend ruft unverändert die Upstream-Modelle. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
63 lines
2.2 KiB
JavaScript
63 lines
2.2 KiB
JavaScript
import { randomBytes } from 'crypto';
|
|
import pg from 'pg';
|
|
import { createPostgresStorage } from './index.js';
|
|
import { runUserRepositoryContract } from '../contract/users.contract.js';
|
|
import { runBrewRepositoryContract, runNotificationRepositoryContract } from '../contract/brews.contract.js';
|
|
|
|
const url = process.env.HB_TEST_POSTGRES_URL;
|
|
const suite = url ? describe : describe.skip;
|
|
if(!url) console.warn('HB_TEST_POSTGRES_URL fehlt: Postgres-Storage-Contract-Tests werden übersprungen.');
|
|
|
|
// Jede Suite bekommt ein frisches Schema test_<random> und räumt danach auf.
|
|
const createStorage = async ()=>{
|
|
const schema = `test_${randomBytes(4).toString('hex')}`;
|
|
const admin = new pg.Client({ connectionString: url });
|
|
await admin.connect();
|
|
await admin.query(`CREATE SCHEMA ${schema}`);
|
|
const storage = createPostgresStorage(undefined, { url, searchPath: schema, poolMax: 3 });
|
|
await storage.connect();
|
|
return {
|
|
storage,
|
|
cleanup : async ()=>{
|
|
await storage.disconnect();
|
|
await admin.query(`DROP SCHEMA ${schema} CASCADE`);
|
|
await admin.end();
|
|
}
|
|
};
|
|
};
|
|
|
|
suite('Postgres UserRepository (Contract)', ()=>{
|
|
runUserRepositoryContract(async ()=>{
|
|
const { storage, cleanup } = await createStorage();
|
|
return { users: storage.users, cleanup };
|
|
});
|
|
});
|
|
|
|
suite('Postgres BrewRepository (Contract)', ()=>{
|
|
runBrewRepositoryContract(async ()=>{
|
|
const { storage, cleanup } = await createStorage();
|
|
return { brews: storage.brews, cleanup };
|
|
});
|
|
});
|
|
|
|
suite('Postgres NotificationRepository (Contract)', ()=>{
|
|
runNotificationRepositoryContract(async ()=>{
|
|
const { storage, cleanup } = await createStorage();
|
|
return { notifications: storage.notifications, cleanup };
|
|
});
|
|
});
|
|
|
|
suite('Postgres Verbindung und Migrationen', ()=>{
|
|
test('Start ohne HB_POSTGRES_URL bricht mit klarer Meldung ab', async ()=>{
|
|
const storage = createPostgresStorage({ get: ()=>undefined });
|
|
await expect(storage.connect()).rejects.toThrow(/HB_POSTGRES_URL/);
|
|
});
|
|
|
|
test('Migrationen sind idempotent, isReady folgt connect/disconnect', async ()=>{
|
|
const { storage, cleanup } = await createStorage();
|
|
expect(storage.isReady()).toBe(true);
|
|
await cleanup();
|
|
expect(storage.isReady()).toBe(false);
|
|
});
|
|
});
|