Add in signature field testing support. Some signature fixes

This commit is contained in:
Dean 2024-04-08 12:18:05 +01:00
parent 7106689ebd
commit 9b276aa3c1
4 changed files with 98 additions and 28 deletions

View File

@ -237,6 +237,11 @@ describe.each([
name: "attachment", name: "attachment",
constraints: { type: "array", presence: false }, constraints: { type: "array", presence: false },
} }
const signature: FieldSchema = {
type: FieldType.SIGNATURE,
name: "signature",
constraints: { type: "array", presence: false },
}
const bool: FieldSchema = { const bool: FieldSchema = {
type: FieldType.BOOLEAN, type: FieldType.BOOLEAN,
name: "boolean", name: "boolean",
@ -301,6 +306,10 @@ describe.each([
attachmentUndefined: attachment, attachmentUndefined: attachment,
attachmentEmpty: attachment, attachmentEmpty: attachment,
attachmentEmptyArrayStr: attachment, attachmentEmptyArrayStr: attachment,
signatureNull: signature,
signatureUndefined: signature,
signatureEmpty: signature,
signatureEmptyArrayStr: signature,
arrayFieldEmptyArrayStr: arrayField, arrayFieldEmptyArrayStr: arrayField,
arrayFieldArrayStrKnown: arrayField, arrayFieldArrayStrKnown: arrayField,
arrayFieldNull: arrayField, arrayFieldNull: arrayField,
@ -340,6 +349,10 @@ describe.each([
attachmentUndefined: undefined, attachmentUndefined: undefined,
attachmentEmpty: "", attachmentEmpty: "",
attachmentEmptyArrayStr: "[]", attachmentEmptyArrayStr: "[]",
signatureNull: null,
signatureUndefined: undefined,
signatureEmpty: "",
signatureEmptyArrayStr: "[]",
arrayFieldEmptyArrayStr: "[]", arrayFieldEmptyArrayStr: "[]",
arrayFieldUndefined: undefined, arrayFieldUndefined: undefined,
arrayFieldNull: null, arrayFieldNull: null,
@ -372,6 +385,10 @@ describe.each([
expect(row.attachmentUndefined).toBe(undefined) expect(row.attachmentUndefined).toBe(undefined)
expect(row.attachmentEmpty).toEqual([]) expect(row.attachmentEmpty).toEqual([])
expect(row.attachmentEmptyArrayStr).toEqual([]) expect(row.attachmentEmptyArrayStr).toEqual([])
expect(row.signatureNull).toEqual([])
expect(row.signatureUndefined).toBe(undefined)
expect(row.signatureEmpty).toEqual([])
expect(row.signatureEmptyArrayStr).toEqual([])
expect(row.arrayFieldEmptyArrayStr).toEqual([]) expect(row.arrayFieldEmptyArrayStr).toEqual([])
expect(row.arrayFieldNull).toEqual([]) expect(row.arrayFieldNull).toEqual([])
expect(row.arrayFieldUndefined).toEqual(undefined) expect(row.arrayFieldUndefined).toEqual(undefined)
@ -783,24 +800,21 @@ describe.each([
}) })
isInternal && isInternal &&
describe("attachments", () => { describe("attachments and signatures", () => {
it("should allow enriching attachment rows", async () => { const coreAttachmentEnrichment = async (
schema: any,
field: string,
attachmentId: string
) => {
const table = await config.api.table.save( const table = await config.api.table.save(
defaultTable({ defaultTable({
schema: { schema,
attachment: {
type: FieldType.ATTACHMENT,
name: "attachment",
constraints: { type: "array", presence: false },
},
},
}) })
) )
const attachmentId = `${uuid.v4()}.csv`
const row = await config.api.row.save(table._id!, { const row = await config.api.row.save(table._id!, {
name: "test", name: "test",
description: "test", description: "test",
attachment: [ [field]: [
{ {
key: `${config.getAppId()}/attachments/${attachmentId}`, key: `${config.getAppId()}/attachments/${attachmentId}`,
}, },
@ -810,11 +824,39 @@ describe.each([
await config.withEnv({ SELF_HOSTED: "true" }, async () => { await config.withEnv({ SELF_HOSTED: "true" }, async () => {
return context.doInAppContext(config.getAppId(), async () => { return context.doInAppContext(config.getAppId(), async () => {
const enriched = await outputProcessing(table, [row]) const enriched = await outputProcessing(table, [row])
expect((enriched as Row[])[0].attachment[0].url).toBe( expect((enriched as Row[])[0]?.[field][0].url).toBe(
`/files/signed/prod-budi-app-assets/${config.getProdAppId()}/attachments/${attachmentId}` `/files/signed/prod-budi-app-assets/${config.getProdAppId()}/attachments/${attachmentId}`
) )
}) })
}) })
}
it("should allow enriching attachment rows", async () => {
coreAttachmentEnrichment(
{
attachment: {
type: FieldType.ATTACHMENT,
name: "attachment",
constraints: { type: "array", presence: false },
},
},
"attachment",
`${uuid.v4()}.csv`
)
})
it("should allow enriching signature rows", async () => {
coreAttachmentEnrichment(
{
signature: {
type: FieldType.SIGNATURE,
name: "signature",
constraints: { type: "array", presence: false },
},
},
"signature",
`${uuid.v4()}.png`
)
}) })
}) })

View File

@ -30,7 +30,10 @@ export async function getRowsWithAttachments(appId: string, table: Table) {
const db = dbCore.getDB(appId) const db = dbCore.getDB(appId)
const attachmentCols: string[] = [] const attachmentCols: string[] = []
for (let [key, column] of Object.entries(table.schema)) { for (let [key, column] of Object.entries(table.schema)) {
if (column.type === FieldType.ATTACHMENT) { if (
column.type === FieldType.ATTACHMENT ||
column.type === FieldType.SIGNATURE
) {
attachmentCols.push(key) attachmentCols.push(key)
} }
} }

View File

@ -23,27 +23,18 @@ describe("should be able to re-write attachment URLs", () => {
await config.init() await config.init()
}) })
it("should update URLs on a number of rows over the limit", async () => { const coreBehaviour = async (tblSchema: any, field: string) => {
const table = await config.api.table.save({ const table = await config.api.table.save({
name: "photos", name: "photos",
type: "table", type: "table",
sourceId: INTERNAL_TABLE_SOURCE_ID, sourceId: INTERNAL_TABLE_SOURCE_ID,
sourceType: TableSourceType.INTERNAL, sourceType: TableSourceType.INTERNAL,
schema: { schema: tblSchema,
photo: {
type: FieldType.ATTACHMENT,
name: "photo",
},
otherCol: {
type: FieldType.STRING,
name: "otherCol",
},
},
}) })
for (let i = 0; i < FIND_LIMIT * 4; i++) { for (let i = 0; i < FIND_LIMIT * 4; i++) {
await config.api.row.save(table._id!, { await config.api.row.save(table._id!, {
photo: [attachment], [field]: [attachment],
otherCol: "string", otherCol: "string",
}) })
} }
@ -56,8 +47,39 @@ describe("should be able to re-write attachment URLs", () => {
) )
for (const row of rows) { for (const row of rows) {
expect(row.otherCol).toBe("string") expect(row.otherCol).toBe("string")
expect(row.photo[0].url).toBe("") expect(row[field][0].url).toBe("")
expect(row.photo[0].key).toBe(`${db.name}/attachments/a.png`) expect(row[field][0].key).toBe(`${db.name}/attachments/a.png`)
} }
}
it("Attachment field, should update URLs on a number of rows over the limit", async () => {
await coreBehaviour(
{
photo: {
type: FieldType.ATTACHMENT,
name: "photo",
},
otherCol: {
type: FieldType.STRING,
name: "otherCol",
},
},
"photo"
)
})
it("Signature field, should update URLs on a number of rows over the limit", async () => {
await coreBehaviour(
{
signature: {
type: FieldType.SIGNATURE,
name: "signature",
},
otherCol: {
type: FieldType.STRING,
name: "otherCol",
},
},
"signature"
)
}) })
}) })

View File

@ -95,7 +95,10 @@ export class AttachmentCleanup {
return AttachmentCleanup.coreCleanup(() => { return AttachmentCleanup.coreCleanup(() => {
let files: string[] = [] let files: string[] = []
for (let [key, schema] of Object.entries(table.schema)) { for (let [key, schema] of Object.entries(table.schema)) {
if (schema.type !== FieldType.ATTACHMENT) { if (
schema.type !== FieldType.ATTACHMENT &&
schema.type !== FieldType.SIGNATURE
) {
continue continue
} }
const oldKeys = const oldKeys =