72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import fs from 'fs';
|
|
import { resolve } from 'path';
|
|
|
|
import { GenericContainer, StartedTestContainer } from 'testcontainers';
|
|
import Knex from 'knex';
|
|
import knexTinyLogger from 'knex-tiny-logger';
|
|
|
|
jest.setTimeout(18000_000);
|
|
|
|
const createKnexDb = (mappedPort: string, database: string) => {
|
|
const db = Knex({
|
|
client: 'pg',
|
|
connection: `postgresql://testuser:test@localhost:${mappedPort}/${database}`,
|
|
pool: { min: 0 },
|
|
});
|
|
knexTinyLogger(db);
|
|
return db;
|
|
};
|
|
|
|
const createDB = async (mappedPort): Promise<Knex> => {
|
|
const initialDb = createKnexDb(mappedPort, 'postgres');
|
|
await initialDb.raw('CREATE DATABASE photowalltest');
|
|
await initialDb.destroy();
|
|
|
|
const db = createKnexDb(mappedPort, 'photowalltest');
|
|
const sql = fs.readFileSync(resolve(__dirname, 'schema.sql')).toString();
|
|
await db.raw(sql);
|
|
return db;
|
|
};
|
|
|
|
const openTestContainer = async (): Promise<StartedTestContainer> => {
|
|
const container = await new GenericContainer('postgres')
|
|
.withEnv('POSTGRES_USER', 'testuser')
|
|
.withEnv('POSTGRES_PASSWORD', 'test')
|
|
.withEnv('POSTGRES_DB', 'apidb')
|
|
.withExposedPorts(5432)
|
|
.start();
|
|
|
|
// const stream = await container.logs();
|
|
// stream
|
|
// .on('data', (line) => console.debug(line))
|
|
// .on('err', (line) => console.error(line))
|
|
// .on('end', () => console.debug('Docker Container Stream closed...'));
|
|
|
|
return container;
|
|
};
|
|
|
|
function sleep(ms) {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
}
|
|
|
|
describe('GenericContainer', () => {
|
|
let container: StartedTestContainer;
|
|
let db: Knex;
|
|
|
|
beforeAll(async () => {
|
|
container = await openTestContainer();
|
|
db = await createDB(container.getMappedPort(5432));
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await container.stop();
|
|
await db.destroy();
|
|
});
|
|
|
|
it('works', async () => {
|
|
const res = await db.raw('select now()');
|
|
expect(res.rows[0]['now']).toBeInstanceOf(Date);
|
|
await sleep(2000000);
|
|
});
|
|
});
|