Browse Source

refactor/Refactored all the unit tests

pull/3358/head
Muhammed Mustafa 2 years ago
parent
commit
48066c46f4
  1. 48
      packages/nocodb/src/__tests__/unit/rest/init/cleanupMeta.ts
  2. 26
      packages/nocodb/src/__tests__/unit/rest/init/cleanupSakila.ts
  3. 67
      packages/nocodb/src/__tests__/unit/rest/init/index.ts
  4. 57
      packages/nocodb/src/__tests__/unit/rest/server.ts
  5. 51
      packages/nocodb/src/__tests__/unit/rest/tests/auth.test.ts
  6. 6
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/column.ts
  7. 12
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts
  8. 7
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/row.ts
  9. 6
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/table.ts
  10. 4
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/user.ts
  11. 98
      packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts
  12. 58
      packages/nocodb/src/__tests__/unit/rest/tests/table.test.ts
  13. 289
      packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts

48
packages/nocodb/src/__tests__/unit/rest/init/cleanupMeta.ts

@ -0,0 +1,48 @@
import Model from '../../../../lib/models/Model';
import Project from '../../../../lib/models/Project';
import { orderedMetaTables } from '../../../../lib/utils/globals';
const dropTablesAllProjects = 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}`);
})
);
};
const cleanupMetaTables = async (knexClient) => {
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 (knexClient) {
try {
await dropTablesAllProjects(knexClient);
await cleanupMetaTables(knexClient);
} catch (e) {
console.error('cleanupMeta', e);
}
}

26
packages/nocodb/src/__tests__/unit/rest/init/cleanupSakila.ts

@ -0,0 +1,26 @@
import { sakilaTableNames } from '../dbConfig';
const cleanUpSakila = async (sakilaKnexClient) => {
try {
const tablesInSakilaQueryRes = await sakilaKnexClient.raw(`SHOW TABLES;`);
const tablesInSakila = tablesInSakilaQueryRes[0].map(
(table) => Object.values(table)[0]
);
await Promise.all(
tablesInSakila
.filter((tableName) => !sakilaTableNames.includes(tableName))
.map(async (tableName) => {
try {
await sakilaKnexClient.raw(`DROP TABLE ${tableName}`);
} catch (e) {
console.error(e);
}
})
);
} catch (e) {
console.error('cleanUpSakila', e);
}
};
export default cleanUpSakila;

67
packages/nocodb/src/__tests__/unit/rest/init/index.ts

@ -0,0 +1,67 @@
import { dbConfig, dbName, sakilaDbName } from '../dbConfig';
import { Noco } from '../../../../lib';
import express from 'express';
import cleanupMeta from './cleanupMeta';
import cleanUpSakila from './cleanupSakila';
import { createUser } from '../tests/helpers/user';
const knex = require('knex');
const serverInit = async () => {
const server = express();
server.enable('trust proxy');
server.use(await Noco.init());
return server;
};
const resetDatabase = async (knexClient) => {
try {
if (!Noco.initialized) {
try {
await knexClient.raw(`DROP DATABASE ${dbName}`);
} catch (e) {}
await knexClient.raw(`CREATE DATABASE ${dbName}`);
}
} catch (e) {
console.error('resetDatabase', e);
}
};
const cleanupAllTables = async (knexClient) => {
const sakilaKnexClient = knex({
client: 'mysql2',
connection: {
host: 'localhost',
port: '3306',
user: 'root',
password: 'password',
database: sakilaDbName,
},
});
try {
await cleanUpSakila(sakilaKnexClient);
await sakilaKnexClient.destroy();
await cleanupMeta(knexClient);
} catch (e) {
console.error('cleanupAllTables', e);
sakilaKnexClient.destroy();
}
};
export default async function () {
const knexClient = knex(dbConfig);
await resetDatabase(knexClient);
const server = await serverInit();
await cleanupAllTables(knexClient);
await knexClient.destroy();
const { token } = await createUser({ app: server }, { roles: 'editor' });
return { app: server, token };
}

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

@ -1,57 +0,0 @@
import { Noco } from '../../../lib';
import express from 'express';
import { dbConfig, dbName, sakilaDbName, sakilaTableNames } from './dbConfig';
import { orderedMetaTables } from '../../../lib/utils/globals';
const knex = require('knex');
const clearAllMetaTables = async (knexClient) => {
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(
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 {
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 {
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.init());
await clearAllMetaTables(knexClient);
await knexClient.destroy();
return server;
}

51
packages/nocodb/src/__tests__/unit/rest/tests/auth.test.ts

@ -1,21 +1,18 @@
import { expect } from 'chai';
import 'mocha';
import request from 'supertest';
import server from '../server';
import { createUser, defaultUserArgs } from './helpers/user';
import init from '../init';
import { defaultUserArgs } from './helpers/user';
function authTests() {
let app;
let token;
let context;
beforeEach(async function () {
app = await server();
const response = await createUser(app);
token = response.token;
context = await init();
});
it('Signup with valid email', function (done) {
request(app)
request(context.app)
.post('/api/v1/auth/user/signup')
.send({ email: 'new@example.com', password: defaultUserArgs.password })
.expect(200, (err, res) => {
@ -30,21 +27,21 @@ function authTests() {
});
it('Signup with invalid email', (done) => {
request(app)
request(context.app)
.post('/api/v1/auth/user/signup')
.send({ email: 'test', password: defaultUserArgs.password })
.expect(400, done);
});
it('Signup with invalid passsword', (done) => {
request(app)
request(context.app)
.post('/api/v1/auth/user/signup')
.send({ email: defaultUserArgs.email, password: 'weakpass' })
.expect(400, done);
});
it('Signin with valid credentials', function (done) {
request(app)
request(context.app)
.post('/api/v1/auth/user/signin')
.send({
email: defaultUserArgs.email,
@ -55,7 +52,7 @@ function authTests() {
console.log(res.error);
return done(err);
}
token = res.body.token;
const token = res.body.token;
expect(token).to.be.a('string');
// todo: Verify token
done();
@ -63,7 +60,7 @@ function authTests() {
});
it('Signup without email and password', (done) => {
request(app)
request(context.app)
.post('/api/v1/auth/user/signin')
// pass empty data in request
.send({})
@ -71,21 +68,21 @@ function authTests() {
});
it('Signin with invalid credentials', function (done) {
request(app)
request(context.app)
.post('/api/v1/auth/user/signin')
.send({ email: 'abc@abc.com', password: defaultUserArgs.password })
.expect(400, done);
});
it('Signin with invalid password', function (done) {
request(app)
request(context.app)
.post('/api/v1/auth/user/signin')
.send({ email: defaultUserArgs.email, password: 'wrongPassword' })
.expect(400, done);
});
it('me without token', function (done) {
request(app)
request(context.app)
.get('/api/v1/auth/user/me')
.unset('xc-auth')
.expect(200, (err, res) => {
@ -105,9 +102,9 @@ function authTests() {
});
it('me with token', function (done) {
request(app)
request(context.app)
.get('/api/v1/auth/user/me')
.set('xc-auth', token)
.set('xc-auth', context.token)
.expect(200, function (err, res) {
if (err) {
return done(err);
@ -119,7 +116,7 @@ function authTests() {
});
it('Forgot password with a non-existing email id', function (done) {
request(app)
request(context.app)
.post('/api/v1/auth/password/forgot')
.send({ email: 'nonexisting@email.com' })
.expect(400, done);
@ -129,9 +126,9 @@ function authTests() {
// it('Forgot password with an existing email id', function () {});
it('Change password', function (done) {
request(app)
request(context.app)
.post('/api/v1/auth/password/change')
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
currentPassword: defaultUserArgs.password,
newPassword: 'NEW' + defaultUserArgs.password,
@ -140,7 +137,7 @@ function authTests() {
});
it('Change password - after logout', function (done) {
request(app)
request(context.app)
.post('/api/v1/auth/password/change')
.unset('xc-auth')
.send({
@ -154,14 +151,14 @@ function authTests() {
// todo:
it('Reset Password with an invalid token', function (done) {
request(app)
request(context.app)
.post('/api/v1/auth/password/reset/someRandomValue')
.send({ email: defaultUserArgs.email })
.expect(400, done);
});
it('Email validate with an invalid token', function (done) {
request(app)
request(context.app)
.post('/api/v1/auth/email/validate/someRandomValue')
.send({ email: defaultUserArgs.email })
.expect(400, done);
@ -169,7 +166,7 @@ function authTests() {
// todo:
// it('Email validate with a valid token', function (done) {
// // request(app)
// // request(context.app)
// // .post('/auth/email/validate/someRandomValue')
// // .send({email: EMAIL_ID})
// // .expect(500, done);
@ -177,7 +174,7 @@ function authTests() {
// todo:
// it('Forgot password validate with a valid token', function (done) {
// // request(app)
// // request(context.app)
// // .post('/auth/token/validate/someRandomValue')
// // .send({email: EMAIL_ID})
// // .expect(500, done);
@ -185,7 +182,7 @@ function authTests() {
// todo:
// it('Reset Password with an valid token', function (done) {
// // request(app)
// // request(context.app)
// // .post('/auth/password/reset/someRandomValue')
// // .send({password: 'anewpassword'})
// // .expect(500, done);

6
packages/nocodb/src/__tests__/unit/rest/tests/helpers/column.ts

@ -157,10 +157,10 @@ const defaultColumns = [
// }
// };
const createColumn = async (app, token, table, columnAttr) => {
const response = await request(app)
const createColumn = async (context, table, columnAttr) => {
const response = await request(context.app)
.post(`/api/v1/db/meta/tables/${table.id}/columns`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
...columnAttr,
});

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

@ -42,20 +42,20 @@ const createSharedBase = async (app, token, project, sharedBaseArgs = {}) => {
});
};
const createExternalProject = async (app, token) => {
const response = await request(app)
const createExternalProject = async (context) => {
const response = await request(context.app)
.post('/api/v1/db/meta/projects/')
.set('xc-auth', token)
.set('xc-auth', context.token)
.send(externalProjectConfig);
const project = response.body;
return project;
};
const createProject = async (app, token, projectArgs = defaultProjectValue) => {
const response = await request(app)
const createProject = async (context, projectArgs = defaultProjectValue) => {
const response = await request(context.app)
.post('/api/v1/db/meta/projects/')
.set('xc-auth', token)
.set('xc-auth', context.token)
.send(projectArgs);
const project = response.body;

7
packages/nocodb/src/__tests__/unit/rest/tests/helpers/row.ts

@ -17,8 +17,7 @@ const rowValue = (column: ColumnType, index: number) => {
};
const createRow = async (
app,
token,
context,
project,
table,
columns: ColumnType[],
@ -29,9 +28,9 @@ const createRow = async (
return acc;
}, {});
const response = await request(app)
const response = await request(context.app)
.post(`/api/v1/db/data/noco/${project.id}/${table.id}`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send(rowData);
return response.body;

6
packages/nocodb/src/__tests__/unit/rest/tests/helpers/table.ts

@ -7,10 +7,10 @@ const defaultTableValue = {
columns: defaultColumns,
};
const createTable = async (app, token, project, args = {}) => {
const response = await request(app)
const createTable = async (context, project, args = {}) => {
const response = await request(context.app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({ ...defaultTableValue, ...args });
const table = response.body;

4
packages/nocodb/src/__tests__/unit/rest/tests/helpers/user.ts

@ -6,9 +6,9 @@ const defaultUserArgs = {
password: 'A1234abh2@dsad',
};
const createUser = async (app, userArgs = {}) => {
const createUser = async (context, userArgs = {}) => {
const args = { ...defaultUserArgs, ...userArgs };
const response = await request(app)
const response = await request(context.app)
.post('/api/v1/auth/user/signup')
.send(args);
const user = User.getByEmail(args.email);

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

@ -1,49 +1,43 @@
import 'mocha';
import request from 'supertest';
import server from '../server';
import Project from '../../../../lib/models/Project';
import { createProject, createSharedBase } from './helpers/project';
import { createUser } from './helpers/user';
import { beforeEach } from 'mocha';
import { Exception } from 'handlebars';
import init from '../init/index';
function projectTest() {
let app;
let token;
let context;
let project;
beforeEach(async function () {
app = await server();
const response = await createUser(app, { roles: 'editor' });
token = response.token;
project = await createProject(app, token);
context = await init();
project = await createProject(context);
});
it('Get project info', function (done) {
request(app)
request(context.app)
.get(`/api/v1/db/meta/projects/${project.id}/info`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({})
.expect(200, done);
});
// todo: Test by creating models under project and check if the UCL is working
it('UI ACL', (done) => {
request(app)
request(context.app)
.get(`/api/v1/db/meta/projects/${project.id}/visibility-rules`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({})
.expect(200, (_, res) => {
console.log('UI ACL Respinse:', res.body);
done();
});
.expect(200, done);
});
// todo: Test creating visibility set
it('List projects', function (done) {
request(app)
request(context.app)
.get('/api/v1/db/meta/projects/')
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({})
.expect(200, (err, res) => {
if (err) done(err);
@ -56,9 +50,9 @@ function projectTest() {
});
it('Create project', function (done) {
request(app)
request(context.app)
.post('/api/v1/db/meta/projects/')
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
title: 'Title1',
})
@ -73,9 +67,9 @@ function projectTest() {
});
it('Create projects with existing title', function (done) {
request(app)
request(context.app)
.post(`/api/v1/db/meta/projects/`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
title: project.title,
})
@ -107,9 +101,9 @@ function projectTest() {
// });
it('Read project', (done) => {
request(app)
request(context.app)
.get(`/api/v1/db/meta/projects/${project.id}`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send()
.expect(200, (err, res) => {
if (err) return done(err);
@ -121,9 +115,9 @@ function projectTest() {
});
it('Update projects', function (done) {
request(app)
request(context.app)
.patch(`/api/v1/db/meta/projects/${project.id}`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
title: 'NewTitle',
})
@ -143,10 +137,12 @@ function projectTest() {
});
it('Update projects with existing title', async function () {
const newProject = await createProject(app, token, { title: 'NewTitle1' });
return await request(app)
const newProject = await createProject(context, {
title: 'NewTitle1',
});
return await request(context.app)
.patch(`/api/v1/db/meta/projects/${project.id}`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
title: newProject.title,
})
@ -154,9 +150,9 @@ function projectTest() {
});
it('Create project shared base', (done) => {
request(app)
request(context.app)
.post(`/api/v1/db/meta/projects/${project.id}/shared`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
roles: 'viewer',
password: 'test',
@ -179,9 +175,9 @@ function projectTest() {
});
it('Created project shared base should have only editor or viewer role', (done) => {
request(app)
request(context.app)
.post(`/api/v1/db/meta/projects/${project.id}/shared`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
roles: 'commenter',
password: 'test',
@ -200,11 +196,11 @@ function projectTest() {
});
it('Updated project shared base should have only editor or viewer role', async () => {
await createSharedBase(app, token, project);
await createSharedBase(context.app, context.token, project);
await request(app)
await request(context.app)
.patch(`/api/v1/db/meta/projects/${project.id}/shared`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
roles: 'commenter',
password: 'test',
@ -218,11 +214,11 @@ function projectTest() {
});
it('Updated project shared base', async () => {
await createSharedBase(app, token, project);
await createSharedBase(context.app, context.token, project);
await request(app)
await request(context.app)
.patch(`/api/v1/db/meta/projects/${project.id}/shared`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
roles: 'editor',
password: 'test',
@ -236,11 +232,11 @@ function projectTest() {
});
it('Get project shared base', async () => {
await createSharedBase(app, token, project);
await createSharedBase(context.app, context.token, project);
await request(app)
await request(context.app)
.get(`/api/v1/db/meta/projects/${project.id}/shared`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send()
.expect(200);
@ -251,11 +247,11 @@ function projectTest() {
});
it('Delete project shared base', async () => {
await createSharedBase(app, token, project);
await createSharedBase(context.app, context.token, project);
await request(app)
await request(context.app)
.delete(`/api/v1/db/meta/projects/${project.id}/shared`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send()
.expect(200);
const updatedProject = await Project.getByTitleOrId(project.id);
@ -267,26 +263,26 @@ function projectTest() {
// todo: Do compare api test
it('Meta diff sync', (done) => {
request(app)
request(context.app)
.get(`/api/v1/db/meta/projects/${project.id}/meta-diff`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send()
.expect(200, done);
});
it('Meta diff sync', (done) => {
request(app)
request(context.app)
.post(`/api/v1/db/meta/projects/${project.id}/meta-diff`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send()
.expect(200, done);
});
// todo: improve test. Check whether the all the actions are present in the response and correct as well
it('Meta diff sync', (done) => {
request(app)
request(context.app)
.get(`/api/v1/db/meta/projects/${project.id}/audits`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send()
.expect(200, done);
});

58
packages/nocodb/src/__tests__/unit/rest/tests/table.test.ts

@ -1,32 +1,28 @@
// import { expect } from 'chai';
import 'mocha';
import request from 'supertest';
import server from '../server';
import { createUser } from './helpers/user';
import { createTable } from './helpers/table';
import { createProject } from './helpers/project';
import Model from '../../../../lib/models/Model';
import { defaultColumns } from './helpers/column';
import init from '../init';
function tableTest() {
let app;
let token;
let context;
let project;
let table;
beforeEach(async function () {
app = await server();
const response = await createUser(app, { roles: 'editor' });
token = response.token;
context = await init();
project = await createProject(app, token);
table = await createTable(app, token, project);
project = await createProject(context);
table = await createTable(context, project);
});
it('Get table list', function (done) {
request(app)
request(context.app)
.get(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({})
.expect(200, (err, res) => {
if (err) return done(err);
@ -38,9 +34,9 @@ function tableTest() {
});
it('Create table', function (done) {
request(app)
request(context.app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
table_name: 'table2',
title: 'new_title_2',
@ -74,9 +70,9 @@ function tableTest() {
});
it('Create table with no table name', function (done) {
request(app)
request(context.app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
table_name: undefined,
title: 'new_title',
@ -110,9 +106,9 @@ function tableTest() {
});
it('Create table with same table name', function (done) {
request(app)
request(context.app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
table_name: table.table_name,
title: 'New_title',
@ -139,9 +135,9 @@ function tableTest() {
});
it('Create table with same title', function (done) {
request(app)
request(context.app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
table_name: 'New_table_name',
title: table.title,
@ -168,9 +164,9 @@ function tableTest() {
});
it('Create table with title length more than the limit', function (done) {
request(app)
request(context.app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
table_name: 'a'.repeat(256),
title: 'new_title',
@ -197,9 +193,9 @@ function tableTest() {
});
it('Create table with title having leading white space', function (done) {
request(app)
request(context.app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
table_name: 'table_name_with_whitespace ',
title: 'new_title',
@ -230,9 +226,9 @@ function tableTest() {
});
it('Update table', function (done) {
request(app)
request(context.app)
.patch(`/api/v1/db/meta/tables/${table.id}`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
project_id: project.id,
table_name: 'new_title',
@ -251,9 +247,9 @@ function tableTest() {
});
it('Delete table', function (done) {
request(app)
request(context.app)
.delete(`/api/v1/db/meta/tables/${table.id}`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({})
.expect(200, async (err) => {
if (err) return done(err);
@ -275,9 +271,9 @@ function tableTest() {
// todo: Check the if views are also deleted
it('Get table', function (done) {
request(app)
request(context.app)
.get(`/api/v1/db/meta/tables/${table.id}`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({})
.expect(200, async (err, res) => {
if (err) return done(err);
@ -291,9 +287,9 @@ function tableTest() {
// todo: flaky test, order condition is sometimes not met
it('Reorder table', function (done) {
const newOrder = table.order === 0 ? 1 : 0;
request(app)
request(context.app)
.post(`/api/v1/db/meta/tables/${table.id}/reorder`)
.set('xc-auth', token)
.set('xc-auth', context.token)
.send({
order: newOrder,
})

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

@ -1,180 +1,139 @@
// import { expect } from 'chai';
import 'mocha';
import request from 'supertest';
import server from '../server';
import { createUser } from './helpers/user';
import { createTable } from './helpers/table';
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';
import init from '../init';
function tableTest() {
let app;
let token;
let context;
let project;
let table;
let columns: ColumnType[] = [];
const createRows = async (count = 10) => {
await Promise.all(
Array(count)
.fill(0)
.map(async (_, index) =>
createRow(app, token, project, table, columns, index)
)
);
};
beforeEach(async function () {
app = await server();
const response = await createUser(app, { roles: 'editor' });
token = response.token;
project = await createExternalProject(app, token);
table = await createTable(app, token, project);
const columnsData = [
{
title: 'New Title',
column_name: 'new_title',
uidt: 'SingleLineText',
},
{
title: 'Priority',
column_name: 'priority',
uidt: 'Number',
},
];
for (const columnData of columnsData) {
await createColumn(app, token, table, columnData);
}
columns = (await Column.list({ fk_model_id: table.id })).filter(
(col) => !isSystemColumn(col)
);
});
it('Get table data list', async function () {
const rowCount = 10;
await createRows(rowCount);
const response = await request(app)
.get(`/api/v1/db/data/noco/${project.id}/${table.id}`)
.set('xc-auth', token)
.send({})
.expect(200);
if (response.body.list.length !== rowCount) {
throw new Error('Wrong number of rows');
}
});
it('Get table data list with required columns', async function () {
const rowCount = 10;
await createRows(rowCount);
const newTitleColumn = columns.find((col) => col.title === 'New Title');
const visibleColumns = [newTitleColumn.title];
const response = await request(app)
.get(`/api/v1/db/data/noco/${project.id}/${table.id}`)
.set('xc-auth', token)
.query({
fields: visibleColumns,
})
.expect(200);
if (response.body.list.length !== rowCount) {
throw new Error('Wrong number of rows');
}
const sameArrayContent = (a: Array<any>, b: Array<any>) => {
return a.length === b.length && a.every((v, i) => v === b[i]);
};
if (!sameArrayContent(Object.keys(response.body.list[0]), visibleColumns)) {
console.error(Object.keys(response.body.list[0]), visibleColumns);
throw new Error('Wrong column value');
}
});
context = await init();
it('Get desc sorted table data list with required columns', async function () {
const rowCount = 10;
await createRows(rowCount);
const newTitleColumn = columns.find((col) => col.title === 'New Title');
const visibleColumns = [newTitleColumn.title];
const sortInfo = [{ fk_column_id: newTitleColumn.id, direction: 'desc' }];
const response = await request(app)
.get(`/api/v1/db/data/noco/${project.id}/${table.id}`)
.set('xc-auth', token)
.query({
fields: visibleColumns,
sortArrJson: JSON.stringify(sortInfo),
})
.expect(200);
if (response.body.list.length !== rowCount) {
throw new Error('Wrong number of rows');
}
const sameArrayContent = (a: Array<any>, b: Array<any>) => {
return a.length === b.length && a.every((v, i) => v === b[i]);
};
if (!sameArrayContent(Object.keys(response.body.list[0]), visibleColumns)) {
console.error(Object.keys(response.body.list[0]), visibleColumns);
throw new Error('Wrong column value');
}
if (
response.body.list[0][newTitleColumn.title] !== 'test-9' ||
response.body.list[response.body.list.length - 1][
newTitleColumn.title
] !== 'test-0'
) {
throw new Error('Wrong sort');
}
project = await createExternalProject(context);
});
it('Get asc sorted table data list with required columns', async function () {
const rowCount = 10;
await createRows(rowCount);
const newTitleColumn = columns.find((col) => col.title === 'New Title');
const visibleColumns = [newTitleColumn.title];
const sortInfo = [{ fk_column_id: newTitleColumn.id, direction: 'asc' }];
const response = await request(app)
.get(`/api/v1/db/data/noco/${project.id}/${table.id}`)
.set('xc-auth', token)
.query({
fields: visibleColumns,
sortArrJson: JSON.stringify(sortInfo),
})
.expect(200);
if (response.body.list.length !== rowCount) {
throw new Error('Wrong number of rows');
}
const sameArrayContent = (a: Array<any>, b: Array<any>) => {
return a.length === b.length && a.every((v, i) => v === b[i]);
};
if (!sameArrayContent(Object.keys(response.body.list[0]), visibleColumns)) {
console.error(Object.keys(response.body.list[0]), visibleColumns);
throw new Error('Wrong column value');
}
if (
response.body.list[0][newTitleColumn.title] !== 'test-0' ||
response.body.list[response.body.list.length - 1][
newTitleColumn.title
] !== 'test-9'
) {
throw new Error('Wrong sort');
}
});
// it('Get table data list', async function () {
// const rowCount = 10;
// await createRows(rowCount);
// const response = await request(app)
// .get(`/api/v1/db/data/noco/${project.id}/${table.id}`)
// .set('xc-auth', token)
// .send({})
// .expect(200);
// if (response.body.list.length !== rowCount) {
// throw new Error('Wrong number of rows');
// }
// });
// it('Get table data list with required columns', async function () {
// const rowCount = 10;
// await createRows(rowCount);
// const newTitleColumn = columns.find((col) => col.title === 'New Title');
// const visibleColumns = [newTitleColumn.title];
// const response = await request(app)
// .get(`/api/v1/db/data/noco/${project.id}/${table.id}`)
// .set('xc-auth', token)
// .query({
// fields: visibleColumns,
// })
// .expect(200);
// if (response.body.list.length !== rowCount) {
// throw new Error('Wrong number of rows');
// }
// const sameArrayContent = (a: Array<any>, b: Array<any>) => {
// return a.length === b.length && a.every((v, i) => v === b[i]);
// };
// if (!sameArrayContent(Object.keys(response.body.list[0]), visibleColumns)) {
// console.error(Object.keys(response.body.list[0]), visibleColumns);
// throw new Error('Wrong column value');
// }
// });
// it('Get desc sorted table data list with required columns', async function () {
// const rowCount = 10;
// await createRows(rowCount);
// const newTitleColumn = columns.find((col) => col.title === 'New Title');
// const visibleColumns = [newTitleColumn.title];
// const sortInfo = [{ fk_column_id: newTitleColumn.id, direction: 'desc' }];
// const response = await request(app)
// .get(`/api/v1/db/data/noco/${project.id}/${table.id}`)
// .set('xc-auth', token)
// .query({
// fields: visibleColumns,
// sortArrJson: JSON.stringify(sortInfo),
// })
// .expect(200);
// if (response.body.list.length !== rowCount) {
// throw new Error('Wrong number of rows');
// }
// const sameArrayContent = (a: Array<any>, b: Array<any>) => {
// return a.length === b.length && a.every((v, i) => v === b[i]);
// };
// if (!sameArrayContent(Object.keys(response.body.list[0]), visibleColumns)) {
// console.error(Object.keys(response.body.list[0]), visibleColumns);
// throw new Error('Wrong column value');
// }
// if (
// response.body.list[0][newTitleColumn.title] !== 'test-9' ||
// response.body.list[response.body.list.length - 1][
// newTitleColumn.title
// ] !== 'test-0'
// ) {
// throw new Error('Wrong sort');
// }
// });
// it('Get asc sorted table data list with required columns', async function () {
// const rowCount = 10;
// await createRows(rowCount);
// const newTitleColumn = columns.find((col) => col.title === 'New Title');
// const visibleColumns = [newTitleColumn.title];
// const sortInfo = [{ fk_column_id: newTitleColumn.id, direction: 'asc' }];
// const response = await request(app)
// .get(`/api/v1/db/data/noco/${project.id}/${table.id}`)
// .set('xc-auth', token)
// .query({
// fields: visibleColumns,
// sortArrJson: JSON.stringify(sortInfo),
// })
// .expect(200);
// if (response.body.list.length !== rowCount) {
// throw new Error('Wrong number of rows');
// }
// const sameArrayContent = (a: Array<any>, b: Array<any>) => {
// return a.length === b.length && a.every((v, i) => v === b[i]);
// };
// if (!sameArrayContent(Object.keys(response.body.list[0]), visibleColumns)) {
// console.error(Object.keys(response.body.list[0]), visibleColumns);
// throw new Error('Wrong column value');
// }
// if (
// response.body.list[0][newTitleColumn.title] !== 'test-0' ||
// response.body.list[response.body.list.length - 1][
// newTitleColumn.title
// ] !== 'test-9'
// ) {
// throw new Error('Wrong sort');
// }
// });
it('Get actors', async () => {
const actorTable = await Model.getByIdOrName({

Loading…
Cancel
Save