Browse Source

refactor/Now unit tests dont have side effects and before each tests, meta tables are cleared and user generated tables is dropped

pull/3358/head
Muhammed Mustafa 2 years ago
parent
commit
afca1d69b1
  1. 2
      packages/nocodb/package.json
  2. 2
      packages/nocodb/src/__tests__/unit/rest/dbConfig.ts
  3. 10
      packages/nocodb/src/__tests__/unit/rest/index.test.ts
  4. 54
      packages/nocodb/src/__tests__/unit/rest/server.ts
  5. 3
      packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts
  6. 7
      packages/nocodb/src/lib/Noco.ts
  7. 35
      packages/nocodb/src/lib/utils/globals.ts

2
packages/nocodb/package.json

@ -21,7 +21,7 @@
"local:test:graphql": "cross-env DATABASE_URL=mysql://root:password@localhost:3306/sakila TS_NODE_PROJECT=tsconfig.json mocha -r ts-node/register src/__tests__/graphql.test.ts --recursive --timeout 10000 --exit",
"test:graphql": "cross-env TS_NODE_PROJECT=tsconfig.json mocha -r ts-node/register src/__tests__/graphql.test.ts --recursive --timeout 10000 --exit",
"test:grpc": "cross-env TS_NODE_PROJECT=tsconfig.json mocha -r ts-node/register src/__tests__/grpc.test.ts --recursive --timeout 10000 --exit",
"local:test:rest": "cross-env DATABASE_URL=mysql://root:password@localhost:3306/sakila TS_NODE_PROJECT=tsconfig.json mocha -r ts-node/register src/__tests__/unit/rest/index.test.ts --recursive --timeout 10000 --exit",
"local:test:rest": "cross-env mocha -r ts-node/register src/__tests__/unit/rest/index.test.ts --recursive --timeout 10000 --exit",
"test:rest": "cross-env TS_NODE_PROJECT=tsconfig.json mocha -r ts-node/register src/__tests__/rest.test.ts --recursive --timeout 10000 --exit",
"test1": "run-s build test:*",
"test:lint": "tslint --project . && prettier \"src/**/*.ts\" --list-different",

2
packages/nocodb/src/__tests__/unit/rest/dbConfig.ts

@ -6,7 +6,7 @@ process.env[`DATABASE_URL`] = `mysql2://root:password@localhost:3306/${dbName}`;
const dbConfig = NcConfigFactory.urlToDbConfig(
NcConfigFactory.extractXcUrlFromJdbc(process.env[`DATABASE_URL`])
);
dbConfig.connection.database = 'sakila';
dbConfig.connection.database = dbName;
dbConfig.meta = {
tn: 'nc_evolutions',
dbAlias: 'db',

10
packages/nocodb/src/__tests__/unit/rest/index.test.ts

@ -1,18 +1,10 @@
import 'mocha';
import authTests from './tests/auth.test';
import projectTests from './tests/project.test';
import NcConfigFactory from '../../../lib/utils/NcConfigFactory';
const dbName = `test_meta`;
process.env.NODE_ENV = 'test';
process.env.TEST = 'test';
process.env[`DATABASE_URL`] = `mysql2://root:password@localhost:3306/${dbName}`;
const dbConfig = NcConfigFactory.urlToDbConfig(
NcConfigFactory.extractXcUrlFromJdbc(process.env[`DATABASE_URL`])
);
dbConfig.connection.database = 'test_meta';
process.env.NC_DISABLE_CACHE = 'true';
authTests();
projectTests();

54
packages/nocodb/src/__tests__/unit/rest/server.ts

@ -1,18 +1,64 @@
import { Noco } from '../../../lib';
import express from 'express';
import { dbConfig, dbName } from './dbConfig';
import Model from '../../../lib/models/Model';
import Project from '../../../lib/models/Project';
import { orderedMetaTables } from '../../../lib/utils/globals';
const knex = require('knex');
const clearAllMetaTables = async (knexClient) => {
const projects = await Project.list({});
const userCreatedTableNames = [];
await Promise.all(
projects.map(async (project) => {
await project.getBases();
const base = project.bases && project.bases[0];
if (!base) return;
const models = await Model.list({
project_id: project.id,
base_id: base.id,
});
models.forEach((model) => {
userCreatedTableNames.push(model.table_name);
});
})
);
await Promise.all(
userCreatedTableNames.map(async (tableName) => {
await knexClient.raw(`DROP TABLE ${tableName}`);
})
);
await knexClient.raw('SET FOREIGN_KEY_CHECKS = 0');
for (const tableName of orderedMetaTables) {
try {
await knexClient.raw(`DELETE FROM ${tableName}`);
} catch (e) {}
}
await knexClient.raw('SET FOREIGN_KEY_CHECKS = 1');
};
export default async function () {
const knexClient = knex(dbConfig);
try {
await knex(dbConfig).raw(`DROP DATABASE ${dbName}`);
await knex(dbConfig).raw(`CREATE DATABASE ${dbName}`);
} catch {}
if (!Noco.initialized) {
try {
await knexClient.raw(`DROP DATABASE ${dbName}`);
} catch (e) {}
await knexClient.raw(`CREATE DATABASE ${dbName}`);
}
} catch (e) {
console.error(e);
}
const server = express();
server.enable('trust proxy');
server.use(await Noco.forceInit());
server.use(await Noco.init());
await clearAllMetaTables(knexClient);
await knexClient.destroy();
return server;
}

3
packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts

@ -4,13 +4,14 @@ import server from '../server';
import Project from '../../../../lib/models/Project';
import { createProject } from './helpers/project';
import { createUser } from './helpers/user';
import { beforeEach } from 'mocha';
function projectTest() {
let app;
let token;
let project;
before(async function () {
beforeEach(async function () {
app = await server();
const response = await createUser(app, { roles: 'editor' });
token = response.token;

7
packages/nocodb/src/lib/Noco.ts

@ -66,11 +66,6 @@ export default class Noco {
return `${siteUrl}${Noco._this?.config?.dashboardPath}`;
}
public static async forceInit(): Promise<Router> {
Noco._this = new Noco();
return Noco._this.init();
}
public static async init(
args?: {
progressCallback?: Function;
@ -86,10 +81,12 @@ export default class Noco {
return Noco._this.router;
}
Noco._this = new Noco();
Noco.initialized = true;
return Noco._this.init(args, server, app);
}
private static config: NcConfig;
public static initialized: boolean;
public readonly router: express.Router;
public readonly projectRouter: express.Router;
public static _ncMeta: NcMetaIO;

35
packages/nocodb/src/lib/utils/globals.ts

@ -39,6 +39,41 @@ export enum MetaTable {
SYNC_LOGS = 'nc_sync_logs_v2',
}
export const orderedMetaTables = [
MetaTable.MODEL_ROLE_VISIBILITY,
MetaTable.PLUGIN,
MetaTable.AUDIT,
MetaTable.TEAM_USERS,
MetaTable.TEAMS,
MetaTable.ORGS,
MetaTable.PROJECT_USERS,
MetaTable.USERS,
MetaTable.KANBAN_VIEW_COLUMNS,
MetaTable.KANBAN_VIEW,
MetaTable.GRID_VIEW_COLUMNS,
MetaTable.GRID_VIEW,
MetaTable.GALLERY_VIEW_COLUMNS,
MetaTable.GALLERY_VIEW,
MetaTable.FORM_VIEW_COLUMNS,
MetaTable.FORM_VIEW,
MetaTable.SHARED_VIEWS,
MetaTable.SORT,
MetaTable.FILTER_EXP,
MetaTable.HOOK_LOGS,
MetaTable.HOOKS,
MetaTable.VIEWS,
MetaTable.COL_FORMULA,
MetaTable.COL_ROLLUP,
MetaTable.COL_LOOKUP,
MetaTable.COL_SELECT_OPTIONS,
MetaTable.COL_RELATIONS,
MetaTable.COLUMN_VALIDATIONS,
MetaTable.COLUMNS,
MetaTable.MODELS,
MetaTable.BASES,
MetaTable.PROJECT,
];
export enum CacheScope {
PROJECT = 'project',
BASE = 'base',

Loading…
Cancel
Save