Skip to content

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

add unit test

Fendy Heryantoc2e66acbdf3f
diff --git a/packages/nocodb-sdk/src/lib/stringHelpers.ts b/packages/nocodb-sdk/src/lib/stringHelpers.ts
index b7e00a5630..2712b479d4 100644
--- a/packages/nocodb-sdk/src/lib/stringHelpers.ts
+++ b/packages/nocodb-sdk/src/lib/stringHelpers.ts
@@ -136,12 +136,15 @@ export function generateUniqueCopyName<T = string>(
return String((item as any).title);
});
- let newName = `${prefix} ${originalName}`;
+ const getPrefix = () => {
+ return prefix && prefix !== '' ? `${prefix} ` : prefix;
+ };
+ let newName = `${getPrefix()}${originalName}`;
let counter = 1;
while (existingNames.includes(newName)) {
const counterText = counterFormat.replace('{counter}', counter.toString());
- newName = `${prefix} ${originalName}${separator}${counterText}`;
+ newName = `${getPrefix()}${originalName}${separator}${counterText}`;
counter++;
}
@@ -172,7 +175,10 @@ export const trimMatchingQuotes = (str?: string | null): string => {
* Note: Since we are using ES2017, `String.prototype.matchAll` is not available.
* This function acts as a fallback to achieve the same behavior.
*/
-export function stringAllMatches(str: string, regex: RegExp): RegExpExecArray[] {
+export function stringAllMatches(
+ str: string,
+ regex: RegExp
+): RegExpExecArray[] {
// Ensure regex has the global flag, because exec() needs it to iterate
const globalRegex = new RegExp(
regex.source,
diff --git a/packages/nocodb/src/helpers/columnHelpers.ts b/packages/nocodb/src/helpers/columnHelpers.ts
index fe58a5370d..7b4dc4fdbc 100644
--- a/packages/nocodb/src/helpers/columnHelpers.ts
+++ b/packages/nocodb/src/helpers/columnHelpers.ts
@@ -650,41 +650,41 @@ export const TableSystemColumns = () => [
{
column_name: 'id',
title: 'Id',
- uidt: 'ID',
+ uidt: UITypes.ID,
allowNonSystem: false,
system: true,
},
{
column_name: 'created_at',
title: 'CreatedAt',
- uidt: 'CreatedTime',
+ uidt: UITypes.CreatedTime,
allowNonSystem: true,
system: true,
},
{
column_name: 'updated_at',
title: 'UpdatedAt',
- uidt: 'LastModifiedTime',
+ uidt: UITypes.LastModifiedTime,
allowNonSystem: true,
},
{
column_name: 'created_by',
title: 'nc_created_by',
- uidt: 'CreatedBy',
+ uidt: UITypes.CreatedBy,
allowNonSystem: true,
system: true,
},
{
column_name: 'updated_by',
title: 'nc_updated_by',
- uidt: 'LastModifiedBy',
+ uidt: UITypes.LastModifiedBy,
allowNonSystem: true,
system: true,
},
{
column_name: 'nc_order',
title: 'nc_order',
- uidt: 'Order',
+ uidt: UITypes.Order,
allowNonSystem: false,
system: true,
},
diff --git a/packages/nocodb/src/helpers/tableHelpers.ts b/packages/nocodb/src/helpers/tableHelpers.ts
index e1111672dc..4fa3854db6 100644
--- a/packages/nocodb/src/helpers/tableHelpers.ts
+++ b/packages/nocodb/src/helpers/tableHelpers.ts
@@ -4,8 +4,9 @@ import {
type NcContext,
} from 'nocodb-sdk';
import { TableSystemColumns } from './columnHelpers';
+import type { UITypes } from 'nocodb-sdk';
-export const verifyCreateTableSystemColumns = (
+export const repopulateCreateTableSystemColumns = (
_context: NcContext,
{ columns }: { columns: ColumnType[] },
) => {
@@ -19,13 +20,11 @@ export const verifyCreateTableSystemColumns = (
return col;
}),
// remove all UIDT ID and Order from request
- ...columns.filter((col) => !strictOneColumnUidt.includes(col.uidt)),
+ ...columns.filter(
+ (col) => !strictOneColumnUidt.includes(col.uidt as UITypes),
+ ),
];
- for (
- let i = result.slice(tableSystemColumns.length).length - 1;
- i >= 0;
- i--
- ) {
+ for (let i = result.length - 1; i >= tableSystemColumns.length; i--) {
const col = result[i];
// check if title, column name or uidt is intersecting with system columns
const intersectingSystemCols = tableSystemColumns.filter(
@@ -46,6 +45,7 @@ export const verifyCreateTableSystemColumns = (
col.title = generateUniqueCopyName(col.title, [sysCol.title], {
prefix: '',
separator: '',
+ counterFormat: '{counter}',
});
}
if (col.column_name && col.column_name === sysCol.column_name) {
@@ -55,6 +55,7 @@ export const verifyCreateTableSystemColumns = (
{
prefix: '',
separator: '',
+ counterFormat: '{counter}',
},
);
}
diff --git a/packages/nocodb/tests/unit/model/index.test.ts b/packages/nocodb/tests/unit/model/index.test.ts
index 5398dfcea9..4b235ff859 100644
--- a/packages/nocodb/tests/unit/model/index.test.ts
+++ b/packages/nocodb/tests/unit/model/index.test.ts
@@ -1,11 +1,13 @@
import 'mocha';
import { runOnSet } from '../utils/runOnSet';
import baseModelSqlTest from './tests/baseModelSql.test';
+import { tableHelpersTest } from './tableHelpers/tableHelpers.test';
function modelTests() {
baseModelSqlTest();
}
export default runOnSet(1, function () {
+ describe('tableHelpersTest', tableHelpersTest);
describe('Model', modelTests);
});
diff --git a/packages/nocodb/tests/unit/model/tableHelpers/tableHelpers.test.ts b/packages/nocodb/tests/unit/model/tableHelpers/tableHelpers.test.ts
new file mode 100644
index 0000000000..7d1142af45
--- /dev/null
+++ b/packages/nocodb/tests/unit/model/tableHelpers/tableHelpers.test.ts
@@ -0,0 +1,96 @@
+import 'mocha';
+import { expect } from 'chai';
+
+import { UITypes } from 'nocodb-sdk';
+import { repopulateCreateTableSystemColumns } from '~/helpers/tableHelpers';
+import { TableSystemColumns } from '~/helpers/columnHelpers';
+
+function tableHelpersTests() {
+ describe('repopulateCreateTableSystemColumns', () => {
+ it('will repopulate columns with system columns for table create', async () => {
+ const idColumn = {
+ title: 'CustomID',
+ uidt: UITypes.ID,
+ };
+ const orderColumn = {
+ title: 'CustomOrder',
+ uidt: UITypes.Order,
+ };
+ const createdByColumn = {
+ title: 'nc_created_by',
+ column_name: 'created_by',
+ uidt: UITypes.CreatedBy,
+ };
+ const createdByColumn2 = {
+ title: 'CustomCreatedBy',
+ column_name: 'created_by',
+ uidt: UITypes.CreatedBy,
+ };
+ const updatedByColumn3 = {
+ title: 'nc_updated_by',
+ column_name: 'updated_by1',
+ uidt: UITypes.LastModifiedBy,
+ };
+ const sameNameColumn = {
+ title: 'Id',
+ column_name: 'id',
+ uidt: UITypes.SingleLineText,
+ };
+ const normalColumn = {
+ title: 'Title',
+ column_name: 'title',
+ uidt: UITypes.SingleLineText,
+ };
+ const columns = [
+ { ...idColumn },
+ { ...orderColumn },
+ { ...createdByColumn },
+ { ...createdByColumn2 },
+ { ...updatedByColumn3 },
+ { ...sameNameColumn },
+ { ...normalColumn },
+ ];
+ const result = repopulateCreateTableSystemColumns({}, { columns });
+ const nonSystemResult = result.slice(TableSystemColumns().length);
+
+ expect(
+ nonSystemResult.some((col: any) => col.uidt === idColumn.uidt),
+ ).to.eq(false, 'uidt ' + idColumn.uidt + ' exists in non system columns');
+ expect(
+ nonSystemResult.some((col: any) => col.uidt === orderColumn.uidt),
+ ).to.eq(
+ false,
+ 'uidt ' + orderColumn.uidt + ' exists in non system columns',
+ );
+
+ // if column is same, remove
+ expect(
+ nonSystemResult.some(
+ (col: any) => col.title === createdByColumn.title + '1',
+ ),
+ ).to.eq(false, createdByColumn.title + '1 exists');
+ // if only column name is same, append
+ expect(
+ nonSystemResult.some(
+ (col: any) => col.column_name === createdByColumn2.column_name + '1',
+ ),
+ ).to.eq(true, createdByColumn2.column_name + '1 not exists');
+ // if only column title is same, append
+ expect(
+ nonSystemResult.some(
+ (col: any) => col.title === updatedByColumn3.title + '1',
+ ),
+ ).to.eq(true, updatedByColumn3.title + '1 not exists');
+ // rename for id field
+ expect(
+ nonSystemResult.some(
+ (col: any) => col.title === sameNameColumn.title + '1',
+ ),
+ ).to.eq(true, sameNameColumn.title + '1 not exists');
+ });
+ });
+}
+
+export function tableHelpersTest() {
+ describe.only('tableHelpers', tableHelpersTests);
+}