Adding test cases for the attachment re-write feature, mocking the direct couch find functionality.

This commit is contained in:
mike12345567 2023-01-06 13:33:22 +00:00
parent cf90282303
commit 76825c38b9
5 changed files with 101 additions and 2 deletions

View File

@ -1,5 +1,5 @@
const setup = require("./utilities")
const { events, constants, db } = require("@budibase/backend-core")
const { events, constants } = require("@budibase/backend-core")
describe("/static", () => {
let request = setup.getRequest()

View File

@ -43,7 +43,7 @@ function rewriteAttachmentUrl(appId: string, attachment: RowAttachment) {
}
}
async function updateAttachmentColumns(prodAppId: string, db: Database) {
export async function updateAttachmentColumns(prodAppId: string, db: Database) {
// iterate through attachment documents and update them
const tables = await sdk.tables.getAllInternalTables(db)
let updatedRows: Row[] = []

View File

@ -1,5 +1,7 @@
import * as attachments from "./attachments"
import * as rows from "./rows"
export default {
...attachments,
...rows,
}

View File

@ -0,0 +1,18 @@
import { db as dbCore, context } from "@budibase/backend-core"
import { Database, Row } from "@budibase/types"
import { getRowParams } from "../../../db/utils"
export async function getAllInternalRows(appId?: string) {
let db: Database
if (appId) {
db = dbCore.getDB(appId)
} else {
db = context.getAppDB()
}
const response = await db.allDocs(
getRowParams(null, null, {
include_docs: true,
})
)
return response.rows.map(row => row.doc) as Row[]
}

View File

@ -0,0 +1,79 @@
import newid from "../../db/newid"
const attachment = {
size: 73479,
name: "2022-12-14 11_11_44-.png",
url: "/prod-budi-app-assets/app_bbb/attachments/a.png",
extension: "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", () => {
it("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,
})
}
dbCore.directCouchFind
// @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)
const finalRows = await sdk.rows.getAllInternalRows(db.name)
for (let rowToCheck of finalRows) {
expect(rowToCheck.otherCol).toBe(row.otherCol)
expect(rowToCheck.photo[0].url).not.toBe(row.photo[0].url)
expect(rowToCheck.photo[0].url).toBe(
`/prod-budi-app-assets/${db.name}/attachments/a.png`
)
expect(rowToCheck.photo[0].key).toBe(`${db.name}/attachments/a.png`)
}
})
})