2019-01-27 21:36:42 +02:00
|
|
|
import {Config} from '../../common/config/private/Config';
|
|
|
|
import {DatabaseType} from '../../common/config/private/IPrivateConfig';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import {SQLConnection} from '../../backend/model/sql/SQLConnection';
|
|
|
|
|
|
|
|
declare let describe: any;
|
|
|
|
const savedDescribe = describe;
|
|
|
|
|
|
|
|
export class SQLTestHelper {
|
|
|
|
|
|
|
|
static enable = {
|
|
|
|
sqlite: true,
|
|
|
|
mysql: true
|
|
|
|
};
|
|
|
|
public static readonly savedDescribe = savedDescribe;
|
|
|
|
tempDir: string;
|
|
|
|
dbPath: string;
|
|
|
|
|
|
|
|
constructor(public dbType: DatabaseType) {
|
2019-02-23 00:39:01 +02:00
|
|
|
this.tempDir = path.join(__dirname, './tmp');
|
|
|
|
this.dbPath = path.join(__dirname, './tmp', 'test.db');
|
2019-01-27 21:36:42 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static describe(name: string, tests: (helper?: SQLTestHelper) => void) {
|
|
|
|
savedDescribe(name, async () => {
|
|
|
|
if (SQLTestHelper.enable.sqlite) {
|
|
|
|
const helper = new SQLTestHelper(DatabaseType.sqlite);
|
|
|
|
savedDescribe('sqlite', () => {
|
|
|
|
return tests(helper);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (SQLTestHelper.enable.mysql) {
|
|
|
|
const helper = new SQLTestHelper(DatabaseType.mysql);
|
|
|
|
savedDescribe('mysql', function () {
|
|
|
|
this.timeout(99999999);
|
|
|
|
// @ts-ignore
|
|
|
|
return tests(helper);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async initDB() {
|
|
|
|
if (this.dbType === DatabaseType.sqlite) {
|
|
|
|
await this.initSQLite();
|
|
|
|
} else {
|
|
|
|
await this.initMySQL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async clearDB() {
|
|
|
|
if (this.dbType === DatabaseType.sqlite) {
|
|
|
|
await this.clearUpSQLite();
|
|
|
|
} else {
|
|
|
|
await this.clearUpMysql();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async initSQLite() {
|
|
|
|
await this.resetSQLite();
|
|
|
|
|
2019-12-09 15:05:06 +02:00
|
|
|
Config.Server.Database.type = DatabaseType.sqlite;
|
|
|
|
Config.Server.Database.sqlite.storage = this.dbPath;
|
2019-01-27 21:36:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async initMySQL() {
|
2019-12-09 15:05:06 +02:00
|
|
|
Config.Server.Database.type = DatabaseType.mysql;
|
|
|
|
Config.Server.Database.mysql.database = 'pigallery2_test';
|
2019-01-27 21:36:42 +02:00
|
|
|
|
|
|
|
await this.resetMySQL();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async resetSQLite() {
|
|
|
|
await SQLConnection.close();
|
|
|
|
|
|
|
|
if (fs.existsSync(this.dbPath)) {
|
|
|
|
fs.unlinkSync(this.dbPath);
|
|
|
|
}
|
|
|
|
if (fs.existsSync(this.tempDir)) {
|
|
|
|
fs.rmdirSync(this.tempDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async resetMySQL() {
|
2019-12-09 15:05:06 +02:00
|
|
|
Config.Server.Database.type = DatabaseType.mysql;
|
|
|
|
Config.Server.Database.mysql.database = 'pigallery2_test';
|
2019-01-27 21:36:42 +02:00
|
|
|
const conn = await SQLConnection.getConnection();
|
|
|
|
await conn.query('DROP DATABASE IF EXISTS ' + conn.options.database);
|
|
|
|
await conn.query('CREATE DATABASE IF NOT EXISTS ' + conn.options.database);
|
|
|
|
await SQLConnection.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async clearUpMysql() {
|
2019-12-09 15:05:06 +02:00
|
|
|
Config.Server.Database.type = DatabaseType.mysql;
|
|
|
|
Config.Server.Database.mysql.database = 'pigallery2_test';
|
2019-01-27 21:36:42 +02:00
|
|
|
const conn = await SQLConnection.getConnection();
|
|
|
|
await conn.query('DROP DATABASE IF EXISTS ' + conn.options.database);
|
|
|
|
await SQLConnection.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async clearUpSQLite() {
|
|
|
|
return this.resetSQLite();
|
|
|
|
}
|
|
|
|
}
|