Browse Source

fix(nocodb): Fixed issues with test sakila project config where it was not taking the dbconfig from TestDbMngr

pull/3721/head
Muhammed Mustafa 2 years ago
parent
commit
85eeb424cc
  1. 35
      packages/nocodb/tests/unit/TestDbMngr.ts
  2. 50
      packages/nocodb/tests/unit/factory/project.ts
  3. 2
      packages/nocodb/tests/unit/init/index.ts

35
packages/nocodb/tests/unit/TestDbMngr.ts

@ -12,17 +12,37 @@ export default class TestDbMngr {
public static sakilaKnex: knex; public static sakilaKnex: knex;
public static defaultConnection = { public static defaultConnection = {
user: process.env['DB_USER'] || 'root', user: 'root',
password: process.env['DB_PASSWORD'] || 'password', password: 'password',
host: process.env['DB_HOST'] || 'localhost', host: 'localhost',
port: Number(process.env['DB_PORT']) || 3306, port: 3306,
client: 'mysql2', client: 'mysql2',
} }
public static connection: {
user: string;
password: string;
host: string;
port: number;
client: string;
} = TestDbMngr.defaultConnection;
public static dbConfig: DbConfig; public static dbConfig: DbConfig;
static populateConnectionConfig() {
const { user, password, host, port, client } = TestDbMngr.defaultConnection;
TestDbMngr.connection = {
user: process.env['DB_USER'] || user,
password: process.env['DB_PASSWORD'] || password,
host: process.env['DB_HOST'] || host,
port: Number(process.env['DB_PORT']) || port,
client
}
}
static async testConnection(config: DbConfig) { static async testConnection(config: DbConfig) {
try { try {
console.log('Testing connection', TestDbMngr.connection);
return await SqlMgrv2.testConnection(config); return await SqlMgrv2.testConnection(config);
} catch (e) { } catch (e) {
console.log(e); console.log(e);
@ -31,15 +51,18 @@ export default class TestDbMngr {
} }
static async init() { static async init() {
TestDbMngr.populateConnectionConfig()
if(await TestDbMngr.isMysqlConfigured()){ if(await TestDbMngr.isMysqlConfigured()){
await TestDbMngr.connectMysql(); await TestDbMngr.connectMysql();
} else { } else {
console.log('Mysql is not configured. Switching to sqlite');
await TestDbMngr.switchToSqlite(); await TestDbMngr.switchToSqlite();
} }
} }
static async isMysqlConfigured() { static async isMysqlConfigured() {
const { user, password, host, port, client } = TestDbMngr.defaultConnection; const { user, password, host, port, client } = TestDbMngr.connection;
const config = NcConfigFactory.urlToDbConfig(`${client}://${user}:${password}@${host}:${port}`); const config = NcConfigFactory.urlToDbConfig(`${client}://${user}:${password}@${host}:${port}`);
config.connection = { config.connection = {
user, user,
@ -52,7 +75,7 @@ export default class TestDbMngr {
} }
static async connectMysql() { static async connectMysql() {
const { user, password, host, port, client } = TestDbMngr.defaultConnection; const { user, password, host, port, client } = TestDbMngr.connection;
if(!process.env[`DATABASE_URL`]){ if(!process.env[`DATABASE_URL`]){
process.env[`DATABASE_URL`] = `${client}://${user}:${password}@${host}:${port}/${TestDbMngr.dbName}`; process.env[`DATABASE_URL`] = `${client}://${user}:${password}@${host}:${port}/${TestDbMngr.dbName}`;
} }

50
packages/nocodb/tests/unit/factory/project.ts

@ -1,27 +1,41 @@
import request from 'supertest'; import request from 'supertest';
import Project from '../../../src/lib/models/Project'; import Project from '../../../src/lib/models/Project';
import TestDbMngr from '../TestDbMngr';
const externalProjectConfig = { const sakilaProjectConfig = (context) => {
title: 'sakila', let base;
bases: [
{ if(context.sakilaDbConfig.client === 'mysql2'){
type: 'mysql2', base = {
type: context.sakilaDbConfig.client,
config: {
client: context.sakilaDbConfig.client,
connection: context.sakilaDbConfig.connection
}
};
} else {
base = {
type: context.sakilaDbConfig.client,
config: { config: {
client: 'mysql2', client: context.sakilaDbConfig.client,
connection: { connection: {
host: 'localhost', client: context.sakilaDbConfig.client,
port: '3306', connection: context.sakilaDbConfig.connection,
user: 'root',
password: 'password',
database: TestDbMngr.sakilaDbName,
}, },
}, },
inflection_column: 'camelize', };
inflection_table: 'camelize', }
},
], base = {
external: true, ...base,
inflection_column: 'camelize',
inflection_table: 'camelize',
};
return {
title: 'sakila',
bases: [base],
external: true,
}
}; };
const defaultProjectValue = { const defaultProjectValue = {
@ -47,7 +61,7 @@ const createSakilaProject = async (context) => {
const response = await request(context.app) const response = await request(context.app)
.post('/api/v1/db/meta/projects/') .post('/api/v1/db/meta/projects/')
.set('xc-auth', context.token) .set('xc-auth', context.token)
.send(externalProjectConfig); .send(sakilaProjectConfig(context));
return (await Project.getByTitleOrId(response.body.id)) as Project; return (await Project.getByTitleOrId(response.body.id)) as Project;
}; };

2
packages/nocodb/tests/unit/init/index.ts

@ -38,5 +38,5 @@ export default async function () {
const { token } = await createUser({ app: server }, { roles: 'editor' }); const { token } = await createUser({ app: server }, { roles: 'editor' });
return { app: server, token, dbConfig: TestDbMngr.dbConfig }; return { app: server, token, dbConfig: TestDbMngr.dbConfig, sakilaDbConfig: TestDbMngr.getSakilaDbConfig() };
} }

Loading…
Cancel
Save