Skip to content

Commit

Permalink
feat(testing): Add support for e2e tests backed by Postgres
Browse files Browse the repository at this point in the history
Relates to #207
  • Loading branch information
michaelbromley committed Jan 28, 2020
1 parent dbc591f commit 50bdbd8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
},
"devDependencies": {
"@types/mysql": "^2.15.8",
"@types/pg": "^7.14.1",
"@vendure/core": "^0.7.0",
"mysql": "^2.17.1",
"pg": "^7.17.1",
"rimraf": "^3.0.0",
"typescript": "^3.6.4"
}
Expand Down
48 changes: 41 additions & 7 deletions packages/testing/src/test-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { NestFactory } from '@nestjs/core';
import { DefaultLogger, Logger, VendureConfig } from '@vendure/core';
import { preBootstrapConfig } from '@vendure/core/dist/bootstrap';
import fs from 'fs';
import { Connection } from 'mysql';
import path from 'path';
import { MysqlConnectionOptions } from 'typeorm/driver/mysql/MysqlConnectionOptions';
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
import { SqljsConnectionOptions } from 'typeorm/driver/sqljs/SqljsConnectionOptions';

import { populateForTesting } from './data-population/populate-for-testing';
Expand Down Expand Up @@ -34,15 +34,16 @@ export class TestServer {
*/
async init(options: TestServerOptions): Promise<void> {
const { type } = this.vendureConfig.dbConnectionOptions;
const { dbConnectionOptions } = this.vendureConfig;
switch (type) {
case 'sqljs':
await this.initSqljs(options);
break;
case 'mysql':
await this.initMysql(
this.vendureConfig.dbConnectionOptions as MysqlConnectionOptions,
options,
);
await this.initMysql(dbConnectionOptions as MysqlConnectionOptions, options);
break;
case 'postgres':
await this.initPostgres(dbConnectionOptions as PostgresConnectionOptions, options);
break;
default:
throw new Error(`The TestServer does not support the database type "${type}"`);
Expand Down Expand Up @@ -89,8 +90,8 @@ export class TestServer {

private async initMysql(connectionOptions: MysqlConnectionOptions, options: TestServerOptions) {
const filename = this.getCallerFilename(2);
const dbName = this.getDbNameFromFilename(filename);
const conn = await this.getMysqlConnection(connectionOptions);
const dbName = 'e2e_' + path.basename(filename).replace(/[^a-z0-9_]/gi, '_');
(connectionOptions as any).database = dbName;
(connectionOptions as any).synchronize = true;
await new Promise((resolve, reject) => {
Expand All @@ -117,7 +118,21 @@ export class TestServer {
conn.destroy();
}

private async getMysqlConnection(connectionOptions: MysqlConnectionOptions): Promise<Connection> {
private async initPostgres(connectionOptions: PostgresConnectionOptions, options: TestServerOptions) {
const filename = this.getCallerFilename(2);
const dbName = this.getDbNameFromFilename(filename);
(connectionOptions as any).database = dbName;
(connectionOptions as any).synchronize = true;
const client = await this.getPostgresConnection(connectionOptions);
await client.query(`DROP DATABASE IF EXISTS ${dbName}`);
await client.query(`CREATE DATABASE ${dbName}`);
await this.populateInitialData(this.vendureConfig, options);
await client.end();
}

private async getMysqlConnection(
connectionOptions: MysqlConnectionOptions,
): Promise<import('mysql').Connection> {
const { createConnection } = await import('mysql');
const conn = createConnection({
host: connectionOptions.host,
Expand All @@ -137,6 +152,21 @@ export class TestServer {
return conn;
}

private async getPostgresConnection(
connectionOptions: PostgresConnectionOptions,
): Promise<import('pg').Client> {
const { Client } = require('pg');
const client = new Client({
host: connectionOptions.host,
port: connectionOptions.port,
user: connectionOptions.username,
password: connectionOptions.password,
database: 'postgres',
});
await client.connect();
return client;
}

private getDbFilePath(dataDir: string) {
// tslint:disable-next-line:no-non-null-assertion
const testFilePath = this.getCallerFilename(3);
Expand All @@ -145,6 +175,10 @@ export class TestServer {
return dbFilePath;
}

private getDbNameFromFilename(filename: string): string {
return 'e2e_' + path.basename(filename).replace(/[^a-z0-9_]/gi, '_');
}

private getCallerFilename(depth: number): string {
let pst: ErrorConstructor['prepareStackTrace'];
let stack: any;
Expand Down
37 changes: 37 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,19 @@
resolved "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==

"@types/pg-types@*":
version "1.11.5"
resolved "https://registry.npmjs.org/@types/pg-types/-/pg-types-1.11.5.tgz#1eebbe62b6772fcc75c18957a90f933d155e005b"
integrity sha512-L8ogeT6vDzT1vxlW3KITTCt+BVXXVkLXfZ/XNm6UqbcJgxf+KPO7yjWx7dQQE8RW07KopL10x2gNMs41+IkMGQ==

"@types/pg@^7.14.1":
version "7.14.1"
resolved "https://registry.npmjs.org/@types/pg/-/pg-7.14.1.tgz#40358b57c34970f750f6a26e2a5463c9f4758136"
integrity sha512-gQgg4bLuykokypx4O1fwEzl5e6UjjyaBtN3znn5zhm0YB9BnKyHDw+e4cQY9rAPzpdM2qpJbn9TNzUazbmTsdw==
dependencies:
"@types/node" "*"
"@types/pg-types" "*"

"@types/[email protected]":
version "1.16.4"
resolved "https://registry.npmjs.org/@types/prettier/-/prettier-1.16.4.tgz#5e5e97702cb68498aaba7349b941648daaf2385c"
Expand Down Expand Up @@ -12130,11 +12143,21 @@ [email protected]:
resolved "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz#943bd463bf5b71b4170115f80f8efc9a0c0eb78c"
integrity sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==

pg-packet-stream@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/pg-packet-stream/-/pg-packet-stream-1.1.0.tgz#e45c3ae678b901a2873af1e17b92d787962ef914"
integrity sha512-kRBH0tDIW/8lfnnOyTwKD23ygJ/kexQVXZs7gEyBljw4FYqimZFxnMMx50ndZ8In77QgfGuItS5LLclC2TtjYg==

pg-pool@^2.0.4:
version "2.0.7"
resolved "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.7.tgz#f14ecab83507941062c313df23f6adcd9fd0ce54"
integrity sha512-UiJyO5B9zZpu32GSlP0tXy8J2NsJ9EFGFfz5v6PSbdz/1hBLX1rNiiy5+mAm5iJJYwfCv4A0EBcQLGWwjbpzZw==

pg-pool@^2.0.9:
version "2.0.9"
resolved "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.9.tgz#7ed69a27e204f99e9804a851404db6aa908a6dea"
integrity sha512-gNiuIEKNCT3OnudQM2kvgSnXsLkSpd6mS/fRnqs6ANtrke6j8OY5l9mnAryf1kgwJMWLg0C1N1cYTZG1xmEYHQ==

pg-types@^2.1.0:
version "2.2.0"
resolved "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz#2d0250d636454f7cfa3b6ae0382fdfa8063254a3"
Expand All @@ -12146,6 +12169,20 @@ pg-types@^2.1.0:
postgres-date "~1.0.4"
postgres-interval "^1.1.0"

pg@^7.17.1:
version "7.17.1"
resolved "https://registry.npmjs.org/pg/-/pg-7.17.1.tgz#1eb4d900e1f21f43978b306972b02a2329138755"
integrity sha512-SYWEip6eADsgDQIZk0bmB2JDOrC8Xu6z10KlhlXl03NSomwVmHB6ZTVyDCwOfT6bXHI8QndJdk5XxSSRXikaSA==
dependencies:
buffer-writer "2.0.0"
packet-reader "1.0.0"
pg-connection-string "0.1.3"
pg-packet-stream "^1.1.0"
pg-pool "^2.0.9"
pg-types "^2.1.0"
pgpass "1.x"
semver "4.3.2"

pg@^7.8.0:
version "7.12.1"
resolved "https://registry.npmjs.org/pg/-/pg-7.12.1.tgz#880636d46d2efbe0968e64e9fe0eeece8ef72a7e"
Expand Down

0 comments on commit 50bdbd8

Please sign in to comment.