Browse Source

refactor/Minor refactor and added/commented a test for tableRow

pull/3358/head
Muhammed Mustafa 2 years ago
parent
commit
ba744ba4b0
  1. 87
      packages/nocodb/src/__tests__/unit/rest/tests/factory/column.ts
  2. 64
      packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts

87
packages/nocodb/src/__tests__/unit/rest/tests/factory/column.ts

@ -1,8 +1,6 @@
// import { OracleUi, SqlUiFactory, UITypes } from 'nocodb-sdk'; import { ColumnType, UITypes } from 'nocodb-sdk';
import { UITypes } from 'nocodb-sdk';
import request from 'supertest'; import request from 'supertest';
import Model from '../../../../../lib/models/Model'; import Model from '../../../../../lib/models/Model';
// import { dbConfig } from '../../dbConfig';
const defaultColumns = [ const defaultColumns = [
{ {
@ -99,74 +97,18 @@ const 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 (context, table, columnAttr) => { const createColumn = async (context, table, columnAttr) => {
const response = await request(context.app) await request(context.app)
.post(`/api/v1/db/meta/tables/${table.id}/columns`) .post(`/api/v1/db/meta/tables/${table.id}/columns`)
.set('xc-auth', context.token) .set('xc-auth', context.token)
.send({ .send({
...columnAttr, ...columnAttr,
}); });
return response.body;
const column: ColumnType = (await table.getColumns()).find(
(column) => column.title === columnAttr.title
);
return column;
}; };
const createRollupColumn = async ( const createRollupColumn = async (
@ -202,7 +144,8 @@ const createRollupColumn = async (
column.uidt === UITypes.LinkToAnotherRecord && column.uidt === UITypes.LinkToAnotherRecord &&
column.colOptions?.fk_related_model_id === childTable.id column.colOptions?.fk_related_model_id === childTable.id
); );
await createColumn(context, table, {
const rollupColumn = await createColumn(context, table, {
title: title, title: title,
uidt: UITypes.Rollup, uidt: UITypes.Rollup,
fk_relation_column_id: ltarColumn?.id, fk_relation_column_id: ltarColumn?.id,
@ -212,11 +155,6 @@ const createRollupColumn = async (
column_name: title, column_name: title,
}); });
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const rollupColumn = (await table.getColumns()).find(
(column) => column.title === title
)!;
return rollupColumn; return rollupColumn;
}; };
@ -257,7 +195,7 @@ const createLookupColumn = async (
column.uidt === UITypes.LinkToAnotherRecord && column.uidt === UITypes.LinkToAnotherRecord &&
column.colOptions?.fk_related_model_id === childTable.id column.colOptions?.fk_related_model_id === childTable.id
); );
await createColumn(context, table, { const lookupColumn = await createColumn(context, table, {
title: title, title: title,
uidt: UITypes.Lookup, uidt: UITypes.Lookup,
fk_relation_column_id: ltarColumn?.id, fk_relation_column_id: ltarColumn?.id,
@ -266,11 +204,6 @@ const createLookupColumn = async (
column_name: title, column_name: title,
}); });
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const lookupColumn = (await table.getColumns()).find(
(column) => column.title === title
)!;
return lookupColumn; return lookupColumn;
}; };

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

@ -661,7 +661,7 @@ function tableTest() {
} }
}); });
it('Formula column on rollup customer table', async function () { it('Sorted Formula column on rollup customer table', async function () {
const rollupColumn = await createRollupColumn(context, { const rollupColumn = await createRollupColumn(context, {
project, project,
title: 'Number of rentals', title: 'Number of rentals',
@ -671,7 +671,7 @@ function tableTest() {
relatedTableColumnTitle: 'RentalDate', relatedTableColumnTitle: 'RentalDate',
}); });
await createColumn(context, customerTable, { const formulaColumn = await createColumn(context, customerTable, {
uidt: UITypes.Formula, uidt: UITypes.Formula,
title: 'Formula', title: 'Formula',
formula: `ADD({${rollupColumn.title}}, 10)`, formula: `ADD({${rollupColumn.title}}, 10)`,
@ -680,8 +680,19 @@ function tableTest() {
const response = await request(context.app) const response = await request(context.app)
.get(`/api/v1/db/data/noco/${project.id}/${customerTable.id}`) .get(`/api/v1/db/data/noco/${project.id}/${customerTable.id}`)
.set('xc-auth', context.token) .set('xc-auth', context.token)
.query({
sortArrJson: JSON.stringify([
{
fk_column_id: formulaColumn?.id,
direction: 'asc',
},
]),
})
.expect(200); .expect(200);
if (response.body.list[0][formulaColumn.title] !== 22)
throw new Error('Wrong sorting');
if ( if (
(response.body.list as Array<any>).every( (response.body.list as Array<any>).every(
(row) => row['Formula'] !== row[rollupColumn.title] + 10 (row) => row['Formula'] !== row[rollupColumn.title] + 10
@ -690,8 +701,55 @@ function tableTest() {
throw new Error('Wrong formula'); throw new Error('Wrong formula');
} }
}); });
// it.only('Get nested sorted filtered table with nested fields data list with a formula > lookup > rollup column in customer table', async function () {
// const rentalTable = await Model.getByIdOrName({
// project_id: project.id,
// base_id: project.bases[0].id,
// table_name: 'rental',
// });
// const rollupColumn = await createRollupColumn(context, {
// project,
// title: 'Number of rentals',
// rollupFunction: 'count',
// table: customerTable,
// relatedTableName: 'rental',
// relatedTableColumnTitle: 'RentalDate',
// });
// const lookupColumn = await createLookupColumn(context, {
// project,
// title: 'Lookup',
// table: rentalTable,
// relatedTableName: customerTable.table_name,
// relatedTableColumnTitle: rollupColumn.title,
// });
// const formulaColumn = await createColumn(context, rentalTable, {
// uidt: UITypes.Formula,
// title: 'Formula',
// formula: `ADD({${lookupColumn.title}}, 10)`,
// });
// console.log(formulaColumn);
// const response = await request(context.app)
// .get(`/api/v1/db/data/noco/${project.id}/${customerTable.id}`)
// .set('xc-auth', context.token)
// .query({
// sortArrJson: JSON.stringify([
// {
// fk_column_id: formulaColumn?.id,
// direction: 'asc',
// },
// ]),
// })
// .expect(200);
// console.log(response.body);
// });
} }
export default function () { export default function () {
describe('TableRow', tableTest); describe.only('TableRow', tableTest);
} }

Loading…
Cancel
Save