2019-12-10 17:44:35 +08:00
|
|
|
import {Config} from '../../src/common/config/private/Config';
|
2019-01-28 03:36:42 +08:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2019-12-15 00:27:01 +08:00
|
|
|
import {SQLConnection} from '../../src/backend/model/database/sql/SQLConnection';
|
2019-12-10 17:44:35 +08:00
|
|
|
import {ServerConfig} from '../../src/common/config/private/IPrivateConfig';
|
2019-01-28 03:36:42 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-12-10 16:36:14 +08:00
|
|
|
constructor(public dbType: ServerConfig.DatabaseType) {
|
2019-02-23 06:39:01 +08:00
|
|
|
this.tempDir = path.join(__dirname, './tmp');
|
|
|
|
this.dbPath = path.join(__dirname, './tmp', 'test.db');
|
2019-01-28 03:36:42 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static describe(name: string, tests: (helper?: SQLTestHelper) => void) {
|
|
|
|
savedDescribe(name, async () => {
|
|
|
|
if (SQLTestHelper.enable.sqlite) {
|
2019-12-10 16:36:14 +08:00
|
|
|
const helper = new SQLTestHelper(ServerConfig.DatabaseType.sqlite);
|
2019-01-28 03:36:42 +08:00
|
|
|
savedDescribe('sqlite', () => {
|
|
|
|
return tests(helper);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (SQLTestHelper.enable.mysql) {
|
2019-12-10 16:36:14 +08:00
|
|
|
const helper = new SQLTestHelper(ServerConfig.DatabaseType.mysql);
|
2019-01-28 03:36:42 +08:00
|
|
|
savedDescribe('mysql', function () {
|
|
|
|
this.timeout(99999999);
|
|
|
|
// @ts-ignore
|
|
|
|
return tests(helper);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async initDB() {
|
2019-12-10 16:36:14 +08:00
|
|
|
if (this.dbType === ServerConfig.DatabaseType.sqlite) {
|
2019-01-28 03:36:42 +08:00
|
|
|
await this.initSQLite();
|
|
|
|
} else {
|
|
|
|
await this.initMySQL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async clearDB() {
|
2019-12-10 16:36:14 +08:00
|
|
|
if (this.dbType === ServerConfig.DatabaseType.sqlite) {
|
2019-01-28 03:36:42 +08:00
|
|
|
await this.clearUpSQLite();
|
|
|
|
} else {
|
|
|
|
await this.clearUpMysql();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async initSQLite() {
|
|
|
|
await this.resetSQLite();
|
|
|
|
|
2019-12-10 16:36:14 +08:00
|
|
|
Config.Server.Database.type = ServerConfig.DatabaseType.sqlite;
|
2019-12-29 07:35:41 +08:00
|
|
|
Config.Server.Database.dbFolder = path.dirname(this.dbPath);
|
2019-01-28 03:36:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private async initMySQL() {
|
2019-12-10 16:36:14 +08:00
|
|
|
Config.Server.Database.type = ServerConfig.DatabaseType.mysql;
|
2019-12-09 21:05:06 +08:00
|
|
|
Config.Server.Database.mysql.database = 'pigallery2_test';
|
2019-01-28 03:36:42 +08: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-10 16:36:14 +08:00
|
|
|
Config.Server.Database.type = ServerConfig.DatabaseType.mysql;
|
2019-12-09 21:05:06 +08:00
|
|
|
Config.Server.Database.mysql.database = 'pigallery2_test';
|
2019-01-28 03:36:42 +08: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-10 16:36:14 +08:00
|
|
|
Config.Server.Database.type = ServerConfig.DatabaseType.mysql;
|
2019-12-09 21:05:06 +08:00
|
|
|
Config.Server.Database.mysql.database = 'pigallery2_test';
|
2019-01-28 03:36:42 +08: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();
|
|
|
|
}
|
|
|
|
}
|