From ba744ba4b03375b4137d4341608a9e97306036ca Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Thu, 1 Sep 2022 19:39:53 +0530 Subject: [PATCH] refactor/Minor refactor and added/commented a test for tableRow --- .../unit/rest/tests/factory/column.ts | 87 +++---------------- .../unit/rest/tests/tableRow.test.ts | 64 +++++++++++++- 2 files changed, 71 insertions(+), 80 deletions(-) diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/factory/column.ts b/packages/nocodb/src/__tests__/unit/rest/tests/factory/column.ts index 5337cbd31e..194fb5e4aa 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/factory/column.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/factory/column.ts @@ -1,8 +1,6 @@ -// import { OracleUi, SqlUiFactory, UITypes } from 'nocodb-sdk'; -import { UITypes } from 'nocodb-sdk'; +import { ColumnType, UITypes } from 'nocodb-sdk'; import request from 'supertest'; import Model from '../../../../../lib/models/Model'; -// import { dbConfig } from '../../dbConfig'; const defaultColumns = [ { @@ -99,74 +97,18 @@ const defaultColumns = [ }, ]; -// const sqlUI = SqlUiFactory.create({ client: dbConfig.client }) as Exclude< -// ReturnType, -// 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 response = await request(context.app) + await request(context.app) .post(`/api/v1/db/meta/tables/${table.id}/columns`) .set('xc-auth', context.token) .send({ ...columnAttr, }); - return response.body; + + const column: ColumnType = (await table.getColumns()).find( + (column) => column.title === columnAttr.title + ); + return column; }; const createRollupColumn = async ( @@ -202,7 +144,8 @@ const createRollupColumn = async ( column.uidt === UITypes.LinkToAnotherRecord && column.colOptions?.fk_related_model_id === childTable.id ); - await createColumn(context, table, { + + const rollupColumn = await createColumn(context, table, { title: title, uidt: UITypes.Rollup, fk_relation_column_id: ltarColumn?.id, @@ -212,11 +155,6 @@ const createRollupColumn = async ( 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; }; @@ -257,7 +195,7 @@ const createLookupColumn = async ( column.uidt === UITypes.LinkToAnotherRecord && column.colOptions?.fk_related_model_id === childTable.id ); - await createColumn(context, table, { + const lookupColumn = await createColumn(context, table, { title: title, uidt: UITypes.Lookup, fk_relation_column_id: ltarColumn?.id, @@ -266,11 +204,6 @@ const createLookupColumn = async ( 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; }; diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts b/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts index c98f23c08c..29dfff2f31 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts +++ b/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, { project, title: 'Number of rentals', @@ -671,7 +671,7 @@ function tableTest() { relatedTableColumnTitle: 'RentalDate', }); - await createColumn(context, customerTable, { + const formulaColumn = await createColumn(context, customerTable, { uidt: UITypes.Formula, title: 'Formula', formula: `ADD({${rollupColumn.title}}, 10)`, @@ -680,8 +680,19 @@ function tableTest() { 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); + if (response.body.list[0][formulaColumn.title] !== 22) + throw new Error('Wrong sorting'); + if ( (response.body.list as Array).every( (row) => row['Formula'] !== row[rollupColumn.title] + 10 @@ -690,8 +701,55 @@ function tableTest() { 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 () { - describe('TableRow', tableTest); + describe.only('TableRow', tableTest); }