From afca1d69b1696ba88e49d8b30ac695bb20197f1b Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Mon, 29 Aug 2022 12:46:20 +0530 Subject: [PATCH] refactor/Now unit tests dont have side effects and before each tests, meta tables are cleared and user generated tables is dropped --- packages/nocodb/package.json | 2 +- .../src/__tests__/unit/rest/dbConfig.ts | 2 +- .../src/__tests__/unit/rest/index.test.ts | 10 +--- .../nocodb/src/__tests__/unit/rest/server.ts | 54 +++++++++++++++++-- .../__tests__/unit/rest/tests/project.test.ts | 3 +- packages/nocodb/src/lib/Noco.ts | 7 +-- packages/nocodb/src/lib/utils/globals.ts | 35 ++++++++++++ 7 files changed, 92 insertions(+), 21 deletions(-) diff --git a/packages/nocodb/package.json b/packages/nocodb/package.json index 170bd5ec97..c50986d449 100644 --- a/packages/nocodb/package.json +++ b/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", diff --git a/packages/nocodb/src/__tests__/unit/rest/dbConfig.ts b/packages/nocodb/src/__tests__/unit/rest/dbConfig.ts index ecef55bc1f..6e0eaf5bc4 100644 --- a/packages/nocodb/src/__tests__/unit/rest/dbConfig.ts +++ b/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', diff --git a/packages/nocodb/src/__tests__/unit/rest/index.test.ts b/packages/nocodb/src/__tests__/unit/rest/index.test.ts index 428ec250a0..065f6da10c 100644 --- a/packages/nocodb/src/__tests__/unit/rest/index.test.ts +++ b/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(); diff --git a/packages/nocodb/src/__tests__/unit/rest/server.ts b/packages/nocodb/src/__tests__/unit/rest/server.ts index ce7be9e299..28f35866db 100644 --- a/packages/nocodb/src/__tests__/unit/rest/server.ts +++ b/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; } diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts b/packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts index 2b3e174c80..2c95911d28 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts +++ b/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; diff --git a/packages/nocodb/src/lib/Noco.ts b/packages/nocodb/src/lib/Noco.ts index 82d9808f04..4094793455 100644 --- a/packages/nocodb/src/lib/Noco.ts +++ b/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 { - 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; diff --git a/packages/nocodb/src/lib/utils/globals.ts b/packages/nocodb/src/lib/utils/globals.ts index 2de2056d59..3a0037d330 100644 --- a/packages/nocodb/src/lib/utils/globals.ts +++ b/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',