Skip to content

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

add tests for cross base links

Fendy Heryanto64a39fe31fc9
diff --git a/packages/nocodb/tests/unit/crossBaseLink/crud.spec.ts b/packages/nocodb/tests/unit/crossBaseLink/crud.spec.ts
new file mode 100644
index 0000000000..86887e676b
--- /dev/null
+++ b/packages/nocodb/tests/unit/crossBaseLink/crud.spec.ts
@@ -0,0 +1,356 @@
+import 'mocha';
+import { expect } from 'chai';
+import request from 'supertest';
+
+import { initCrossBaseModel, ncAxiosLinkAdd } from './init';
+import type { ICrossBaseTestContext } from './init';
+
+/**
+ * Helper to get a record and check its link count field
+ * Uses the records API instead of links API for cross-base link verification
+ * Note: Cross-base link GET API has a backend limitation where it uses the wrong
+ * context when loading related model columns, causing errors.
+ */
+async function ncAxiosRecordGet({
+ context,
+ tableId,
+ rowId,
+ status = 200,
+}: {
+ context: { context: any; base: any };
+ tableId: string;
+ rowId: string;
+ status?: number;
+}) {
+ const url = `/api/v3/data/${context.base.id}/${tableId}/records/${rowId}`;
+ const response = await request(context.context.app)
+ .get(url)
+ .set('xc-auth', context.context.token);
+ expect(response.status).to.equal(status);
+ return response;
+}
+
+async function ncAxiosLinkRemove({
+ context,
+ urlParams,
+ body = {},
+ status = 200,
+}: {
+ context: { context: any; base: any };
+ urlParams: { tableId: string; linkId: string; rowId: string };
+ body?: any;
+ status?: number;
+}) {
+ const url = `/api/v3/data/${context.base.id}/${urlParams.tableId}/links/${urlParams.linkId}/${urlParams.rowId}`;
+ const response = await request(context.context.app)
+ .delete(url)
+ .set('xc-auth', context.context.token)
+ .send(body);
+ expect(response.status).to.equal(status);
+ return response;
+}
+
+function crossBaseCrudTests() {
+ let _setup: ICrossBaseTestContext;
+ let _context: ICrossBaseTestContext['context'];
+ let _bases: ICrossBaseTestContext['bases'];
+ let _tables: ICrossBaseTestContext['tables'];
+ let _crossLinks: ICrossBaseTestContext['crossLinks'];
+
+ beforeEach(async function () {
+ const setup = await initCrossBaseModel();
+ _setup = setup;
+ _context = setup.context;
+ _bases = setup.bases;
+ _tables = setup.tables;
+ _crossLinks = setup.crossLinks;
+ });
+
+ describe('Create Cross-Base Links', () => {
+ it('should create HM link across bases', async () => {
+ // Link row 4 in base1.table1 to rows 10,11 in base2.table1
+ const response = await ncAxiosLinkAdd({
+ context: { context: _context, base: _bases.base1 },
+ urlParams: {
+ tableId: _tables.t1_base1.id,
+ linkId: _crossLinks.b1t1_HM_b2t1.id,
+ rowId: '4',
+ },
+ body: [{ id: 10 }, { id: 11 }],
+ status: 200,
+ });
+
+ expect(response.status).to.equal(200);
+
+ // Verify links were created by checking the record's link count
+ const getResponse = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t1_base1.id,
+ rowId: '4',
+ });
+
+ // The link column should have 2 linked records
+ const linkField = getResponse.body.fields['B2T1s'];
+ expect(linkField).to.be.an('array');
+ expect(linkField.length).to.equal(2);
+ });
+
+ it('should create MM link across bases', async () => {
+ // Link row 5 in base1.table1 to rows 5,6 in base2.table2
+ const response = await ncAxiosLinkAdd({
+ context: { context: _context, base: _bases.base1 },
+ urlParams: {
+ tableId: _tables.t1_base1.id,
+ linkId: _crossLinks.b1t1_MM_b2t2.id,
+ rowId: '5',
+ },
+ body: [{ id: 5 }, { id: 6 }],
+ status: 200,
+ });
+
+ expect(response.status).to.equal(200);
+
+ // Verify links were created by checking the record's link count
+ const getResponse = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t1_base1.id,
+ rowId: '5',
+ });
+
+ // The link column should have 2 linked records
+ const linkField = getResponse.body.fields['B2T2sMM'];
+ expect(linkField).to.be.an('array');
+ expect(linkField.length).to.equal(2);
+ });
+
+ it('should create OO link across bases', async () => {
+ // Link row 3 in base1.table2 to row 9 in base2.table1
+ const response = await ncAxiosLinkAdd({
+ context: { context: _context, base: _bases.base1 },
+ urlParams: {
+ tableId: _tables.t2_base1.id,
+ linkId: _crossLinks.b1t2_OO_b2t1.id,
+ rowId: '3',
+ },
+ body: [{ id: 9 }],
+ status: 200,
+ });
+
+ expect(response.status).to.equal(200);
+
+ // Verify link was created by checking the record's link field
+ const getResponse = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t2_base1.id,
+ rowId: '3',
+ });
+
+ // For OO links, the field contains the linked record's display value
+ expect(getResponse.body.fields['B2T1OO']).to.exist;
+ });
+ });
+
+ describe('Read Cross-Base Links', () => {
+ it('should read linked records count from other base via HM', async () => {
+ // Row 1 in base1.table1 was linked to rows 1,2,3 in base2.table1 during setup
+ // Verify via record API since link GET API has cross-base context issues
+ const response = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t1_base1.id,
+ rowId: '1',
+ });
+
+ // The HM link should have 3 linked records
+ const linkField = response.body.fields['B2T1s'];
+ expect(linkField).to.be.an('array');
+ expect(linkField.length).to.equal(3);
+ });
+
+ it('should read linked records count from other base via MM', async () => {
+ // Row 1 in base1.table1 was linked to rows 1,2 in base2.table2 during setup
+ const response = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t1_base1.id,
+ rowId: '1',
+ });
+
+ // The MM link should have 2 linked records
+ const linkField = response.body.fields['B2T2sMM'];
+ expect(linkField).to.be.an('array');
+ expect(linkField.length).to.equal(2);
+ });
+
+ it('should read linked record from other base via OO', async () => {
+ // Row 1 in base1.table2 was linked to row 7 in base2.table1 during setup
+ const response = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t2_base1.id,
+ rowId: '1',
+ });
+
+ // OO link should have a value (the display value of the linked record)
+ expect(response.body.fields['B2T1OO']).to.exist;
+ });
+
+ it('should verify multiple links can be created for pagination', async () => {
+ // Create more links for pagination test
+ const addResponse = await ncAxiosLinkAdd({
+ context: { context: _context, base: _bases.base1 },
+ urlParams: {
+ tableId: _tables.t1_base1.id,
+ linkId: _crossLinks.b1t1_HM_b2t1.id,
+ rowId: '10',
+ },
+ body: Array.from({ length: 15 }, (_, i) => ({ id: i + 20 })),
+ status: 200,
+ });
+
+ expect(addResponse.status).to.equal(200);
+
+ // Verify links were created by checking record
+ const getResponse = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t1_base1.id,
+ rowId: '10',
+ });
+
+ const linkField = getResponse.body.fields['B2T1s'];
+ expect(linkField).to.be.an('array');
+ expect(linkField.length).to.equal(15);
+ });
+ });
+
+ describe('Update/Delete Cross-Base Links', () => {
+ it('should unlink cross-base records', async () => {
+ // First verify row 1 has 3 linked records via record API
+ const beforeResponse = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t1_base1.id,
+ rowId: '1',
+ });
+ const beforeLinkField = beforeResponse.body.fields['B2T1s'];
+ expect(beforeLinkField).to.be.an('array');
+ expect(beforeLinkField.length).to.equal(3);
+
+ // Unlink one record
+ await ncAxiosLinkRemove({
+ context: { context: _context, base: _bases.base1 },
+ urlParams: {
+ tableId: _tables.t1_base1.id,
+ linkId: _crossLinks.b1t1_HM_b2t1.id,
+ rowId: '1',
+ },
+ body: [{ id: 1 }],
+ status: 200,
+ });
+
+ // Verify link was removed
+ const afterResponse = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t1_base1.id,
+ rowId: '1',
+ });
+ const afterLinkField = afterResponse.body.fields['B2T1s'];
+ expect(afterLinkField).to.be.an('array');
+ expect(afterLinkField.length).to.equal(2);
+ });
+
+ it('should unlink MM cross-base records', async () => {
+ // First verify row 2 has 3 linked records (linked during setup)
+ const beforeResponse = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t1_base1.id,
+ rowId: '2',
+ });
+ const beforeLinkField = beforeResponse.body.fields['B2T2sMM'];
+ expect(beforeLinkField).to.be.an('array');
+ expect(beforeLinkField.length).to.equal(3);
+
+ // Unlink one record
+ await ncAxiosLinkRemove({
+ context: { context: _context, base: _bases.base1 },
+ urlParams: {
+ tableId: _tables.t1_base1.id,
+ linkId: _crossLinks.b1t1_MM_b2t2.id,
+ rowId: '2',
+ },
+ body: [{ id: 2 }],
+ status: 200,
+ });
+
+ // Verify link was removed
+ const afterResponse = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t1_base1.id,
+ rowId: '2',
+ });
+ const afterLinkField = afterResponse.body.fields['B2T2sMM'];
+ expect(afterLinkField).to.be.an('array');
+ expect(afterLinkField.length).to.equal(2);
+ });
+
+ it('should replace OO cross-base link', async () => {
+ // Row 1 in base1.table2 is linked to row 7 in base2.table1
+ // Verify initial link exists
+ const beforeResponse = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t2_base1.id,
+ rowId: '1',
+ });
+ expect(beforeResponse.body.fields['B2T1OO']).to.exist;
+
+ // Replace with row 10
+ await ncAxiosLinkAdd({
+ context: { context: _context, base: _bases.base1 },
+ urlParams: {
+ tableId: _tables.t2_base1.id,
+ linkId: _crossLinks.b1t2_OO_b2t1.id,
+ rowId: '1',
+ },
+ body: [{ id: 10 }],
+ status: 200,
+ });
+
+ // Verify link was replaced (OO should have the new link's display value)
+ const afterResponse = await ncAxiosRecordGet({
+ context: { context: _context, base: _bases.base1 },
+ tableId: _tables.t2_base1.id,
+ rowId: '1',
+ });
+
+ // The link field should still exist with a value
+ expect(afterResponse.body.fields['B2T1OO']).to.exist;
+ });
+ });
+
+ describe('Cross-Base Link Metadata', () => {
+ it('should have fk_related_base_id set for cross-base HM link', async () => {
+ const ctx1 = _setup.contexts.ctx1;
+ const column = await _crossLinks.b1t1_HM_b2t1.getColOptions(ctx1);
+
+ expect(column.fk_related_base_id).to.exist;
+ expect(column.fk_related_base_id).to.equal(_bases.base2.id);
+ });
+
+ it('should have fk_related_base_id set for cross-base MM link', async () => {
+ const ctx1 = _setup.contexts.ctx1;
+ const column = await _crossLinks.b1t1_MM_b2t2.getColOptions(ctx1);
+
+ expect(column.fk_related_base_id).to.exist;
+ expect(column.fk_related_base_id).to.equal(_bases.base2.id);
+ expect(column.fk_mm_base_id).to.exist;
+ });
+
+ it('should have fk_related_base_id set for cross-base OO link', async () => {
+ const ctx1 = _setup.contexts.ctx1;
+ const column = await _crossLinks.b1t2_OO_b2t1.getColOptions(ctx1);
+
+ expect(column.fk_related_base_id).to.exist;
+ expect(column.fk_related_base_id).to.equal(_bases.base2.id);
+ });
+ });
+}
+
+export function crossBaseCrudTest() {
+ describe('CrossBaseCrudTest', crossBaseCrudTests);
+}
diff --git a/packages/nocodb/tests/unit/crossBaseLink/formula.spec.ts b/packages/nocodb/tests/unit/crossBaseLink/formula.spec.ts
new file mode 100644
index 0000000000..153e4c5aab
--- /dev/null
+++ b/packages/nocodb/tests/unit/crossBaseLink/formula.spec.ts
@@ -0,0 +1,374 @@
+import 'mocha';
+import { expect } from 'chai';
+
+import { UITypes } from 'nocodb-sdk';
+import { createColumn } from '../factory/column';
+import { listRow } from '../factory/row';
+import { initCrossBaseModel } from './init';
+import type { ICrossBaseTestContext } from './init';
+
+/**
+ * Creates a lookup column for cross-base links
+ */
+async function createCrossBaseLookupColumn(
+ context: ICrossBaseTestContext['context'],
+ {
+ table,
+ title,
+ relationColumnId,
+ lookupColumnId,
+ }: {
+ table: any;
+ title: string;
+ relationColumnId: string;
+ lookupColumnId: string;
+ },
+) {
+ const lookupColumn = await createColumn(context, table, {
+ title: title,
+ uidt: UITypes.Lookup,
+ fk_relation_column_id: relationColumnId,
+ fk_lookup_column_id: lookupColumnId,
+ table_name: table.table_name,
+ column_name: title,
+ });
+
+ return lookupColumn;
+}
+
+/**
+ * Creates a rollup column for cross-base links
+ */
+async function createCrossBaseRollupColumn(
+ context: ICrossBaseTestContext['context'],
+ {
+ table,
+ title,
+ relationColumnId,
+ rollupColumnId,
+ rollupFunction,
+ }: {
+ table: any;
+ title: string;
+ relationColumnId: string;
+ rollupColumnId: string;
+ rollupFunction: string;
+ },
+) {
+ const rollupColumn = await createColumn(context, table, {
+ title: title,
+ uidt: UITypes.Rollup,
+ fk_relation_column_id: relationColumnId,
+ fk_rollup_column_id: rollupColumnId,
+ rollup_function: rollupFunction,
+ table_name: table.table_name,
+ column_name: title,
+ });
+
+ return rollupColumn;
+}
+
+function crossBaseFormulaTests() {
+ let _context: ICrossBaseTestContext['context'];
+ let _bases: ICrossBaseTestContext['bases'];
+ let _tables: ICrossBaseTestContext['tables'];
+ let _crossLinks: ICrossBaseTestContext['crossLinks'];
+ let _contexts: ICrossBaseTestContext['contexts'];
+
+ beforeEach(async function () {
+ const setup = await initCrossBaseModel();
+ _context = setup.context;
+ _bases = setup.bases;
+ _tables = setup.tables;
+ _crossLinks = setup.crossLinks;
+ _contexts = setup.contexts;
+ });
+
+ describe('Formula with Cross-Base Lookup', () => {
+ it('should create formula referencing cross-base lookup', async () => {
+ // First create a lookup column
+ const base2Table1Columns = await _tables.t1_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const titleColumn = base2Table1Columns.find(
+ (col) => col.title === 'Title',
+ );
+
+ await createCrossBaseLookupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'CrossBaseLookup',
+ relationColumnId: _crossLinks.b1t1_HM_b2t1.id,
+ lookupColumnId: titleColumn.id,
+ });
+
+ // Create formula referencing the lookup
+ const formulaCol = await createColumn(_context, _tables.t1_base1, {
+ title: 'FormulaOnLookup',
+ uidt: UITypes.Formula,
+ formula: 'CONCAT({CrossBaseLookup}, " - computed")',
+ formula_raw: 'CONCAT({CrossBaseLookup}, " - computed")',
+ });
+
+ expect(formulaCol).to.exist;
+ expect(formulaCol.title).to.equal('FormulaOnLookup');
+
+ // Get rows and verify formula values
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(row1.FormulaOnLookup).to.exist;
+ expect(row1.FormulaOnLookup).to.include(' - computed');
+ });
+
+ it('should create formula with CONCAT on cross-base lookup', async () => {
+ // Create lookup column
+ const base2Table1Columns = await _tables.t1_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const titleColumn = base2Table1Columns.find(
+ (col) => col.title === 'Title',
+ );
+
+ await createCrossBaseLookupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'LookupForConcat',
+ relationColumnId: _crossLinks.b1t1_HM_b2t1.id,
+ lookupColumnId: titleColumn.id,
+ });
+
+ // Create formula with CONCAT
+ const formulaCol = await createColumn(_context, _tables.t1_base1, {
+ title: 'ConcatFormula',
+ uidt: UITypes.Formula,
+ formula: 'CONCAT("Links: ", {LookupForConcat})',
+ formula_raw: 'CONCAT("Links: ", {LookupForConcat})',
+ });
+
+ expect(formulaCol).to.exist;
+
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(row1.ConcatFormula).to.include('Links: ');
+ });
+ });
+
+ describe('Formula with Cross-Base Rollup', () => {
+ it('should create formula with arithmetic on cross-base rollup', async () => {
+ // Create rollup column counting linked records
+ const base2Table1Columns = await _tables.t1_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const titleColumn = base2Table1Columns.find(
+ (col) => col.title === 'Title',
+ );
+
+ await createCrossBaseRollupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'LinkCount',
+ relationColumnId: _crossLinks.b1t1_HM_b2t1.id,
+ rollupColumnId: titleColumn.id,
+ rollupFunction: 'count',
+ });
+
+ // Create formula doubling the count
+ const formulaCol = await createColumn(_context, _tables.t1_base1, {
+ title: 'DoubledCount',
+ uidt: UITypes.Formula,
+ formula: '{LinkCount} * 2',
+ formula_raw: '{LinkCount} * 2',
+ });
+
+ expect(formulaCol).to.exist;
+
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ // Row 1 has 3 links, so doubled should be 6
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(Number(row1.DoubledCount)).to.equal(6);
+
+ // Row 2 has 2 links, so doubled should be 4
+ const row2 = rows.find((r: any) => r.Id === 2);
+ expect(Number(row2.DoubledCount)).to.equal(4);
+ });
+
+ it('should create formula with SUM rollup from cross-base', async () => {
+ // Get Amount column from base2.table2
+ const base2Table2Columns = await _tables.t2_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const amountColumn = base2Table2Columns.find(
+ (col) => col.title === 'Amount',
+ );
+
+ // Create rollup summing Amount
+ await createCrossBaseRollupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'TotalAmount',
+ relationColumnId: _crossLinks.b1t1_MM_b2t2.id,
+ rollupColumnId: amountColumn.id,
+ rollupFunction: 'sum',
+ });
+
+ // Create formula adding 100 to the sum
+ const formulaCol = await createColumn(_context, _tables.t1_base1, {
+ title: 'AmountPlus100',
+ uidt: UITypes.Formula,
+ formula: '{TotalAmount} + 100',
+ formula_raw: '{TotalAmount} + 100',
+ });
+
+ expect(formulaCol).to.exist;
+
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ // Row 1: sum(10, 20) = 30, plus 100 = 130
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(Number(row1.AmountPlus100)).to.equal(130);
+
+ // Row 2: sum(20, 30, 40) = 90, plus 100 = 190
+ const row2 = rows.find((r: any) => r.Id === 2);
+ expect(Number(row2.AmountPlus100)).to.equal(190);
+ });
+
+ it('should create formula with IF condition on cross-base rollup', async () => {
+ // Create rollup column
+ const base2Table1Columns = await _tables.t1_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const titleColumn = base2Table1Columns.find(
+ (col) => col.title === 'Title',
+ );
+
+ await createCrossBaseRollupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'CountForIF',
+ relationColumnId: _crossLinks.b1t1_HM_b2t1.id,
+ rollupColumnId: titleColumn.id,
+ rollupFunction: 'count',
+ });
+
+ // Create formula with IF condition
+ const formulaCol = await createColumn(_context, _tables.t1_base1, {
+ title: 'HasManyLinks',
+ uidt: UITypes.Formula,
+ formula: 'IF({CountForIF} > 2, "Many", "Few")',
+ formula_raw: 'IF({CountForIF} > 2, "Many", "Few")',
+ });
+
+ expect(formulaCol).to.exist;
+
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ // Row 1 has 3 links (> 2) -> "Many"
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(row1.HasManyLinks).to.equal('Many');
+
+ // Row 2 has 2 links (not > 2) -> "Few"
+ const row2 = rows.find((r: any) => r.Id === 2);
+ expect(row2.HasManyLinks).to.equal('Few');
+
+ // Row 3 has 1 link (not > 2) -> "Few"
+ const row3 = rows.find((r: any) => r.Id === 3);
+ expect(row3.HasManyLinks).to.equal('Few');
+ });
+ });
+
+ describe('Combined Formula with Lookup and Rollup', () => {
+ it('should create formula combining local column and cross-base rollup', async () => {
+ // Create rollup column
+ const base2Table2Columns = await _tables.t2_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const amountColumn = base2Table2Columns.find(
+ (col) => col.title === 'Amount',
+ );
+
+ await createCrossBaseRollupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'SumForCombined',
+ relationColumnId: _crossLinks.b1t1_MM_b2t2.id,
+ rollupColumnId: amountColumn.id,
+ rollupFunction: 'sum',
+ });
+
+ // Create formula combining Title and rollup
+ const formulaCol = await createColumn(_context, _tables.t1_base1, {
+ title: 'CombinedFormula',
+ uidt: UITypes.Formula,
+ formula: 'CONCAT({Title}, " - Total: ", {SumForCombined})',
+ formula_raw: 'CONCAT({Title}, " - Total: ", {SumForCombined})',
+ });
+
+ expect(formulaCol).to.exist;
+
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ // Row 1: "B1T1_001 - Total: 30"
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(row1.CombinedFormula).to.include('B1T1_001');
+ expect(row1.CombinedFormula).to.include('Total:');
+ });
+ });
+
+ describe('Formula Edge Cases', () => {
+ it('should handle formula on unlinked rows', async () => {
+ // Create rollup column
+ const base2Table1Columns = await _tables.t1_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const titleColumn = base2Table1Columns.find(
+ (col) => col.title === 'Title',
+ );
+
+ await createCrossBaseRollupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'CountForEdge',
+ relationColumnId: _crossLinks.b1t1_HM_b2t1.id,
+ rollupColumnId: titleColumn.id,
+ rollupFunction: 'count',
+ });
+
+ // Create formula
+ const formulaCol = await createColumn(_context, _tables.t1_base1, {
+ title: 'EdgeFormula',
+ uidt: UITypes.Formula,
+ formula: '{CountForEdge} + 1',
+ formula_raw: '{CountForEdge} + 1',
+ });
+
+ expect(formulaCol).to.exist;
+
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ // Row 10 has no links, count should be 0, formula should be 1
+ const row10 = rows.find((r: any) => r.Id === 10);
+ expect(Number(row10.EdgeFormula)).to.equal(1);
+ });
+ });
+}
+
+export function crossBaseFormulaTest() {
+ describe('CrossBaseFormulaTest', crossBaseFormulaTests);
+}
diff --git a/packages/nocodb/tests/unit/crossBaseLink/index.spec.ts b/packages/nocodb/tests/unit/crossBaseLink/index.spec.ts
new file mode 100644
index 0000000000..a3736153da
--- /dev/null
+++ b/packages/nocodb/tests/unit/crossBaseLink/index.spec.ts
@@ -0,0 +1,18 @@
+import 'mocha';
+import { runOnSet } from '../utils/runOnSet';
+import { isEE } from '../utils/helpers';
+import { crossBaseCrudTest } from './crud.spec';
+import { crossBaseLookupRollupTest } from './lookup-rollup.spec';
+import { crossBaseFormulaTest } from './formula.spec';
+
+function _crossBaseLinkTests() {
+ crossBaseCrudTest();
+ crossBaseLookupRollupTest();
+ crossBaseFormulaTest();
+}
+
+export const crossBaseLinkTests = runOnSet(2, function () {
+ if (isEE()) {
+ describe('Cross Base Links', _crossBaseLinkTests);
+ }
+});
diff --git a/packages/nocodb/tests/unit/crossBaseLink/init.ts b/packages/nocodb/tests/unit/crossBaseLink/init.ts
new file mode 100644
index 0000000000..9b52338163
--- /dev/null
+++ b/packages/nocodb/tests/unit/crossBaseLink/init.ts
@@ -0,0 +1,307 @@
+import { expect } from 'chai';
+import request from 'supertest';
+
+import { UITypes } from 'nocodb-sdk';
+import { createProject } from '../factory/base';
+import { createColumn, customColumns } from '../factory/column';
+import { createBulkRows } from '../factory/row';
+import { createTable } from '../factory/table';
+import init from '../init';
+import type Base from '../../../src/models/Base';
+import type { Model } from '../../../src/models';
+import type { Column } from '../../../src/models';
+
+export interface ICrossBaseTestContext {
+ context: Awaited<ReturnType<typeof init>>;
+ bases: {
+ base1: Base;
+ base2: Base;
+ };
+ contexts: {
+ ctx1: { workspace_id: string; base_id: string };
+ ctx2: { workspace_id: string; base_id: string };
+ };
+ tables: {
+ t1_base1: Model;
+ t2_base1: Model;
+ t1_base2: Model;
+ t2_base2: Model;
+ };
+ crossLinks: {
+ b1t1_HM_b2t1: Column;
+ b1t1_MM_b2t2: Column;
+ b1t2_OO_b2t1: Column;
+ b2t1_BT_b1t1: Column;
+ };
+}
+
+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((_, i) => {
+ return {
+ Title: `${tableName}_${(i + 1).toString().padStart(3, '0')}`,
+ };
+ });
+};
+
+const getNumberRows = (tableName: string) => {
+ return Array.from({ length: 40 }).map((_, i) => {
+ return {
+ Title: `${tableName}_${(i + 1).toString().padStart(3, '0')}`,
+ Amount: (i + 1) * 10,
+ };
+ });
+};
+
+/**
+ * Creates a cross-base LTAR column by specifying ref_base_id
+ */
+const createCrossBaseLtarColumn = async (
+ context: Awaited<ReturnType<typeof init>>,
+ {
+ title,
+ parentTable,
+ childTable,
+ refBaseId,
+ type,
+ }: {
+ title: string;
+ parentTable: Model;
+ childTable: Model;
+ refBaseId: string;
+ type: string;
+ },
+) => {
+ const ltarColumn = await createColumn(context, parentTable, {
+ title: title,
+ column_name: title,
+ uidt: UITypes.LinkToAnotherRecord,
+ parentId: parentTable.id,
+ childId: childTable.id,
+ ref_base_id: refBaseId,
+ type: type,
+ });
+
+ return ltarColumn;
+};
+
+/**
+ * Sets up the test environment with two bases and cross-base links
+ * Used by: tests/unit/crossBaseLink tests
+ */
+export async function initCrossBaseModel(): Promise<ICrossBaseTestContext> {
+ console.time('#### crossBaseLinkTests');
+ const context = await init();
+
+ // Create two bases in the same workspace
+ const base1 = await createProject(context, { title: 'CrossBaseTest1' });
+ const base2 = await createProject(context, { title: 'CrossBaseTest2' });
+
+ const ctx1 = {
+ workspace_id: base1.fk_workspace_id,
+ base_id: base1.id,
+ };
+
+ const ctx2 = {
+ workspace_id: base2.fk_workspace_id,
+ base_id: base2.id,
+ };
+
+ // Define columns for tables
+ const titleColumns = [
+ {
+ title: 'Title',
+ column_name: 'Title',
+ uidt: UITypes.SingleLineText,
+ pv: true,
+ },
+ ];
+
+ const numberColumns = [
+ {
+ title: 'Title',
+ column_name: 'Title',
+ uidt: UITypes.SingleLineText,
+ pv: true,
+ },
+ {
+ title: 'Amount',
+ column_name: 'Amount',
+ uidt: UITypes.Number,
+ },
+ ];
+
+ // Create tables in base1
+ const t1_base1 = await createTable(context, base1, {
+ title: 'Table1',
+ table_name: 'table1',
+ columns: customColumns('custom', titleColumns),
+ });
+ await createBulkRows(context, {
+ base: base1,
+ table: t1_base1,
+ values: getRows('B1T1'),
+ });
+
+ const t2_base1 = await createTable(context, base1, {
+ title: 'Table2',
+ table_name: 'table2',
+ columns: customColumns('custom', titleColumns),
+ });
+ await createBulkRows(context, {
+ base: base1,
+ table: t2_base1,
+ values: getRows('B1T2'),
+ });
+
+ // Create tables in base2
+ const t1_base2 = await createTable(context, base2, {
+ title: 'Table1',
+ table_name: 'table1',
+ columns: customColumns('custom', titleColumns),
+ });
+ await createBulkRows(context, {
+ base: base2,
+ table: t1_base2,
+ values: getRows('B2T1'),
+ });
+
+ const t2_base2 = await createTable(context, base2, {
+ title: 'Table2',
+ table_name: 'table2',
+ columns: customColumns('custom', numberColumns),
+ });
+ await createBulkRows(context, {
+ base: base2,
+ table: t2_base2,
+ values: getNumberRows('B2T2'),
+ });
+
+ // Create cross-base HM link: base1.table1 -> base2.table1
+ const b1t1_HM_b2t1 = await createCrossBaseLtarColumn(context, {
+ title: 'B2T1s',
+ parentTable: t1_base1,
+ childTable: t1_base2,
+ refBaseId: base2.id,
+ type: 'hm',
+ });
+
+ // Get the BT column created on base2.table1 (reverse of HM)
+ const b2t1_BT_b1t1 = (await t1_base2.getColumns(ctx2)).find(
+ (col) =>
+ col.uidt === UITypes.LinkToAnotherRecord &&
+ col.colOptions?.fk_related_model_id === t1_base1.id,
+ );
+
+ // Create cross-base MM link: base1.table1 <-> base2.table2
+ const b1t1_MM_b2t2 = await createCrossBaseLtarColumn(context, {
+ title: 'B2T2sMM',
+ parentTable: t1_base1,
+ childTable: t2_base2,
+ refBaseId: base2.id,
+ type: 'mm',
+ });
+
+ // Create cross-base OO link: base1.table2 -> base2.table1
+ const b1t2_OO_b2t1 = await createCrossBaseLtarColumn(context, {
+ title: 'B2T1OO',
+ parentTable: t2_base1,
+ childTable: t1_base2,
+ refBaseId: base2.id,
+ type: 'oo',
+ });
+
+ // Link helper functions
+ const linkTo_b1t1_HM_b2t1 = (rowId: string, body: any[]) => {
+ return ncAxiosLinkAdd({
+ context: { context, base: base1 },
+ urlParams: {
+ tableId: t1_base1.id,
+ linkId: b1t1_HM_b2t1.id,
+ rowId: rowId,
+ },
+ body: body,
+ status: 200,
+ });
+ };
+
+ const linkTo_b1t1_MM_b2t2 = (rowId: string, body: any[]) => {
+ return ncAxiosLinkAdd({
+ context: { context, base: base1 },
+ urlParams: {
+ tableId: t1_base1.id,
+ linkId: b1t1_MM_b2t2.id,
+ rowId: rowId,
+ },
+ body: body,
+ status: 200,
+ });
+ };
+
+ const linkTo_b1t2_OO_b2t1 = (rowId: string, body: any[]) => {
+ return ncAxiosLinkAdd({
+ context: { context, base: base1 },
+ urlParams: {
+ tableId: t2_base1.id,
+ linkId: b1t2_OO_b2t1.id,
+ rowId: rowId,
+ },
+ body: body,
+ status: 200,
+ });
+ };
+
+ // Establish HM links
+ await linkTo_b1t1_HM_b2t1('1', [{ id: 1 }, { id: 2 }, { id: 3 }]);
+ await linkTo_b1t1_HM_b2t1('2', [{ id: 4 }, { id: 5 }]);
+ await linkTo_b1t1_HM_b2t1('3', [{ id: 6 }]);
+
+ // Establish MM links
+ await linkTo_b1t1_MM_b2t2('1', [{ id: 1 }, { id: 2 }]);
+ await linkTo_b1t1_MM_b2t2('2', [{ id: 2 }, { id: 3 }, { id: 4 }]);
+
+ // Establish OO links
+ await linkTo_b1t2_OO_b2t1('1', [{ id: 7 }]);
+ await linkTo_b1t2_OO_b2t1('2', [{ id: 8 }]);
+
+ console.timeEnd('#### crossBaseLinkTests');
+
+ return {
+ context,
+ bases: { base1, base2 },
+ contexts: { ctx1, ctx2 },
+ tables: { t1_base1, t2_base1, t1_base2, t2_base2 },
+ crossLinks: {
+ b1t1_HM_b2t1,
+ b1t1_MM_b2t2,
+ b1t2_OO_b2t1,
+ b2t1_BT_b1t1,
+ },
+ } as ICrossBaseTestContext;
+}
+
+export { ncAxiosLinkAdd, createCrossBaseLtarColumn };
diff --git a/packages/nocodb/tests/unit/crossBaseLink/lookup-rollup.spec.ts b/packages/nocodb/tests/unit/crossBaseLink/lookup-rollup.spec.ts
new file mode 100644
index 0000000000..6c118e4365
--- /dev/null
+++ b/packages/nocodb/tests/unit/crossBaseLink/lookup-rollup.spec.ts
@@ -0,0 +1,439 @@
+import 'mocha';
+import { expect } from 'chai';
+import request from 'supertest';
+
+import { UITypes } from 'nocodb-sdk';
+import { createColumn } from '../factory/column';
+import { listRow } from '../factory/row';
+import { initCrossBaseModel } from './init';
+import type { ICrossBaseTestContext } from './init';
+
+/**
+ * Creates a lookup column for cross-base links
+ * Unlike the standard createLookupColumn, this handles cross-base relationships
+ */
+async function createCrossBaseLookupColumn(
+ context: ICrossBaseTestContext['context'],
+ {
+ table,
+ title,
+ relationColumnId,
+ lookupColumnId,
+ }: {
+ table: any;
+ title: string;
+ relationColumnId: string;
+ lookupColumnId: string;
+ },
+) {
+ const lookupColumn = await createColumn(context, table, {
+ title: title,
+ uidt: UITypes.Lookup,
+ fk_relation_column_id: relationColumnId,
+ fk_lookup_column_id: lookupColumnId,
+ table_name: table.table_name,
+ column_name: title,
+ });
+
+ return lookupColumn;
+}
+
+/**
+ * Creates a rollup column for cross-base links
+ */
+async function createCrossBaseRollupColumn(
+ context: ICrossBaseTestContext['context'],
+ {
+ table,
+ title,
+ relationColumnId,
+ rollupColumnId,
+ rollupFunction,
+ }: {
+ table: any;
+ title: string;
+ relationColumnId: string;
+ rollupColumnId: string;
+ rollupFunction: string;
+ },
+) {
+ const rollupColumn = await createColumn(context, table, {
+ title: title,
+ uidt: UITypes.Rollup,
+ fk_relation_column_id: relationColumnId,
+ fk_rollup_column_id: rollupColumnId,
+ rollup_function: rollupFunction,
+ table_name: table.table_name,
+ column_name: title,
+ });
+
+ return rollupColumn;
+}
+
+/**
+ * Helper function to fetch rows via API (to ensure fresh column data)
+ */
+async function fetchRowsViaApi(
+ context: ICrossBaseTestContext['context'],
+ base: ICrossBaseTestContext['bases']['base1'],
+ table: ICrossBaseTestContext['tables']['t1_base1'],
+) {
+ const response = await request(context.app)
+ .get(`/api/v1/db/data/noco/${base.id}/${table.id}`)
+ .set('xc-auth', context.token);
+
+ expect(response.status).to.equal(200);
+ return response.body.list;
+}
+
+function crossBaseLookupRollupTests() {
+ let _setup: ICrossBaseTestContext;
+ let _context: ICrossBaseTestContext['context'];
+ let _bases: ICrossBaseTestContext['bases'];
+ let _tables: ICrossBaseTestContext['tables'];
+ let _crossLinks: ICrossBaseTestContext['crossLinks'];
+ let _contexts: ICrossBaseTestContext['contexts'];
+
+ beforeEach(async function () {
+ const setup = await initCrossBaseModel();
+ _setup = setup;
+ _context = setup.context;
+ _bases = setup.bases;
+ _tables = setup.tables;
+ _crossLinks = setup.crossLinks;
+ _contexts = setup.contexts;
+ });
+
+ describe('Cross-Base Lookup', () => {
+ it('should lookup Title from cross-base linked table via HM', async () => {
+ // Get the Title column from base2.table1
+ const base2Table1Columns = await _tables.t1_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const titleColumn = base2Table1Columns.find(
+ (col) => col.title === 'Title',
+ );
+
+ // Create lookup column on base1.table1 looking up Title via HM link
+ const lookupCol = await createCrossBaseLookupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'CrossBaseLookupHM',
+ relationColumnId: _crossLinks.b1t1_HM_b2t1.id,
+ lookupColumnId: titleColumn.id,
+ });
+
+ expect(lookupCol).to.exist;
+ expect(lookupCol.title).to.equal('CrossBaseLookupHM');
+
+ // Get rows via API to ensure fresh column data
+ const rows = await fetchRowsViaApi(
+ _context,
+ _bases.base1,
+ _tables.t1_base1,
+ );
+
+ // Row 1 is linked to B2T1_001, B2T1_002, B2T1_003
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(row1.CrossBaseLookupHM).to.exist;
+
+ // Lookup should return array of values or comma-separated string
+ const lookupValue = row1.CrossBaseLookupHM;
+ if (Array.isArray(lookupValue)) {
+ expect(lookupValue).to.include('B2T1_001');
+ expect(lookupValue).to.include('B2T1_002');
+ expect(lookupValue).to.include('B2T1_003');
+ } else {
+ expect(lookupValue).to.include('B2T1_001');
+ }
+ });
+
+ it('should lookup Title from cross-base linked table via MM', async () => {
+ // Get the Title column from base2.table2
+ const base2Table2Columns = await _tables.t2_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const titleColumn = base2Table2Columns.find(
+ (col) => col.title === 'Title',
+ );
+
+ // Create lookup column on base1.table1 looking up Title via MM link
+ const lookupCol = await createCrossBaseLookupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'CrossBaseLookupMM',
+ relationColumnId: _crossLinks.b1t1_MM_b2t2.id,
+ lookupColumnId: titleColumn.id,
+ });
+
+ expect(lookupCol).to.exist;
+
+ // Get rows via API to ensure fresh column data
+ const rows = await fetchRowsViaApi(
+ _context,
+ _bases.base1,
+ _tables.t1_base1,
+ );
+
+ // Row 1 is linked to B2T2_001, B2T2_002
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(row1.CrossBaseLookupMM).to.exist;
+ });
+
+ it('should lookup Title from cross-base linked table via OO', async () => {
+ // Get the Title column from base2.table1
+ const base2Table1Columns = await _tables.t1_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const titleColumn = base2Table1Columns.find(
+ (col) => col.title === 'Title',
+ );
+
+ // Create lookup column on base1.table2 looking up Title via OO link
+ const lookupCol = await createCrossBaseLookupColumn(_context, {
+ table: _tables.t2_base1,
+ title: 'CrossBaseLookupOO',
+ relationColumnId: _crossLinks.b1t2_OO_b2t1.id,
+ lookupColumnId: titleColumn.id,
+ });
+
+ expect(lookupCol).to.exist;
+
+ // Get rows via API to ensure fresh column data
+ const rows = await fetchRowsViaApi(
+ _context,
+ _bases.base1,
+ _tables.t2_base1,
+ );
+
+ // Row 1 is linked to B2T1_007
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(row1.CrossBaseLookupOO).to.exist;
+
+ // OO lookup should return single value
+ const lookupValue = row1.CrossBaseLookupOO;
+ if (Array.isArray(lookupValue)) {
+ expect(lookupValue[0]).to.equal('B2T1_007');
+ } else {
+ expect(lookupValue).to.equal('B2T1_007');
+ }
+ });
+ });
+
+ describe('Cross-Base Rollup', () => {
+ it('should count cross-base linked records via HM', async () => {
+ // Get the Title column from base2.table1 (for count)
+ const base2Table1Columns = await _tables.t1_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const titleColumn = base2Table1Columns.find(
+ (col) => col.title === 'Title',
+ );
+
+ // Create rollup column counting linked records
+ const rollupCol = await createCrossBaseRollupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'CrossBaseCountHM',
+ relationColumnId: _crossLinks.b1t1_HM_b2t1.id,
+ rollupColumnId: titleColumn.id,
+ rollupFunction: 'count',
+ });
+
+ expect(rollupCol).to.exist;
+ expect(rollupCol.title).to.equal('CrossBaseCountHM');
+
+ // Get rows and verify rollup values
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ // Row 1 is linked to 3 records, Row 2 to 2, Row 3 to 1
+ const row1 = rows.find((r: any) => r.Id === 1);
+ const row2 = rows.find((r: any) => r.Id === 2);
+ const row3 = rows.find((r: any) => r.Id === 3);
+
+ expect(Number(row1.CrossBaseCountHM)).to.equal(3);
+ expect(Number(row2.CrossBaseCountHM)).to.equal(2);
+ expect(Number(row3.CrossBaseCountHM)).to.equal(1);
+ });
+
+ it('should count cross-base linked records via MM', async () => {
+ // Get the Title column from base2.table2
+ const base2Table2Columns = await _tables.t2_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const titleColumn = base2Table2Columns.find(
+ (col) => col.title === 'Title',
+ );
+
+ // Create rollup column counting linked records
+ const rollupCol = await createCrossBaseRollupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'CrossBaseCountMM',
+ relationColumnId: _crossLinks.b1t1_MM_b2t2.id,
+ rollupColumnId: titleColumn.id,
+ rollupFunction: 'count',
+ });
+
+ expect(rollupCol).to.exist;
+
+ // Get rows and verify rollup values
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ // Row 1 is linked to 2 records, Row 2 to 3
+ const row1 = rows.find((r: any) => r.Id === 1);
+ const row2 = rows.find((r: any) => r.Id === 2);
+
+ expect(Number(row1.CrossBaseCountMM)).to.equal(2);
+ expect(Number(row2.CrossBaseCountMM)).to.equal(3);
+ });
+
+ it('should sum values from cross-base linked table', async () => {
+ // Get the Amount column from base2.table2
+ const base2Table2Columns = await _tables.t2_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const amountColumn = base2Table2Columns.find(
+ (col) => col.title === 'Amount',
+ );
+
+ // Create rollup column summing Amount values
+ const rollupCol = await createCrossBaseRollupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'CrossBaseSumMM',
+ relationColumnId: _crossLinks.b1t1_MM_b2t2.id,
+ rollupColumnId: amountColumn.id,
+ rollupFunction: 'sum',
+ });
+
+ expect(rollupCol).to.exist;
+
+ // Get rows and verify rollup values
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ // Row 1 is linked to B2T2_001 (Amount=10), B2T2_002 (Amount=20)
+ // Sum should be 30
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(Number(row1.CrossBaseSumMM)).to.equal(30);
+
+ // Row 2 is linked to B2T2_002 (20), B2T2_003 (30), B2T2_004 (40)
+ // Sum should be 90
+ const row2 = rows.find((r: any) => r.Id === 2);
+ expect(Number(row2.CrossBaseSumMM)).to.equal(90);
+ });
+
+ it('should calculate average from cross-base linked table', async () => {
+ // Get the Amount column from base2.table2
+ const base2Table2Columns = await _tables.t2_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const amountColumn = base2Table2Columns.find(
+ (col) => col.title === 'Amount',
+ );
+
+ // Create rollup column averaging Amount values
+ const rollupCol = await createCrossBaseRollupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'CrossBaseAvgMM',
+ relationColumnId: _crossLinks.b1t1_MM_b2t2.id,
+ rollupColumnId: amountColumn.id,
+ rollupFunction: 'avg',
+ });
+
+ expect(rollupCol).to.exist;
+
+ // Get rows and verify rollup values
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ // Row 1: avg(10, 20) = 15
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(Number(row1.CrossBaseAvgMM)).to.equal(15);
+
+ // Row 2: avg(20, 30, 40) = 30
+ const row2 = rows.find((r: any) => r.Id === 2);
+ expect(Number(row2.CrossBaseAvgMM)).to.equal(30);
+ });
+
+ it('should find min value from cross-base linked table', async () => {
+ // Get the Amount column from base2.table2
+ const base2Table2Columns = await _tables.t2_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const amountColumn = base2Table2Columns.find(
+ (col) => col.title === 'Amount',
+ );
+
+ // Create rollup column finding min Amount value
+ const rollupCol = await createCrossBaseRollupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'CrossBaseMinMM',
+ relationColumnId: _crossLinks.b1t1_MM_b2t2.id,
+ rollupColumnId: amountColumn.id,
+ rollupFunction: 'min',
+ });
+
+ expect(rollupCol).to.exist;
+
+ // Get rows and verify rollup values
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ // Row 1: min(10, 20) = 10
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(Number(row1.CrossBaseMinMM)).to.equal(10);
+
+ // Row 2: min(20, 30, 40) = 20
+ const row2 = rows.find((r: any) => r.Id === 2);
+ expect(Number(row2.CrossBaseMinMM)).to.equal(20);
+ });
+
+ it('should find max value from cross-base linked table', async () => {
+ // Get the Amount column from base2.table2
+ const base2Table2Columns = await _tables.t2_base2.getColumns(
+ _contexts.ctx2,
+ );
+ const amountColumn = base2Table2Columns.find(
+ (col) => col.title === 'Amount',
+ );
+
+ // Create rollup column finding max Amount value
+ const rollupCol = await createCrossBaseRollupColumn(_context, {
+ table: _tables.t1_base1,
+ title: 'CrossBaseMaxMM',
+ relationColumnId: _crossLinks.b1t1_MM_b2t2.id,
+ rollupColumnId: amountColumn.id,
+ rollupFunction: 'max',
+ });
+
+ expect(rollupCol).to.exist;
+
+ // Get rows and verify rollup values
+ const rows = await listRow({
+ base: _bases.base1,
+ table: _tables.t1_base1,
+ });
+
+ // Row 1: max(10, 20) = 20
+ const row1 = rows.find((r: any) => r.Id === 1);
+ expect(Number(row1.CrossBaseMaxMM)).to.equal(20);
+
+ // Row 2: max(20, 30, 40) = 40
+ const row2 = rows.find((r: any) => r.Id === 2);
+ expect(Number(row2.CrossBaseMaxMM)).to.equal(40);
+ });
+ });
+}
+
+export function crossBaseLookupRollupTest() {
+ describe('CrossBaseLookupRollupTest', crossBaseLookupRollupTests);
+}
diff --git a/packages/nocodb/tests/unit/index.test.ts b/packages/nocodb/tests/unit/index.test.ts
index 25693b9ffc..57bd3f9958 100644
--- a/packages/nocodb/tests/unit/index.test.ts
+++ b/packages/nocodb/tests/unit/index.test.ts
@@ -9,6 +9,7 @@ import { processorTests } from './processor/index.test';
import { errorTests } from './error/index.test';
import { rollupTests } from './rollup/index.test';
import { linksTests } from './links/index.test';
+import { crossBaseLinkTests } from './crossBaseLink/index.spec';
import { dbQueryClientTests } from './dbQueryClient/index.test';
import { helperTests } from './helpersTest/index.test';
@@ -30,6 +31,7 @@ dotenv.config({
formulaTests();
dbQueryClientTests();
linksTests();
+ crossBaseLinkTests();
rollupTests();
errorTests();
restTests();