Browse Source

refactor/Added unit test for table row data api

pull/3358/head
Muhammed Mustafa 2 years ago
parent
commit
e730cf606c
  1. 2
      packages/nocodb/src/__tests__/unit/rest/index.test.ts
  2. 76
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/column.ts
  3. 40
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/row.ts
  4. 185
      packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts

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

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

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

@ -1,3 +1,7 @@
// import { OracleUi, SqlUiFactory, UITypes } from 'nocodb-sdk';
import request from 'supertest';
// import { dbConfig } from '../../dbConfig';
const defaultColumns = [
{
ai: true,
@ -93,4 +97,74 @@ const defaultColumns = [
},
];
export { defaultColumns };
// const sqlUI = SqlUiFactory.create({ client: dbConfig.client }) as Exclude<
// ReturnType<typeof SqlUiFactory['create']>,
// typeof OracleUi
// >;
// const defaultColumn = async (name, type: UITypes) => {
// // const defaultColumnAttr = {
// // ai: false,
// // altered: 1,
// // cdf: null,
// // ck: false,
// // dt: sqlUI.getDataTypeForUiType({ uidt: type }),
// // dtxp: sqlUI.getDefaultLengthForDatatype(type),
// // dtxs: sqlUI.getDefaultScaleForDatatype(type),
// // clen: null,
// // nrqd: true,
// // np: null,
// // ns: null,
// // pk: false,
// // rqd: false,
// // uip: '',
// // un: false,
// // uicn: '',
// // };
// switch (type) {
// case UITypes.Number:
// return {
// // ...defaultColumnAttr,
// column_name: name,
// ct: 'int(11)',
// dtx: 'integer',
// np: 11,
// ns: 0,
// title: 'Id',
// uidt: type,
// };
// case UITypes.SingleLineText:
// return {
// // ...defaultColumnAttr,
// // clen: 45,
// column_name: 'title',
// // ct: 'varchar(45)',
// // dtx: 'specificType',
// title: 'Title',
// uidt: 'SingleLineText',
// };
// case UITypes.Date:
// return {
// ck: false,
// clen: 45,
// column_name: 'date',
// ct: 'varchar(45)',
// dtx: 'specificType',
// title: 'Date',
// uidt: 'DateTime',
// un: false,
// };
// }
// };
const createColumn = async (app, token, table, columnAttr) => {
const response = await request(app)
.post(`/api/v1/db/meta/tables/${table.id}/columns`)
.set('xc-auth', token)
.send({
...columnAttr,
});
return response.body;
};
export { defaultColumns, createColumn };

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

@ -0,0 +1,40 @@
import { ColumnType, UITypes } from 'nocodb-sdk';
import request from 'supertest';
const rowValue = (column: ColumnType, index: number) => {
switch (column.uidt) {
case UITypes.Number:
return index;
case UITypes.SingleLineText:
return `test-${index}`;
case UITypes.Date:
return '2020-01-01';
case UITypes.DateTime:
return '2020-01-01 00:00:00';
default:
return `test-${index}`;
}
};
const createRow = async (
app,
token,
project,
table,
columns: ColumnType[],
index
) => {
const rowData = columns.reduce((acc, column) => {
acc[column.column_name] = rowValue(column, index);
return acc;
}, {});
const response = await request(app)
.post(`/api/v1/db/data/noco/${project.id}/${table.id}`)
.set('xc-auth', token)
.send(rowData);
return response.body;
};
export { createRow };

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

@ -0,0 +1,185 @@
// 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 { createRow } from './helpers/row';
import { createColumn } from './helpers/column';
import { ColumnType, isSystemColumn } from 'nocodb-sdk';
import Column from '../../../../lib/models/Column';
function tableTest() {
let app;
let token;
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 createProject(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');
}
});
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);
console.log(response.body.list);
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);
console.log(response.body.list);
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');
}
});
}
export default function () {
describe('TableRow', tableTest);
}
Loading…
Cancel
Save