Browse Source

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

pull/3358/head
Muhammed Mustafa 2 years ago
parent
commit
f259fc36b9
  1. 31
      packages/nocodb/src/__tests__/unit/rest/dbConfig.ts
  2. 37
      packages/nocodb/src/__tests__/unit/rest/server.ts
  3. 35
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts
  4. 14
      packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts

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

@ -1,6 +1,33 @@
import NcConfigFactory from '../../../lib/utils/NcConfigFactory'; 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}`; process.env[`DATABASE_URL`] = `mysql2://root:password@localhost:3306/${dbName}`;
const dbConfig = NcConfigFactory.urlToDbConfig( const dbConfig = NcConfigFactory.urlToDbConfig(
@ -21,4 +48,4 @@ dbConfig.meta = {
}, },
} as any; } as any;
export { dbConfig, dbName }; export { dbConfig, dbName, sakilaTableNames, sakilaDbName };

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

@ -1,37 +1,30 @@
import { Noco } from '../../../lib'; import { Noco } from '../../../lib';
import express from 'express'; import express from 'express';
import { dbConfig, dbName } from './dbConfig'; import { dbConfig, dbName, sakilaDbName, sakilaTableNames } from './dbConfig';
import Model from '../../../lib/models/Model';
import Project from '../../../lib/models/Project';
import { orderedMetaTables } from '../../../lib/utils/globals'; import { orderedMetaTables } from '../../../lib/utils/globals';
const knex = require('knex'); const knex = require('knex');
const clearAllMetaTables = async (knexClient) => { const clearAllMetaTables = async (knexClient) => {
const projects = await Project.list({}); await knexClient.raw(`use ${sakilaDbName}`);
const userCreatedTableNames = []; const tablesInSakilaQueryRes = await knexClient.raw(`SHOW TABLES;`);
await Promise.all( const tablesInSakila = tablesInSakilaQueryRes[0].map(
projects.map(async (project) => { (table) => Object.values(table)[0]
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( await Promise.all(
userCreatedTableNames.map(async (tableName) => { tablesInSakila
await knexClient.raw(`DROP TABLE ${tableName}`); .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'); await knexClient.raw('SET FOREIGN_KEY_CHECKS = 0');
for (const tableName of orderedMetaTables) { for (const tableName of orderedMetaTables) {
try { try {

35
packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts

@ -1,4 +1,27 @@
import request from 'supertest'; 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 = { const defaultProjectValue = {
title: 'Title', 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 createProject = async (app, token, projectArgs = defaultProjectValue) => {
const response = await request(app) const response = await request(app)
.post('/api/v1/db/meta/projects/') .post('/api/v1/db/meta/projects/')
@ -29,4 +62,4 @@ const createProject = async (app, token, projectArgs = defaultProjectValue) => {
return project; return project;
}; };
export { createProject, createSharedBase }; export { createProject, createSharedBase, createExternalProject };

14
packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts

@ -4,11 +4,12 @@ import request from 'supertest';
import server from '../server'; import server from '../server';
import { createUser } from './helpers/user'; import { createUser } from './helpers/user';
import { createTable } from './helpers/table'; import { createTable } from './helpers/table';
import { createProject } from './helpers/project'; import { createExternalProject } from './helpers/project';
import { createRow } from './helpers/row'; import { createRow } from './helpers/row';
import { createColumn } from './helpers/column'; import { createColumn } from './helpers/column';
import { ColumnType, isSystemColumn } from 'nocodb-sdk'; import { ColumnType, isSystemColumn } from 'nocodb-sdk';
import Column from '../../../../lib/models/Column'; import Column from '../../../../lib/models/Column';
import Model from '../../../../lib/models/Model';
function tableTest() { function tableTest() {
let app; let app;
@ -32,7 +33,7 @@ function tableTest() {
const response = await createUser(app, { roles: 'editor' }); const response = await createUser(app, { roles: 'editor' });
token = response.token; token = response.token;
project = await createProject(app, token); project = await createExternalProject(app, token);
table = await createTable(app, token, project); table = await createTable(app, token, project);
const columnsData = [ const columnsData = [
{ {
@ -174,6 +175,15 @@ function tableTest() {
throw new Error('Wrong sort'); 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 () { export default function () {

Loading…
Cancel
Save