Browse Source

refactor/Added some unit tests for table api

pull/3358/head
Muhammed Mustafa 2 years ago
parent
commit
e95739f871
  1. 2
      packages/nocodb/src/__tests__/unit/rest/index.test.ts
  2. 96
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/column.ts
  3. 20
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/table.ts
  4. 63
      packages/nocodb/src/__tests__/unit/rest/tests/table.test.ts

2
packages/nocodb/src/__tests__/unit/rest/index.test.ts

@ -1,6 +1,7 @@
import 'mocha'; import 'mocha';
import authTests from './tests/auth.test'; import authTests from './tests/auth.test';
import projectTests from './tests/project.test'; import projectTests from './tests/project.test';
import tableTests from './tests/table.test';
process.env.NODE_ENV = 'test'; process.env.NODE_ENV = 'test';
process.env.TEST = 'test'; process.env.TEST = 'test';
@ -8,3 +9,4 @@ process.env.NC_DISABLE_CACHE = 'true';
authTests(); authTests();
projectTests(); projectTests();
tableTests();

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

@ -0,0 +1,96 @@
const defaultColumns = [
{
ai: true,
altered: 1,
cdf: null,
ck: false,
clen: null,
column_name: 'id',
ct: 'int(11)',
dt: 'int',
dtx: 'integer',
dtxp: '11',
dtxs: '',
np: 11,
nrqd: false,
ns: 0,
pk: true,
rqd: true,
title: 'Id',
uicn: '',
uidt: 'ID',
uip: '',
un: true,
},
{
ai: false,
altered: 1,
cdf: null,
ck: false,
clen: 45,
column_name: 'title',
ct: 'varchar(45)',
dt: 'varchar',
dtx: 'specificType',
dtxp: '45',
dtxs: '',
np: null,
nrqd: true,
ns: null,
pk: false,
rqd: false,
title: 'Title',
uicn: '',
uidt: 'SingleLineText',
uip: '',
un: false,
},
{
ai: false,
altered: 1,
cdf: 'CURRENT_TIMESTAMP',
ck: false,
clen: 45,
column_name: 'created_at',
ct: 'varchar(45)',
dt: 'timestamp',
dtx: 'specificType',
dtxp: '',
dtxs: '',
np: null,
nrqd: true,
ns: null,
pk: false,
rqd: false,
title: 'CreatedAt',
uicn: '',
uidt: 'DateTime',
uip: '',
un: false,
},
{
ai: false,
altered: 1,
cdf: 'CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP',
ck: false,
clen: 45,
column_name: 'updated_at',
ct: 'varchar(45)',
dt: 'timestamp',
dtx: 'specificType',
dtxp: '',
dtxs: '',
np: null,
nrqd: true,
ns: null,
pk: false,
rqd: false,
title: 'UpdatedAt',
uicn: '',
uidt: 'DateTime',
uip: '',
un: false,
},
];
export { defaultColumns };

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

@ -0,0 +1,20 @@
import request from 'supertest';
import { defaultColumns } from './column';
const defaultTableValue = {
table_name: 'Table1',
title: 'Table1 Title',
columns: defaultColumns,
};
const createTable = async (app, token, project, args = {}) => {
const response = await request(app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.send({ ...defaultTableValue, ...args });
const table = response.body;
return table;
};
export { createTable };

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

@ -0,0 +1,63 @@
// 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';
function tableTest() {
let app;
let token;
let project;
let table;
before(async function () {
app = await server();
const response = await createUser(app, { roles: 'editor' });
token = response.token;
project = await createProject(app, token);
table = await createTable(app, token, project);
});
it('Get table list', function (done) {
request(app)
.get(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.send({})
.expect(200, (err, res) => {
if (err) return done(err);
if (res.body.list.length !== 1) return done('Wrong number of tables');
done();
});
});
it('Update table', function (done) {
request(app)
.patch(`/api/v1/db/meta/tables/${table.id}`)
.set('xc-auth', token)
.send({
project_id: project.id,
table_name: 'new title',
})
.expect(200, async (err) => {
if (err) return done(err);
const updatedTable = await Model.get(table.id);
if (!updatedTable.table_name.endsWith('new title')) {
return done('Table was not updated');
}
done();
});
});
}
export default function () {
describe('Table', tableTest);
}
Loading…
Cancel
Save