From e788c69c40fb4570f2267cbc67e204a86fa88cf9 Mon Sep 17 00:00:00 2001 From: Raju Udava <86527202+dstala@users.noreply.github.com> Date: Thu, 21 Dec 2023 09:17:05 +0000 Subject: [PATCH] test: API test framework for formula, sample tests for REGEXP --- packages/nocodb/tests/unit/factory/column.ts | 18 ++++ packages/nocodb/tests/unit/rest/index.test.ts | 2 + .../tests/unit/rest/tests/formula.test.ts | 101 ++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 packages/nocodb/tests/unit/rest/tests/formula.test.ts diff --git a/packages/nocodb/tests/unit/factory/column.ts b/packages/nocodb/tests/unit/factory/column.ts index 21740b1bc9..0b88dc1fc2 100644 --- a/packages/nocodb/tests/unit/factory/column.ts +++ b/packages/nocodb/tests/unit/factory/column.ts @@ -386,6 +386,23 @@ const updateViewColumn = async ( return updatedColumn; }; +const updateColumn = async ( + context, + { table, column, attr }: { column: Column; table: Model; attr: any }, +) => { + const res = await request(context.app) + .patch(`/api/v2/meta/columns/${column.id}`) + .set('xc-auth', context.token) + .send({ + ...attr, + }); + + const updatedColumn: Column = (await table.getColumns()).find( + (column) => column.id === column.id, + ); + return updatedColumn; +}; + export { customColumns, defaultColumns, @@ -396,4 +413,5 @@ export { createLookupColumn, createLtarColumn, updateViewColumn, + updateColumn, }; diff --git a/packages/nocodb/tests/unit/rest/index.test.ts b/packages/nocodb/tests/unit/rest/index.test.ts index 0eb63c4506..f14c76c4e3 100644 --- a/packages/nocodb/tests/unit/rest/index.test.ts +++ b/packages/nocodb/tests/unit/rest/index.test.ts @@ -10,6 +10,7 @@ import attachmentTests from './tests/attachment.test'; import filterTest from './tests/filter.test'; import newDataApisTest from './tests/newDataApis.test'; import groupByTest from './tests/groupby.test'; +import formulaTests from './tests/formula.test'; let workspaceTest = () => {}; if (process.env.EE === 'true') { @@ -31,6 +32,7 @@ function restTests() { newDataApisTest(); groupByTest(); workspaceTest(); + formulaTests(); // Enable for dashboard feature // widgetTest(); diff --git a/packages/nocodb/tests/unit/rest/tests/formula.test.ts b/packages/nocodb/tests/unit/rest/tests/formula.test.ts new file mode 100644 index 0000000000..ff40642515 --- /dev/null +++ b/packages/nocodb/tests/unit/rest/tests/formula.test.ts @@ -0,0 +1,101 @@ +import 'mocha'; +import { UITypes } from 'nocodb-sdk'; +import { expect } from 'chai'; +import init from '../../init'; +import { createProject } from '../../factory/base'; +import { createTable } from '../../factory/table'; +import { createBulkRows, listRow, rowMixedValue } from '../../factory/row'; +import { updateColumn } from '../../factory/column'; +import type Model from '../../../../src/models/Model'; +import type Base from '~/models/Base'; + +let context; +let base: Base; +let table: Model; +let columns: any[]; +let unfilteredRecords: any[] = []; + +function formulaRegExpBased() { + // prepare data for test cases + beforeEach(async function () { + context = await init(); + base = await createProject(context); + table = await createTable(context, base, { + table_name: 'sampleTable', + title: 'sampleTable', + columns: [ + { + column_name: 'Id', + title: 'Id', + uidt: UITypes.ID, + }, + { + column_name: 'Title', + title: 'Title', + uidt: UITypes.SingleLineText, + }, + { + column_name: 'formula', + title: 'formula', + uidt: UITypes.Formula, + formula: '20', + }, + ], + }); + + columns = await table.getColumns(); + + const rowAttributes = []; + for (let i = 0; i < 100; i++) { + const row = { + Title: rowMixedValue(columns[1], i), + }; + rowAttributes.push(row); + } + + await createBulkRows(context, { + base, + table, + values: rowAttributes, + }); + unfilteredRecords = await listRow({ base, table }); + + // verify length of unfiltered records to be 800 + expect(unfilteredRecords.length).to.equal(100); + }); + + it('Type: REGEX_MATCH ', async () => { + const formulaList = [ + `REGEX_MATCH("123-45-6789", "\\d{3}-\\d{2}-\\d{4}")`, + 'REGEX_MATCH("123-45-6789", "\\d{3}-\\d{2}-\\d{4}")', + 'REGEX_MATCH("123-45-6789", "\\d{3}-\\d{2}-\\d{4}")', + 'REGEX_MATCH("ABC-45-6789", "\\w{3}-\\d{2}-\\d{4}")', + 'REGEX_MATCH("123-XY-6789", "\\d{3}-\\w{2}-\\d{4}")', + 'REGEX_MATCH("123-45-$#@!", "123-45-[\\s\\S]{4}")', + 'REGEX_MATCH("123456789", "1?2?3?-?4?5-?6?7?8?9?")', + 'REGEX_MATCH("123-456789", "\\d{3}-?\\d{2}-?\\d{4}")', + 'REGEX_MATCH("123-45-6789", "123-\\d{2}-6789")', + ]; + + for (let i = 0; i < formulaList.length; i++) { + await updateColumn(context, { + table, + column: columns[2], + attr: { + formula: formulaList[i], + formula_raw: formulaList[i], + title: 'formula', + uidt: UITypes.Formula, + }, + }); + + console.log(formulaList[i]); + unfilteredRecords = await listRow({ base, table }); + expect(unfilteredRecords[0].formula).to.equal(1); + } + }); +} + +export default function () { + describe('Formula: REGEXP based', formulaRegExpBased); +}