Fix test that doesn't work if you run it with reuse containers.

This commit is contained in:
Sam Rose 2024-04-03 16:09:12 +01:00
parent 193d40f86c
commit 6d41b890db
No known key found for this signature in database
1 changed files with 45 additions and 58 deletions

View File

@ -1,4 +1,12 @@
import newid from "../../db/newid"
import TestConfig from "../../tests/utilities/TestConfiguration"
import { db as dbCore } from "@budibase/backend-core"
import sdk from "../index"
import {
FieldType,
INTERNAL_TABLE_SOURCE_ID,
TableSourceType,
} from "@budibase/types"
import { FIND_LIMIT } from "../app/rows/attachments"
const attachment = {
size: 73479,
@ -8,69 +16,48 @@ const attachment = {
key: "app_bbb/attachments/a.png",
}
const row = {
_id: "ro_ta_aaa",
photo: [attachment],
otherCol: "string",
}
describe("should be able to re-write attachment URLs", () => {
const config = new TestConfig()
const table = {
_id: "ta_aaa",
beforeAll(async () => {
await config.init()
})
it("should update URLs on a number of rows over the limit", async () => {
const table = await config.api.table.save({
name: "photos",
type: "table",
sourceId: INTERNAL_TABLE_SOURCE_ID,
sourceType: TableSourceType.INTERNAL,
schema: {
photo: {
type: "attachment",
type: FieldType.ATTACHMENT,
name: "photo",
},
otherCol: {
type: "string",
type: FieldType.STRING,
name: "otherCol",
},
},
}
jest.mock("@budibase/backend-core", () => {
const core = jest.requireActual("@budibase/backend-core")
return {
...core,
db: {
...core.db,
directCouchFind: jest.fn(),
},
}
})
import { db as dbCore } from "@budibase/backend-core"
import sdk from "../index"
describe("should be able to re-write attachment URLs", () => {
it("should update URLs on a number of rows over the limit", async () => {
const db = dbCore.getDB("app_aaa")
await db.put(table)
const limit = 30
let rows = []
for (let i = 0; i < limit; i++) {
const rowToWrite = {
...row,
_id: `${row._id}_${newid()}`,
}
const { rev } = await db.put(rowToWrite)
rows.push({
...rowToWrite,
_rev: rev,
for (let i = 0; i < FIND_LIMIT * 4; i++) {
await config.api.row.save(table._id!, {
photo: [attachment],
otherCol: "string",
})
}
dbCore.directCouchFind
// @ts-ignore
.mockReturnValueOnce({ rows: rows.slice(0, 25), bookmark: "aaa" })
.mockReturnValueOnce({ rows: rows.slice(25, limit), bookmark: "bbb" })
const db = dbCore.getDB(config.getAppId())
await sdk.backups.updateAttachmentColumns(db.name, db)
const finalRows = await sdk.rows.getAllInternalRows(db.name)
for (let rowToCheck of finalRows) {
expect(rowToCheck.otherCol).toBe(row.otherCol)
expect(rowToCheck.photo[0].url).toBe("")
expect(rowToCheck.photo[0].key).toBe(`${db.name}/attachments/a.png`)
const rows = (await sdk.rows.getAllInternalRows(db.name)).filter(
row => row.tableId === table._id
)
for (const row of rows) {
expect(row.otherCol).toBe("string")
expect(row.photo[0].url).toBe("")
expect(row.photo[0].key).toBe(`${db.name}/attachments/a.png`)
}
})
})