Fix tests.

This commit is contained in:
Sam Rose 2024-08-29 15:45:19 +01:00
parent f9f528e880
commit bced81d241
No known key found for this signature in database
2 changed files with 243 additions and 222 deletions

View File

@ -4,6 +4,7 @@ import {
cache, cache,
context, context,
db, db,
features,
HTTPError, HTTPError,
objectStore, objectStore,
utils, utils,
@ -350,7 +351,7 @@ export async function outputProcessing<T extends Row[] | Row>(
} }
// remove null properties to match internal API // remove null properties to match internal API
const isExternal = isExternalTableID(table._id!) const isExternal = isExternalTableID(table._id!)
if (isExternal || db.isSqsEnabledForTenant()) { if (isExternal || (await features.flags.isEnabled("SQS"))) {
for (const row of enriched) { for (const row of enriched) {
for (const key of Object.keys(row)) { for (const key of Object.keys(row)) {
if (row[key] === null) { if (row[key] === null) {

View File

@ -8,15 +8,9 @@ import {
} from "@budibase/types" } from "@budibase/types"
import { outputProcessing } from ".." import { outputProcessing } from ".."
import { generator, structures } from "@budibase/backend-core/tests" import { generator, structures } from "@budibase/backend-core/tests"
import { setEnv as setCoreEnv } from "@budibase/backend-core"
import * as bbReferenceProcessor from "../bbReferenceProcessor" import * as bbReferenceProcessor from "../bbReferenceProcessor"
import TestConfiguration from "../../../tests/utilities/TestConfiguration"
jest.mock("@budibase/backend-core", () => ({
...jest.requireActual("@budibase/backend-core"),
db: {
...jest.requireActual("@budibase/backend-core").db,
isSqsEnabledForTenant: () => true,
},
}))
jest.mock("../bbReferenceProcessor", (): typeof bbReferenceProcessor => ({ jest.mock("../bbReferenceProcessor", (): typeof bbReferenceProcessor => ({
processInputBBReference: jest.fn(), processInputBBReference: jest.fn(),
@ -26,8 +20,24 @@ jest.mock("../bbReferenceProcessor", (): typeof bbReferenceProcessor => ({
})) }))
describe("rowProcessor - outputProcessing", () => { describe("rowProcessor - outputProcessing", () => {
const config = new TestConfiguration()
let cleanupEnv: () => void = () => {}
beforeAll(async () => {
await config.init()
})
afterAll(async () => {
config.end()
})
beforeEach(() => { beforeEach(() => {
jest.resetAllMocks() jest.resetAllMocks()
cleanupEnv = setCoreEnv({ TENANT_FEATURE_FLAGS: "*SQS" })
})
afterEach(() => {
cleanupEnv()
}) })
const processOutputBBReferenceMock = const processOutputBBReferenceMock =
@ -36,266 +46,276 @@ describe("rowProcessor - outputProcessing", () => {
bbReferenceProcessor.processOutputBBReferences as jest.Mock bbReferenceProcessor.processOutputBBReferences as jest.Mock
it("fetches single user references given a populated field", async () => { it("fetches single user references given a populated field", async () => {
const table: Table = { await config.doInContext(config.getAppId(), async () => {
_id: generator.guid(), const table: Table = {
name: "TestTable", _id: generator.guid(),
type: "table", name: "TestTable",
sourceId: INTERNAL_TABLE_SOURCE_ID, type: "table",
sourceType: TableSourceType.INTERNAL, sourceId: INTERNAL_TABLE_SOURCE_ID,
schema: { sourceType: TableSourceType.INTERNAL,
name: { schema: {
type: FieldType.STRING, name: {
name: "name", type: FieldType.STRING,
constraints: { name: "name",
presence: true, constraints: {
type: "string", presence: true,
type: "string",
},
},
user: {
type: FieldType.BB_REFERENCE_SINGLE,
subtype: BBReferenceFieldSubType.USER,
name: "user",
constraints: {
presence: false,
type: "string",
},
}, },
}, },
user: { }
type: FieldType.BB_REFERENCE_SINGLE,
subtype: BBReferenceFieldSubType.USER,
name: "user",
constraints: {
presence: false,
type: "string",
},
},
},
}
const row = { const row = {
name: "Jack", name: "Jack",
user: "123", user: "123",
} }
const user = structures.users.user() const user = structures.users.user()
processOutputBBReferenceMock.mockResolvedValue(user) processOutputBBReferenceMock.mockResolvedValue(user)
const result = await outputProcessing(table, row, { squash: false }) const result = await outputProcessing(table, row, { squash: false })
expect(result).toEqual({ name: "Jack", user }) expect(result).toEqual({ name: "Jack", user })
expect(bbReferenceProcessor.processOutputBBReference).toHaveBeenCalledTimes( expect(
1 bbReferenceProcessor.processOutputBBReference
) ).toHaveBeenCalledTimes(1)
expect(bbReferenceProcessor.processOutputBBReference).toHaveBeenCalledWith( expect(
"123", bbReferenceProcessor.processOutputBBReference
BBReferenceFieldSubType.USER ).toHaveBeenCalledWith("123", BBReferenceFieldSubType.USER)
) })
}) })
it("fetches users references given a populated field", async () => { it("fetches users references given a populated field", async () => {
const table: Table = { await config.doInContext(config.getAppId(), async () => {
_id: generator.guid(), const table: Table = {
name: "TestTable", _id: generator.guid(),
type: "table", name: "TestTable",
sourceId: INTERNAL_TABLE_SOURCE_ID, type: "table",
sourceType: TableSourceType.INTERNAL, sourceId: INTERNAL_TABLE_SOURCE_ID,
schema: { sourceType: TableSourceType.INTERNAL,
name: { schema: {
type: FieldType.STRING, name: {
name: "name", type: FieldType.STRING,
constraints: { name: "name",
presence: true, constraints: {
type: "string", presence: true,
type: "string",
},
},
users: {
type: FieldType.BB_REFERENCE,
subtype: BBReferenceFieldSubType.USER,
name: "users",
constraints: {
presence: false,
type: "string",
},
}, },
}, },
users: { }
type: FieldType.BB_REFERENCE,
subtype: BBReferenceFieldSubType.USER,
name: "users",
constraints: {
presence: false,
type: "string",
},
},
},
}
const row = { const row = {
name: "Jack", name: "Jack",
users: "123", users: "123",
} }
const users = [structures.users.user()] const users = [structures.users.user()]
processOutputBBReferencesMock.mockResolvedValue(users) processOutputBBReferencesMock.mockResolvedValue(users)
const result = await outputProcessing(table, row, { squash: false }) const result = await outputProcessing(table, row, { squash: false })
expect(result).toEqual({ name: "Jack", users }) expect(result).toEqual({ name: "Jack", users })
expect( expect(
bbReferenceProcessor.processOutputBBReferences bbReferenceProcessor.processOutputBBReferences
).toHaveBeenCalledTimes(1) ).toHaveBeenCalledTimes(1)
expect(bbReferenceProcessor.processOutputBBReferences).toHaveBeenCalledWith( expect(
"123", bbReferenceProcessor.processOutputBBReferences
BBReferenceFieldSubType.USER ).toHaveBeenCalledWith("123", BBReferenceFieldSubType.USER)
) })
}) })
it("should handle attachment list correctly", async () => { it("should handle attachment list correctly", async () => {
const table: Table = { await config.doInContext(config.getAppId(), async () => {
_id: generator.guid(), const table: Table = {
name: "TestTable", _id: generator.guid(),
type: "table", name: "TestTable",
sourceId: INTERNAL_TABLE_SOURCE_ID, type: "table",
sourceType: TableSourceType.INTERNAL, sourceId: INTERNAL_TABLE_SOURCE_ID,
schema: { sourceType: TableSourceType.INTERNAL,
attach: { schema: {
type: FieldType.ATTACHMENTS, attach: {
name: "attach", type: FieldType.ATTACHMENTS,
constraints: {}, name: "attach",
constraints: {},
},
}, },
}, }
}
const row: { attach: RowAttachment[] } = { const row: { attach: RowAttachment[] } = {
attach: [ attach: [
{ {
size: 10,
name: "test",
extension: "jpg",
key: "test.jpg",
},
],
}
const output = await outputProcessing(table, row, { squash: false })
expect(output.attach[0].url?.split("?")[0]).toBe(
"/files/signed/prod-budi-app-assets/test.jpg"
)
row.attach[0].url = ""
const output2 = await outputProcessing(table, row, { squash: false })
expect(output2.attach[0].url?.split("?")[0]).toBe(
"/files/signed/prod-budi-app-assets/test.jpg"
)
row.attach[0].url = "aaaa"
const output3 = await outputProcessing(table, row, { squash: false })
expect(output3.attach[0].url).toBe("aaaa")
})
})
it("should handle single attachment correctly", async () => {
await config.doInContext(config.getAppId(), async () => {
const table: Table = {
_id: generator.guid(),
name: "TestTable",
type: "table",
sourceId: INTERNAL_TABLE_SOURCE_ID,
sourceType: TableSourceType.INTERNAL,
schema: {
attach: {
type: FieldType.ATTACHMENT_SINGLE,
name: "attach",
constraints: {},
},
},
}
const row: { attach: RowAttachment } = {
attach: {
size: 10, size: 10,
name: "test", name: "test",
extension: "jpg", extension: "jpg",
key: "test.jpg", key: "test.jpg",
}, },
], }
}
const output = await outputProcessing(table, row, { squash: false }) const output = await outputProcessing(table, row, { squash: false })
expect(output.attach[0].url?.split("?")[0]).toBe( expect(output.attach.url?.split("?")[0]).toBe(
"/files/signed/prod-budi-app-assets/test.jpg" "/files/signed/prod-budi-app-assets/test.jpg"
) )
row.attach[0].url = "" row.attach.url = ""
const output2 = await outputProcessing(table, row, { squash: false }) const output2 = await outputProcessing(table, row, { squash: false })
expect(output2.attach[0].url?.split("?")[0]).toBe( expect(output2.attach?.url?.split("?")[0]).toBe(
"/files/signed/prod-budi-app-assets/test.jpg" "/files/signed/prod-budi-app-assets/test.jpg"
) )
row.attach[0].url = "aaaa" row.attach.url = "aaaa"
const output3 = await outputProcessing(table, row, { squash: false }) const output3 = await outputProcessing(table, row, { squash: false })
expect(output3.attach[0].url).toBe("aaaa") expect(output3.attach.url).toBe("aaaa")
}) })
it("should handle single attachment correctly", async () => {
const table: Table = {
_id: generator.guid(),
name: "TestTable",
type: "table",
sourceId: INTERNAL_TABLE_SOURCE_ID,
sourceType: TableSourceType.INTERNAL,
schema: {
attach: {
type: FieldType.ATTACHMENT_SINGLE,
name: "attach",
constraints: {},
},
},
}
const row: { attach: RowAttachment } = {
attach: {
size: 10,
name: "test",
extension: "jpg",
key: "test.jpg",
},
}
const output = await outputProcessing(table, row, { squash: false })
expect(output.attach.url?.split("?")[0]).toBe(
"/files/signed/prod-budi-app-assets/test.jpg"
)
row.attach.url = ""
const output2 = await outputProcessing(table, row, { squash: false })
expect(output2.attach?.url?.split("?")[0]).toBe(
"/files/signed/prod-budi-app-assets/test.jpg"
)
row.attach.url = "aaaa"
const output3 = await outputProcessing(table, row, { squash: false })
expect(output3.attach.url).toBe("aaaa")
}) })
it("process output even when the field is not empty", async () => { it("process output even when the field is not empty", async () => {
const table: Table = { await config.doInContext(config.getAppId(), async () => {
_id: generator.guid(), const table: Table = {
name: "TestTable", _id: generator.guid(),
type: "table", name: "TestTable",
sourceId: INTERNAL_TABLE_SOURCE_ID, type: "table",
sourceType: TableSourceType.INTERNAL, sourceId: INTERNAL_TABLE_SOURCE_ID,
schema: { sourceType: TableSourceType.INTERNAL,
name: { schema: {
type: FieldType.STRING, name: {
name: "name", type: FieldType.STRING,
constraints: { name: "name",
presence: true, constraints: {
type: "string", presence: true,
type: "string",
},
},
user: {
type: FieldType.BB_REFERENCE,
subtype: BBReferenceFieldSubType.USER,
name: "user",
constraints: {
presence: false,
type: "string",
},
}, },
}, },
user: { }
type: FieldType.BB_REFERENCE,
subtype: BBReferenceFieldSubType.USER,
name: "user",
constraints: {
presence: false,
type: "string",
},
},
},
}
const row = { const row = {
name: "Jack", name: "Jack",
} }
const result = await outputProcessing(table, row, { squash: false }) const result = await outputProcessing(table, row, { squash: false })
expect(result).toEqual({ name: "Jack" }) expect(result).toEqual({ name: "Jack" })
expect( expect(
bbReferenceProcessor.processOutputBBReferences bbReferenceProcessor.processOutputBBReferences
).toHaveBeenCalledTimes(1) ).toHaveBeenCalledTimes(1)
})
}) })
it("does not fetch bb references when not in the schema", async () => { it("does not fetch bb references when not in the schema", async () => {
const table: Table = { await config.doInContext(config.getAppId(), async () => {
_id: generator.guid(), const table: Table = {
name: "TestTable", _id: generator.guid(),
type: "table", name: "TestTable",
sourceId: INTERNAL_TABLE_SOURCE_ID, type: "table",
sourceType: TableSourceType.INTERNAL, sourceId: INTERNAL_TABLE_SOURCE_ID,
schema: { sourceType: TableSourceType.INTERNAL,
name: { schema: {
type: FieldType.STRING, name: {
name: "name", type: FieldType.STRING,
constraints: { name: "name",
presence: true, constraints: {
type: "string", presence: true,
type: "string",
},
},
user: {
type: FieldType.NUMBER,
name: "user",
constraints: {
presence: false,
type: "string",
},
}, },
}, },
user: { }
type: FieldType.NUMBER,
name: "user",
constraints: {
presence: false,
type: "string",
},
},
},
}
const row = { const row = {
name: "Jack", name: "Jack",
user: "123", user: "123",
} }
const result = await outputProcessing(table, row, { squash: false }) const result = await outputProcessing(table, row, { squash: false })
expect(result).toEqual({ name: "Jack", user: "123" }) expect(result).toEqual({ name: "Jack", user: "123" })
expect( expect(
bbReferenceProcessor.processOutputBBReferences bbReferenceProcessor.processOutputBBReferences
).not.toHaveBeenCalled() ).not.toHaveBeenCalled()
})
}) })
}) })