P5-7141: basic tests (#1)

* added containers for db and apollo

* connected to test db

* added network again

* Added base for tests
This commit is contained in:
Arwid Thornström
2021-06-17 11:04:22 +02:00
committed by GitHub
parent b9b6abbec5
commit 74796e23a8
26 changed files with 22059 additions and 1776 deletions
+129
View File
@@ -0,0 +1,129 @@
import fs from 'fs';
import { resolve } from 'path';
import {
GenericContainer,
Network,
StartedNetwork,
StartedTestContainer,
Wait,
} from 'testcontainers';
import Knex from 'knex';
// import knexTinyLogger from 'knex-tiny-logger';
export const DbNetworkName = 'postgres_container';
export const ApiNetworkName = 'api_container';
const allContainers = {
version: 0,
};
const runFile = async (db, file) => {
const sql = fs.readFileSync(resolve(__dirname, file)).toString();
await db.raw(sql);
};
const createKnexDb = (host: string, mappedPort: number, database: string) => {
const connection = `postgresql://testuser:test@${host}:${mappedPort}/${database}`;
// console.log(`connection`, connection);
const db = Knex({
client: 'pg',
connection: connection,
pool: { min: 0 },
});
// knexTinyLogger(db);
return db;
};
export interface IAllTestContainers {
version: number;
dbContainer?: StartedTestContainer;
db?: Knex;
apiContainer?: StartedTestContainer;
network?: StartedNetwork;
stopAll?: any;
}
export const createAllTestContainers =
async (): Promise<IAllTestContainers> => {
const network = await new Network().start();
// ----------------------------------
// DB container
const dbContainer = await new GenericContainer('postgres')
.withNetworkMode(network.getName())
.withNetworkAliases(DbNetworkName)
.withEnv('POSTGRES_USER', 'testuser')
.withEnv('POSTGRES_PASSWORD', 'test')
.withEnv('POSTGRES_DB', 'apidb')
.withExposedPorts(5432)
.start();
const dbMappedPort = dbContainer.getMappedPort(5432);
const dbHost = dbContainer.getHost();
// ----------------------------------
// Knex DB
const initialDb = createKnexDb(dbHost, dbMappedPort, 'postgres');
await initialDb.raw('CREATE DATABASE photowalltest1');
await initialDb.destroy();
const db = createKnexDb(dbHost, dbMappedPort, 'photowalltest1');
await runFile(db, 'db/schema.sql');
// await db.raw(`INSERT INTO markets (id, name, vat, currency, price_adjustment) VALUES (1, 'SE', 1.25, 'SEK', 1);`);
await runFile(db, 'db/base/locales.sql');
await runFile(db, 'db/base/product-currencies.sql');
await runFile(db, 'db/base/markets.sql');
await runFile(db, 'db/base/market_locales.sql');
// Fields
await runFile(db, 'db/base/order-fields.sql');
await runFile(db, 'db/base/order-row_fields.sql');
await runFile(db, 'db/base/product-fields.sql');
// Designers
await runFile(db, 'db/data/designers.sql');
await runFile(db, 'db/data/designer_texts.sql');
await runFile(db, 'db/data/designers-blocked.sql');
// Set the public as the default schema
await db.raw('set search_path to "public"');
// ----------------------------------
// Api Container
const buildContext = resolve(__dirname, '../../');
const nodeContainer = await GenericContainer.fromDockerfile(
buildContext,
).build();
const dbIpAddress = dbContainer.getIpAddress(network.getName());
const apiContainer = await nodeContainer
.withNetworkMode(network.getName())
.withNetworkAliases(ApiNetworkName)
.withExposedPorts(4000)
.withEnv(
'DATABASE_URL',
`postgresql://testuser:test@${dbIpAddress}:5432/photowalltest1`,
)
.withWaitStrategy(
Wait.forLogMessage('Server is running on http://localhost:4000'),
)
.start();
Object.assign(allContainers, {
version: Math.round(Math.random() * 999999),
dbContainer,
db,
apiContainer,
network,
stopAll: async () => {
await dbContainer.stop();
await apiContainer.stop();
await network.stop();
await db.destroy();
},
});
return allContainers;
};