mirror of https://github.com/nocodb/nocodb
7 changed files with 147 additions and 143 deletions
@ -0,0 +1,83 @@
|
||||
import { DbConfig } from "../../src/interface/config"; |
||||
import { NcConfigFactory } from "../../src/lib"; |
||||
import SqlMgrv2 from "../../src/lib/db/sql-mgr/v2/SqlMgrv2"; |
||||
import fs from 'fs'; |
||||
|
||||
export default class TestDbMngr { |
||||
public static readonly dbName = 'test_meta'; |
||||
public static readonly sakilaDbName = 'test_sakila'; |
||||
|
||||
public static dbConfig: DbConfig; |
||||
|
||||
static switchToSqlite() { |
||||
process.env[`DATABASE_URL`] = `sqlite:///${TestDbMngr.dbName}.sqlite`; |
||||
TestDbMngr.dbConfig = NcConfigFactory.urlToDbConfig( |
||||
NcConfigFactory.extractXcUrlFromJdbc(process.env[`DATABASE_URL`]) |
||||
); |
||||
} |
||||
|
||||
static async testConnection() { |
||||
try { |
||||
return await SqlMgrv2.testConnection(TestDbMngr.dbConfig); |
||||
} catch (e) { |
||||
console.log(e); |
||||
return { code: -1, message: 'Connection invalid' }; |
||||
} |
||||
} |
||||
|
||||
static async init({ |
||||
user = 'root', |
||||
password = 'password', |
||||
host = 'localhost', |
||||
port = 3306, |
||||
client = 'mysql2', |
||||
} = {}) { |
||||
if(!process.env[`DATABASE_URL`]){ |
||||
process.env[`DATABASE_URL`] = `${client}://${user}:${password}@${host}:${port}/${TestDbMngr.dbName}`; |
||||
} |
||||
|
||||
TestDbMngr.dbConfig = NcConfigFactory.urlToDbConfig( |
||||
NcConfigFactory.extractXcUrlFromJdbc(process.env[`DATABASE_URL`]) |
||||
); |
||||
this.dbConfig.meta = { |
||||
tn: 'nc_evolutions', |
||||
dbAlias: 'db', |
||||
api: { |
||||
type: 'rest', |
||||
prefix: '', |
||||
graphqlDepthLimit: 10, |
||||
}, |
||||
inflection: { |
||||
tn: 'camelize', |
||||
cn: 'camelize', |
||||
}, |
||||
} |
||||
|
||||
const result = await TestDbMngr.testConnection() |
||||
if(result.code === -1){ |
||||
TestDbMngr.switchToSqlite(); |
||||
} |
||||
} |
||||
|
||||
static getMetaDbConfig() { |
||||
return TestDbMngr.dbConfig; |
||||
} |
||||
|
||||
static getSakilaDbConfig() { |
||||
const sakilaDbConfig = { ...TestDbMngr.dbConfig }; |
||||
sakilaDbConfig.connection.database = TestDbMngr.sakilaDbName; |
||||
sakilaDbConfig.connection.multipleStatements = true |
||||
|
||||
return sakilaDbConfig; |
||||
} |
||||
|
||||
static async seedSakila(sakilaKnexClient) {
|
||||
const testsDir = __dirname.replace('tests/unit', 'tests'); |
||||
const schemaFile = fs.readFileSync(`${testsDir}/mysql-sakila-db/03-test-sakila-schema.sql`).toString(); |
||||
const dataFile = fs.readFileSync(`${testsDir}/mysql-sakila-db/04-test-sakila-data.sql`).toString(); |
||||
|
||||
await sakilaKnexClient.raw(schemaFile); |
||||
await sakilaKnexClient.raw(dataFile); |
||||
} |
||||
|
||||
} |
@ -1,55 +0,0 @@
|
||||
import { NcConfigFactory } from "../../src/lib"; |
||||
|
||||
|
||||
const sakilaTableNames = [ |
||||
'actor', |
||||
'address', |
||||
'category', |
||||
'city', |
||||
'country', |
||||
'customer', |
||||
'film', |
||||
'film_actor', |
||||
'film_category', |
||||
'film_text', |
||||
'inventory', |
||||
'language', |
||||
'payment', |
||||
'rental', |
||||
'staff', |
||||
'store', |
||||
'actor_info', |
||||
'customer_list', |
||||
'film_list', |
||||
'nicer_but_slower_film_list', |
||||
'sales_by_film_category', |
||||
'sales_by_store', |
||||
'staff_list', |
||||
]; |
||||
|
||||
const dbName = 'test_meta'; |
||||
const sakilaDbName = 'test_sakila'; |
||||
const dbUser = 'root'; |
||||
const dbPassword = 'password'; |
||||
|
||||
process.env[`DATABASE_URL`] = `mysql2://${dbUser}:${dbPassword}@localhost:3306/${dbName}`; |
||||
|
||||
const dbConfig = NcConfigFactory.urlToDbConfig( |
||||
NcConfigFactory.extractXcUrlFromJdbc(process.env[`DATABASE_URL`]) |
||||
); |
||||
dbConfig.connection.database = dbName; |
||||
dbConfig.meta = { |
||||
tn: 'nc_evolutions', |
||||
dbAlias: 'db', |
||||
api: { |
||||
type: 'rest', |
||||
prefix: '', |
||||
graphqlDepthLimit: 10, |
||||
}, |
||||
inflection: { |
||||
tn: 'camelize', |
||||
column_name: 'camelize', |
||||
}, |
||||
} as any; |
||||
|
||||
export { dbConfig, dbName, sakilaTableNames, sakilaDbName, dbUser, dbPassword }; |
Loading…
Reference in new issue