diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/helpers/column.ts b/packages/nocodb/src/__tests__/unit/rest/tests/helpers/column.ts index 54595806d0..dbfc8e23d2 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/helpers/column.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/helpers/column.ts @@ -1,5 +1,7 @@ // import { OracleUi, SqlUiFactory, UITypes } from 'nocodb-sdk'; +import { UITypes } from 'nocodb-sdk'; import request from 'supertest'; +import Model from '../../../../../lib/models/Model'; // import { dbConfig } from '../../dbConfig'; const defaultColumns = [ @@ -167,4 +169,54 @@ const createColumn = async (context, table, columnAttr) => { return response.body; }; -export { defaultColumns, createColumn }; +const createRollupColumn = async ( + context, + { + project, + title, + rollupFunction, + parentTable, + childTableName, + childTableColumnTitle, + }: { + project: any; + title: string; + rollupFunction: string; + parentTable: Model; + childTableName: string; + childTableColumnTitle: string; + } +) => { + const childTable = await Model.getByIdOrName({ + project_id: project.id, + base_id: project.bases[0].id, + table_name: childTableName, + }); + const childTableColumns = await childTable.getColumns(); + const childTableColumn = await childTableColumns.find( + (column) => column.title === childTableColumnTitle + ); + + const ltarColumn = (await parentTable.getColumns()).find( + (column) => + column.uidt === UITypes.LinkToAnotherRecord && + column.colOptions?.fk_related_model_id === childTable.id + ); + await createColumn(context, parentTable, { + title: title, + uidt: UITypes.Rollup, + fk_relation_column_id: ltarColumn?.id, + fk_rollup_column_id: childTableColumn?.id, + rollup_function: rollupFunction, + table_name: parentTable.table_name, + column_name: title, + }); + + const rollupColumn = (await parentTable.getColumns()).find( + (column) => column.title === title + ); + + return rollupColumn; +}; + +export { defaultColumns, createColumn, createRollupColumn }; 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 6d2e37b555..7472202fdd 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts @@ -4,6 +4,7 @@ import Model from '../../../../lib/models/Model'; import init from '../init'; import request from 'supertest'; import { ColumnType } from 'nocodb-sdk'; +import { createRollupColumn } from './helpers/column'; const isColumnsCorrectInResponse = (response, columns: ColumnType[]) => { const responseColumnsListStr = Object.keys(response.body.list[0]) @@ -42,8 +43,9 @@ function tableTest() { .set('xc-auth', context.token) .send({}) .expect(200); + const pageInfo = response.body.pageInfo; - if (response.body.list.length !== 25) { + if (response.body.list.length !== pageInfo.pageSize) { throw new Error('Wrong number of rows'); } @@ -62,8 +64,9 @@ function tableTest() { fields: requiredColumns.map((c) => c.title), }) .expect(200); + const pageInfo = response.body.pageInfo; - if (response.body.list.length !== 25) { + if (response.body.list.length !== pageInfo.pageSize) { throw new Error('Wrong number of rows'); } @@ -89,7 +92,7 @@ function tableTest() { .expect(200); const pageInfo = response.body.pageInfo; - if (response.body.list.length !== 25) { + if (response.body.list.length !== pageInfo.pageSize) { throw new Error('Wrong number of rows'); } @@ -142,7 +145,7 @@ function tableTest() { .expect(200); const pageInfo = response.body.pageInfo; - if (response.body.list.length !== 25) { + if (response.body.list.length !== pageInfo.pageSize) { throw new Error('Wrong number of rows'); } @@ -177,6 +180,43 @@ function tableTest() { throw new Error('Wrong sort on last page'); } }); + + it('Get sorted table data list with a rollup column', async function () { + const rollupColumnName = 'Number of rentals'; + + const rollupColumn = await createRollupColumn(context, { + project, + title: rollupColumnName, + rollupFunction: 'count', + parentTable: customerTable, + childTableName: 'rental', + childTableColumnTitle: 'RentalDate', + }); + + const ascResponse = 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: rollupColumn?.id, direction: 'asc' }, + ]), + }) + .expect(200); + if (ascResponse.body.list[0]['FirstName'] !== 'BRIAN') + throw new Error('Wrong sort'); + + const descResponse = 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: rollupColumn?.id, direction: 'desc' }, + ]), + }) + .expect(200); + if (descResponse.body.list[0]['FirstName'] !== 'ELEANOR') + throw new Error('Wrong sort'); + }); } export default function () {