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