π₯ π₯ π₯ A Free & Self-hostable Airtable Alternative
TypeScript airtableairtable-alternativeautomatic-apihacktoberfestlow-codeno-codeno-code-databaseno-code-platformpostgresqlrest-apirestful-apispreadsheetsqliteswagger
unit test do not return temp_ attachments
Fendy Heryantodd03acfe7b93
diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.tsindex 889324e424..142a4b558a 100644--- a/packages/nocodb/src/db/BaseModelSqlv2.ts+++ b/packages/nocodb/src/db/BaseModelSqlv2.ts@@ -5578,6 +5578,9 @@ class BaseModelSqlv2 implements IBaseModelSqlV2 {}if (d[col.id]?.length) {+ d[col.id] = d[col.id].filter(+ (attr) => attr.id && !attr.id?.startsWith('temp_'),+ );for (let i = 0; i < d[col.id].length; i++) {if (typeof d[col.id][i] === 'string') {d[col.id][i] = JSON.parse(d[col.id][i]);@@ -5707,10 +5710,7 @@ class BaseModelSqlv2 implements IBaseModelSqlV2 {}),);}- } else if (- attachment?.url &&- !attachment.id.startsWith('temp_')- ) {+ } else if (attachment?.url) {if (attachment?.url.startsWith('data:')) {continue;}diff --git a/packages/nocodb/src/db/field-handler/handlers/attachment/attachment.general.handler.ts b/packages/nocodb/src/db/field-handler/handlers/attachment/attachment.general.handler.tsindex c55932bab3..218a9c9ee3 100644--- a/packages/nocodb/src/db/field-handler/handlers/attachment/attachment.general.handler.ts+++ b/packages/nocodb/src/db/field-handler/handlers/attachment/attachment.general.handler.ts@@ -65,10 +65,12 @@ export class AttachmentGeneralHandler extends ComputedFieldHandler {}}const { removed } = detailedDiff(- new Array(oldValueMap.keys()),+ Array.from(oldValueMap.keys()),value.map((k) => k.id),);- await FileReference.delete(params.options.context, removed);+ if (removed.length > 0) {+ await FileReference.delete(params.options.context, removed);+ }return {value: value.map((attr) => {return attr.id.startsWith('temp_')diff --git a/packages/nocodb/tests/unit/rest/tests/dataApiV3/attachment.test.ts b/packages/nocodb/tests/unit/rest/tests/dataApiV3/attachment.test.tsnew file mode 100644index 0000000000..4322e94a10--- /dev/null+++ b/packages/nocodb/tests/unit/rest/tests/dataApiV3/attachment.test.ts@@ -0,0 +1,125 @@+import 'mocha';+import nock from 'nock';+import { expect } from 'chai';++import {+ beforeEachAttachment,+ beforeEach as dataApiV3BeforeEach,+} from './beforeEach';+import { ncAxios } from './ncAxios';++const API_VERSION = 'v3';+describe('Attachment V3', () => {+ let testContext;+ let table;+ let urlPrefix: string;+ let testAxios;+ let ncAxiosPost: INcAxios['ncAxiosPost'];+ let ncAxiosGet: INcAxios['ncAxiosGet'];+ let ncAxiosPatch: INcAxios['ncAxiosPatch'];++ beforeEach(async function () {+ const base64Image =+ 'iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAADMElEQVR4nOzVwQnAIBQFQYXff81RUkQCOyDj1YOPnbXWPmeTRef+/3O/OyBjzh3CD95BfqICMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMO0TAAD//2Anhf4QtqobAAAAAElFTkSuQmCC';+ const imageBuffer = Buffer.from(base64Image, 'base64');+ testContext = await dataApiV3BeforeEach();++ testAxios = ncAxios(testContext);+ ncAxiosPost = testAxios.ncAxiosPost;+ ncAxiosGet = testAxios.ncAxiosGet;+ ncAxiosPatch = testAxios.ncAxiosPatch;+ const initResult = await beforeEachAttachment(testContext);++ table = initResult.table;+ urlPrefix = `/api/${API_VERSION}/data/${testContext.base.id}`;++ nock('http://myhost.local/files')+ .get('/image')+ // FIXME: increase if insert / update result is updated too fast+ // delay to ensure background not clear before list+ .delay(10)+ .reply(200, imageBuffer, {+ 'Content-Type': 'image/png',+ 'Content-Disposition': 'attachment; filename="test-image.png"',+ 'Content-Length': imageBuffer.length,+ } as any)+ .persist();+ });++ afterEach(function () {+ nock.restore();+ nock.cleanAll();+ });++ it('Upload file and update from url', async () => {+ const rsp = await ncAxiosPost({+ url: `${urlPrefix}/${table.id}/records`,+ body: [+ {+ fields: {+ Attachment: [+ {+ url: 'http://myhost.local/files/image',+ },+ {+ url: 'http://myhost.local/files/image',+ },+ ],+ },+ },+ ],+ });++ expect(rsp.body.records.length).to.eq(1);+ expect(rsp.body.records[0].fields.Attachment.length).to.eq(0);++ // FIXME: if 100 ms is not enough+ // wait 100 ms for background to kick off+ await new Promise<void>((resolve) => setTimeout(() => resolve(), 100));++ const getRsp1 = await ncAxiosGet({+ url: `${urlPrefix}/${table.id}/records/${rsp.body.records[0].id}`,+ });+ expect(getRsp1.body.fields.Attachment.length).to.eq(2);+ expect(getRsp1.body.fields.Attachment[0].id).to.not.satisfy((id) =>+ id.startsWith('temp_'),+ );++ const patchRsp = await ncAxiosPatch({+ url: `${urlPrefix}/${table.id}/records`,+ body: [+ {+ id: getRsp1.body.id,+ fields: {+ Attachment: [+ {+ id: getRsp1.body.fields.Attachment[0].id,+ },+ {+ url: 'http://myhost.local/files/image',+ },+ ],+ },+ },+ ],+ });++ expect(patchRsp.body.records.length).to.eq(1);+ expect(patchRsp.body.records[0].fields.Attachment.length).to.eq(1);++ // FIXME: if 100 ms is not enough+ // wait 100 ms for background to kick off+ await new Promise<void>((resolve) => setTimeout(() => resolve(), 100));++ const getRsp2 = await ncAxiosGet({+ url: `${urlPrefix}/${table.id}/records/${rsp.body.records[0].id}`,+ });+ expect(getRsp2.body.fields.Attachment.length).to.eq(2);+ expect(getRsp2.body.fields.Attachment[1].id).to.not.satisfy((id) =>+ id.startsWith('temp_'),+ );+ expect(getRsp2.body.fields.Attachment[0].id).to.eq(+ getRsp1.body.fields.Attachment[0].id,+ );+ });+});diff --git a/packages/nocodb/tests/unit/rest/tests/dataApiV3/beforeEach.ts b/packages/nocodb/tests/unit/rest/tests/dataApiV3/beforeEach.tsindex 4a8975fa99..938ed3f2a4 100644--- a/packages/nocodb/tests/unit/rest/tests/dataApiV3/beforeEach.ts+++ b/packages/nocodb/tests/unit/rest/tests/dataApiV3/beforeEach.ts@@ -472,9 +472,27 @@ export const beforeEachUserBased = async (testContext: ITestContext) => {};};-const FILE_PATH = path.join(__dirname, 'test.txt');-export const beforeEachAttachment = async (_testContext: ITestContext) => {- console.time('#### attachmentTests');- fs.writeFileSync(FILE_PATH, 'test', `utf-8`);- console.timeEnd('#### attachmentTests');+export const beforeEachAttachment = async (testContext: ITestContext) => {+ const table = await createTable(testContext.context, testContext.base, {+ table_name: 'attachmentBased',+ title: 'attachmentBased',+ columns: [+ {+ column_name: 'id',+ title: 'Id',+ uidt: UITypes.ID,+ description: `id ${UITypes.ID}`,+ },+ {+ uidt: UITypes.Attachment,+ column_name: 'attachment',+ title: 'Attachment',+ },+ ],+ });+ const columns = await table.getColumns(testContext.ctx);+ return {+ table,+ columns,+ };};diff --git a/packages/nocodb/tests/unit/rest/tests/dataApiV3/error-handling.test.ts b/packages/nocodb/tests/unit/rest/tests/dataApiV3/error-handling.test.tsindex c04f661500..ab7086161b 100644--- a/packages/nocodb/tests/unit/rest/tests/dataApiV3/error-handling.test.ts+++ b/packages/nocodb/tests/unit/rest/tests/dataApiV3/error-handling.test.ts@@ -970,15 +970,5 @@ describe('dataApiV3', () => {).to.equal(true);});});-- describe.skip('attachment', () => {- let table: Model;- const columns: Column[] = [];-- beforeEach(async function () {- const initResult = await beforeEachAttachment(testContext);- urlPrefix = `/api/${API_VERSION}/data/${testContext.base.id}`;- });- });});});diff --git a/packages/nocodb/tests/unit/rest/tests/dataApiV3/index.test.ts b/packages/nocodb/tests/unit/rest/tests/dataApiV3/index.test.tsindex 8073caef49..010e6d601c 100644--- a/packages/nocodb/tests/unit/rest/tests/dataApiV3/index.test.ts+++ b/packages/nocodb/tests/unit/rest/tests/dataApiV3/index.test.ts@@ -6,4 +6,5 @@ export const dataApiV3Test = async () => {await import('./patch-update.test');await import('./delete.test');await import('./list-and-crud.test');+ await import('./attachment.test');};diff --git a/packages/nocodb/tests/unit/rest/tests/dataApiV3/post-insert.test.ts b/packages/nocodb/tests/unit/rest/tests/dataApiV3/post-insert.test.tsindex f6d9639f81..0e959dfad5 100644--- a/packages/nocodb/tests/unit/rest/tests/dataApiV3/post-insert.test.ts+++ b/packages/nocodb/tests/unit/rest/tests/dataApiV3/post-insert.test.ts@@ -538,18 +538,5 @@ describe('dataApiV3', () => {}});});-- // v3 has no attachment atm- describe.skip('attachment', () => {- let table: Model;- const columns: Column[] = [];-- beforeEach(async function () {- const initResult = await beforeEachAttachment(testContext);- urlPrefix = `/api/${API_VERSION}/data/${testContext.base.id}`;- });-- it('Upload file - Super admin', async () => {});- });});});