From f259fc36b9e858ab8332af790870d3625be075fa Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Tue, 30 Aug 2022 17:17:25 +0530 Subject: [PATCH] refactor/Now project configuration of test environment is a external project with sakila database as data source and all original sakila tables are shared between all tests while user created tables will be dropped on each test's setup/beforeEach --- .../src/__tests__/unit/rest/dbConfig.ts | 31 +++++++++++++++- .../nocodb/src/__tests__/unit/rest/server.ts | 37 ++++++++----------- .../unit/rest/tests/helpers/project.ts | 35 +++++++++++++++++- .../unit/rest/tests/tableRow.test.ts | 14 ++++++- 4 files changed, 90 insertions(+), 27 deletions(-) diff --git a/packages/nocodb/src/__tests__/unit/rest/dbConfig.ts b/packages/nocodb/src/__tests__/unit/rest/dbConfig.ts index 6e0eaf5bc4..4d6882ba79 100644 --- a/packages/nocodb/src/__tests__/unit/rest/dbConfig.ts +++ b/packages/nocodb/src/__tests__/unit/rest/dbConfig.ts @@ -1,6 +1,33 @@ import NcConfigFactory from '../../../lib/utils/NcConfigFactory'; -const dbName = `test_meta`; +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 = 'sakila'; process.env[`DATABASE_URL`] = `mysql2://root:password@localhost:3306/${dbName}`; const dbConfig = NcConfigFactory.urlToDbConfig( @@ -21,4 +48,4 @@ dbConfig.meta = { }, } as any; -export { dbConfig, dbName }; +export { dbConfig, dbName, sakilaTableNames, sakilaDbName }; diff --git a/packages/nocodb/src/__tests__/unit/rest/server.ts b/packages/nocodb/src/__tests__/unit/rest/server.ts index 28f35866db..8bfc4b5c24 100644 --- a/packages/nocodb/src/__tests__/unit/rest/server.ts +++ b/packages/nocodb/src/__tests__/unit/rest/server.ts @@ -1,37 +1,30 @@ 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 { dbConfig, dbName, sakilaDbName, sakilaTableNames } from './dbConfig'; 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 knexClient.raw(`use ${sakilaDbName}`); + const tablesInSakilaQueryRes = await knexClient.raw(`SHOW TABLES;`); + const tablesInSakila = tablesInSakilaQueryRes[0].map( + (table) => Object.values(table)[0] ); await Promise.all( - userCreatedTableNames.map(async (tableName) => { - await knexClient.raw(`DROP TABLE ${tableName}`); - }) + tablesInSakila + .filter((tableName) => !sakilaTableNames.includes(tableName)) + .map(async (tableName) => { + try { + await knexClient.raw(`DROP TABLE ${sakilaDbName}.${tableName}`); + } catch (e) { + console.error(e); + } + }) ); + await knexClient.raw(`use ${dbName}`); await knexClient.raw('SET FOREIGN_KEY_CHECKS = 0'); for (const tableName of orderedMetaTables) { try { diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts b/packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts index ff70be3647..8628a5d463 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts @@ -1,4 +1,27 @@ import request from 'supertest'; +import { sakilaDbName } from '../../dbConfig'; + +const externalProjectConfig = { + title: 'sakila', + bases: [ + { + type: 'mysql2', + config: { + client: 'mysql2', + connection: { + host: 'localhost', + port: '3306', + user: 'root', + password: 'password', + database: sakilaDbName, + }, + }, + inflection_column: 'camelize', + inflection_table: 'camelize', + }, + ], + external: true, +}; const defaultProjectValue = { title: 'Title', @@ -19,6 +42,16 @@ const createSharedBase = async (app, token, project, sharedBaseArgs = {}) => { }); }; +const createExternalProject = async (app, token) => { + const response = await request(app) + .post('/api/v1/db/meta/projects/') + .set('xc-auth', token) + .send(externalProjectConfig); + + const project = response.body; + return project; +}; + const createProject = async (app, token, projectArgs = defaultProjectValue) => { const response = await request(app) .post('/api/v1/db/meta/projects/') @@ -29,4 +62,4 @@ const createProject = async (app, token, projectArgs = defaultProjectValue) => { return project; }; -export { createProject, createSharedBase }; +export { createProject, createSharedBase, createExternalProject }; diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts b/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts index a0a62345a4..aa6e47623c 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts @@ -4,11 +4,12 @@ import request from 'supertest'; import server from '../server'; import { createUser } from './helpers/user'; import { createTable } from './helpers/table'; -import { createProject } from './helpers/project'; +import { createExternalProject } from './helpers/project'; import { createRow } from './helpers/row'; import { createColumn } from './helpers/column'; import { ColumnType, isSystemColumn } from 'nocodb-sdk'; import Column from '../../../../lib/models/Column'; +import Model from '../../../../lib/models/Model'; function tableTest() { let app; @@ -32,7 +33,7 @@ function tableTest() { const response = await createUser(app, { roles: 'editor' }); token = response.token; - project = await createProject(app, token); + project = await createExternalProject(app, token); table = await createTable(app, token, project); const columnsData = [ { @@ -174,6 +175,15 @@ function tableTest() { throw new Error('Wrong sort'); } }); + + it('Get actors', async () => { + const actorTable = await Model.getByIdOrName({ + project_id: project.id, + base_id: project.bases[0].id, + table_name: 'actor', + }); + console.log(actorTable); + }); } export default function () {