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';
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 };

37
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 {

35
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 };

14
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 () {

Loading…
Cancel
Save