Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

add rollup on rollup unit test

Fendy Heryanto0a5a3fa7f360
diff --git a/packages/nocodb/tests/unit/factory/column.ts b/packages/nocodb/tests/unit/factory/column.ts
index 9f2f191b31..764a2dc091 100644
--- a/packages/nocodb/tests/unit/factory/column.ts
+++ b/packages/nocodb/tests/unit/factory/column.ts
@@ -980,6 +980,9 @@ const createRollupColumn = async (
relatedTableName: string;
relatedTableColumnTitle: string;
},
+ option?: {
+ throwError?: boolean;
+ },
) => {
const ctx = {
workspace_id: base.fk_workspace_id,
@@ -1004,15 +1007,20 @@ const createRollupColumn = async (
column.colOptions?.fk_related_model_id === childTable.id,
);
- const rollupColumn = await createColumn(context, table, {
- title: title,
- uidt: UITypes.Rollup,
- fk_relation_column_id: ltarColumn?.id,
- fk_rollup_column_id: childTableColumn?.id,
- rollup_function: rollupFunction,
- table_name: table.table_name,
- column_name: title,
- });
+ const rollupColumn = await createColumn(
+ context,
+ table,
+ {
+ title: title,
+ uidt: UITypes.Rollup,
+ fk_relation_column_id: ltarColumn?.id,
+ fk_rollup_column_id: childTableColumn?.id,
+ rollup_function: rollupFunction,
+ table_name: table.table_name,
+ column_name: title,
+ },
+ option,
+ );
return rollupColumn;
};
diff --git a/packages/nocodb/tests/unit/index.test.ts b/packages/nocodb/tests/unit/index.test.ts
index 6919e18f65..5b4a897a2f 100644
--- a/packages/nocodb/tests/unit/index.test.ts
+++ b/packages/nocodb/tests/unit/index.test.ts
@@ -7,6 +7,7 @@ import TestDbMngr from './TestDbMngr';
import { dataApiV3Test } from './rest/tests/dataApiV3/index.test';
import { processorTests } from './processor/index.test';
import { errorTests } from './error/index.test';
+import { rollupTests } from './rollup/index.test';
process.env.NODE_ENV = 'test';
process.env.TEST = 'true';
@@ -23,6 +24,7 @@ dotenv.config({
modelTests();
formulaTests();
+ rollupTests();
errorTests();
restTests();
processorTests();
diff --git a/packages/nocodb/tests/unit/rollup/index.test.ts b/packages/nocodb/tests/unit/rollup/index.test.ts
new file mode 100644
index 0000000000..c366afa558
--- /dev/null
+++ b/packages/nocodb/tests/unit/rollup/index.test.ts
@@ -0,0 +1,11 @@
+import 'mocha';
+import { rollupRollupTest } from './tests/rollup-rollup.test';
+
+function _rollupTests() {
+ rollupRollupTest();
+}
+
+// FIXME: run on set 2
+export function rollupTests() {
+ describe('Rollup', _rollupTests);
+}
diff --git a/packages/nocodb/tests/unit/rollup/initModel.ts b/packages/nocodb/tests/unit/rollup/initModel.ts
new file mode 100644
index 0000000000..838deb69f0
--- /dev/null
+++ b/packages/nocodb/tests/unit/rollup/initModel.ts
@@ -0,0 +1,281 @@
+import { expect } from 'chai';
+import request from 'supertest';
+
+import { UITypes } from 'nocodb-sdk';
+import { Model } from '../../../src/models';
+import Base from '../../../src/models/Base';
+import { createProject } from '../factory/base';
+import {
+ createLookupColumn,
+ createLtarColumn2,
+ createRollupColumn,
+ customColumns,
+} from '../factory/column';
+import { createBulkRows } from '../factory/row';
+import { createTable } from '../factory/table';
+import init from '../init';
+
+export interface ITestContext {
+ context: Awaited<ReturnType<typeof init>>;
+ ctx: {
+ workspace_id: any;
+ base_id: any;
+ };
+ base: Base;
+ tables: {
+ table1: Model;
+ table2: Model;
+ table3: Model;
+ table4: Model;
+ };
+}
+async function ncAxiosLinkAdd({
+ context,
+ urlParams,
+ body = {},
+ status = 200,
+ msg,
+}: {
+ context: { context: any; base: any };
+ urlParams: { tableId: string; linkId: string; rowId: string };
+ body?: any;
+ status?: number;
+ msg?: string;
+}) {
+ const url = `/api/v3/data/${context.base.id}/${urlParams.tableId}/links/${urlParams.linkId}/${urlParams.rowId}`;
+ const response = await request(context.context.app)
+ .post(url)
+ .set('xc-auth', context.context.token)
+ .send(body);
+
+ expect(response.status).to.equal(status);
+ if (msg) {
+ expect(response.body.message || response.body.msg).to.equal(msg);
+ }
+ return response;
+}
+
+const getRows = (tableName: string) => {
+ return Array.from({ length: 40 }).map((v, i) => {
+ return {
+ Title: `${tableName}_${(i + 1).toString().padStart(3, '0')}`,
+ };
+ });
+};
+
+const getDuplicatedRows = (tableName: string) => {
+ return Array.from({ length: 40 }).map((v, i) => {
+ return {
+ Title: `${tableName}_${((i + 1) % 5).toString().padStart(3, '0')}`,
+ };
+ });
+};
+
+/**
+ * Sets up the test environment with base, table, view, and BaseModelSql instance
+ * Used by: tests/unit/formula/tests/formula-lookup-ltar.test.ts
+ */
+export async function initInitialModel() {
+ console.time('#### formulaLookupLtarTests');
+ const context = await init();
+ const base = await createProject(context);
+
+ const ctx = {
+ workspace_id: base.fk_workspace_id,
+ base_id: base.id,
+ };
+
+ const columns = [
+ {
+ title: 'Title',
+ column_name: 'Title',
+ uidt: UITypes.SingleLineText,
+ pv: true,
+ },
+ ];
+ const table1 = await createTable(context, base, {
+ title: 'Table1',
+ table_name: 'table1',
+ columns: customColumns('custom', columns),
+ });
+ // insert records
+ await createBulkRows(context, {
+ base: base,
+ table: table1,
+ values: getRows('T1'),
+ });
+ const table2 = await createTable(context, base, {
+ title: 'Table2',
+ table_name: 'table2',
+ columns: customColumns('custom', columns),
+ });
+ // insert records
+ await createBulkRows(context, {
+ base: base,
+ table: table2,
+ values: getRows('T2'),
+ });
+ const table3 = await createTable(context, base, {
+ title: 'Table3',
+ table_name: 'table3',
+ columns: customColumns('custom', columns),
+ });
+ // insert records
+ await createBulkRows(context, {
+ base: base,
+ table: table3,
+ values: getRows('T3'),
+ });
+ const table4 = await createTable(context, base, {
+ title: 'Table4',
+ table_name: 'table4',
+ columns: customColumns('custom', columns),
+ });
+ // insert records
+ await createBulkRows(context, {
+ base: base,
+ table: table4,
+ values: getDuplicatedRows('T4'),
+ });
+
+ // Create links
+ const t3_HM_t2_Ltar = await createLtarColumn2(context, {
+ title: 'T2s',
+ parentTable: table3,
+ childTable: table2,
+ type: 'hm',
+ });
+ // Create links
+ const t3_HM_t4_Ltar = await createLtarColumn2(context, {
+ title: 'T4s',
+ parentTable: table3,
+ childTable: table4,
+ type: 'hm',
+ });
+ const t2_HM_t1_Ltar = await createLtarColumn2(context, {
+ title: 'T1s',
+ parentTable: table2,
+ childTable: table1,
+ type: 'hm',
+ });
+
+ const source = (await Base.getByTitleOrId(ctx, base.id)).getSources();
+ await createLookupColumn(context, {
+ base,
+ title: 'table1Name',
+ table: await Model.getByIdOrName(ctx, {
+ base_id: base.id,
+ source_id: source.id!,
+ id: table2.id,
+ }),
+ relatedTableName: table1.table_name,
+ relatedTableColumnTitle: 'Title',
+ relationColumnId: t2_HM_t1_Ltar.id,
+ });
+ await createLookupColumn(context, {
+ base,
+ title: 'table2Name',
+ table: await Model.getByIdOrName(ctx, {
+ base_id: base.id,
+ source_id: source.id!,
+ id: table3.id,
+ }),
+ relatedTableName: table2.table_name,
+ relatedTableColumnTitle: 'Title',
+ relationColumnId: t3_HM_t2_Ltar.id,
+ });
+ await createLookupColumn(context, {
+ base,
+ title: 'table2_table1s',
+ table: await Model.getByIdOrName(ctx, {
+ base_id: base.id,
+ source_id: source.id!,
+ id: table3.id,
+ }),
+ relatedTableName: table2.table_name,
+ relatedTableColumnTitle: 'T1s',
+ relationColumnId: t3_HM_t2_Ltar.id,
+ });
+
+ const linkTo_t2_HM_t1_Ltar = (rowId: string, body: any[]) => {
+ ncAxiosLinkAdd({
+ context: {
+ context,
+ base,
+ },
+ urlParams: {
+ tableId: table2.id,
+ linkId: t2_HM_t1_Ltar.id,
+ rowId: rowId,
+ },
+ body: body,
+ status: 200,
+ });
+ };
+ const linkTo_t3_HM_t2_Ltar = (rowId: string, body: any[]) => {
+ ncAxiosLinkAdd({
+ context: {
+ context,
+ base,
+ },
+ urlParams: {
+ tableId: table3.id,
+ linkId: t3_HM_t2_Ltar.id,
+ rowId: rowId,
+ },
+ body: body,
+ status: 200,
+ });
+ };
+ const linkTo_t3_HM_t4_Ltar = (rowId: string, body: any[]) => {
+ ncAxiosLinkAdd({
+ context: {
+ context,
+ base,
+ },
+ urlParams: {
+ tableId: table3.id,
+ linkId: t3_HM_t4_Ltar.id,
+ rowId: rowId,
+ },
+ body: body,
+ status: 200,
+ });
+ };
+ await linkTo_t2_HM_t1_Ltar('1', [{ id: 1 }, { id: 2 }, { id: 3 }]);
+ await linkTo_t2_HM_t1_Ltar('2', [{ id: 4 }, { id: 5 }, { id: 6 }]);
+ await linkTo_t2_HM_t1_Ltar('3', [{ id: 7 }]);
+ await linkTo_t3_HM_t2_Ltar('1', [{ id: 1 }, { id: 2 }]);
+ await linkTo_t3_HM_t2_Ltar('2', [{ id: 3 }]);
+ await linkTo_t3_HM_t4_Ltar(
+ '1',
+ Array.from({ length: 20 }).map((v, i) => ({ id: i + 1 })),
+ );
+
+ return {
+ context,
+ ctx,
+ base,
+ tables: {
+ table1,
+ table2,
+ table3,
+ table4,
+ },
+ } as ITestContext;
+}
+
+export async function initRollupColumns(context: ITestContext) {
+ const t1TitleCount = await createRollupColumn(context.context, {
+ base: context.base,
+ title: 'T1TitleCount',
+ rollupFunction: 'count',
+ table: context.tables.table2,
+ relatedTableName: context.tables.table1.table_name,
+ relatedTableColumnTitle: 'Title',
+ });
+
+ return {
+ t1TitleCount,
+ };
+}
diff --git a/packages/nocodb/tests/unit/rollup/tests/rollup-rollup.test.ts b/packages/nocodb/tests/unit/rollup/tests/rollup-rollup.test.ts
new file mode 100644
index 0000000000..e572160b83
--- /dev/null
+++ b/packages/nocodb/tests/unit/rollup/tests/rollup-rollup.test.ts
@@ -0,0 +1,53 @@
+import 'mocha';
+import { UITypes } from 'nocodb-sdk';
+import { expect } from 'chai';
+import { initInitialModel, initRollupColumns } from '../initModel';
+import { createColumn, createRollupColumn } from '../../factory/column';
+import { listRow } from '../../factory/row';
+
+function rollupRollupTests() {
+ let _setup;
+ let _context;
+ let _ctx: {
+ workspace_id: string;
+ base_id: string;
+ };
+ let _base;
+ let _tables;
+ let _view;
+ let _baseModelSql;
+
+ beforeEach(async function () {
+ const setup = await initInitialModel();
+ await initRollupColumns(setup);
+ _setup = setup;
+ _context = setup.context;
+ _ctx = setup.ctx;
+ _base = setup.base;
+ _tables = setup.tables;
+ });
+
+ it(`will create rollup on rollup column`, async () => {
+ const T2T1TitleSum = await createRollupColumn(
+ _context,
+ {
+ base: _base,
+ title: 'T2T1TitleSum',
+ rollupFunction: 'sum',
+ table: _tables.table3,
+ relatedTableName: _tables.table2.table_name,
+ relatedTableColumnTitle: 'T1TitleCount',
+ },
+ {
+ throwError: true,
+ },
+ );
+ expect(typeof T2T1TitleSum).to.eq(`object`);
+ const rows = await listRow({ base: _base, table: _tables.table3 });
+ expect(rows[0].T2T1TitleSum).to.greaterThan(0);
+ });
+}
+
+export function rollupRollupTest() {
+ describe('RollupRollupTest', rollupRollupTests);
+}