Final server test fixes, after all updates to context/removal of context faking.

This commit is contained in:
mike12345567 2022-11-15 19:04:39 +00:00
parent 8b2cfd9dfa
commit cca1b59c76
4 changed files with 87 additions and 62 deletions

View File

@ -1,4 +1,3 @@
const env = require("../../environment")
const setup = require("./utilities") const setup = require("./utilities")
describe("test the update row action", () => { describe("test the update row action", () => {

View File

@ -1,6 +1,5 @@
const TestConfig = require("../../../tests/utilities/TestConfiguration") const TestConfig = require("../../../tests/utilities/TestConfiguration")
const { TENANT_ID } = require("../../../tests/utilities/structures") const { context } = require("@budibase/backend-core")
const { doInTenant } = require("@budibase/backend-core/tenancy")
const actions = require("../../actions") const actions = require("../../actions")
const emitter = require("../../../events/index") const emitter = require("../../../events/index")
const env = require("../../../environment") const env = require("../../../environment")
@ -33,7 +32,7 @@ exports.runInProd = async fn => {
} }
exports.runStep = async function runStep(stepId, inputs) { exports.runStep = async function runStep(stepId, inputs) {
return doInTenant(TENANT_ID, async () => { async function run() {
let step = await actions.getAction(stepId) let step = await actions.getAction(stepId)
expect(step).toBeDefined() expect(step).toBeDefined()
return step({ return step({
@ -43,7 +42,14 @@ exports.runStep = async function runStep(stepId, inputs) {
apiKey: exports.apiKey, apiKey: exports.apiKey,
emitter, emitter,
}) })
}) }
if (config?.appId) {
return context.doInContext(config?.appId, async () => {
return run()
})
} else {
return run()
}
} }
exports.apiKey = "test" exports.apiKey = "test"

View File

@ -1,15 +1,17 @@
const TestConfig = require("../../tests/utilities/TestConfiguration") const TestConfig = require("../../tests/utilities/TestConfiguration")
const { basicRow, basicLinkedRow, basicTable } = require("../../tests/utilities/structures") const { basicRow, basicLinkedRow, basicTable } = require("../../tests/utilities/structures")
const LinkController = require("../linkedRows/LinkController") const LinkController = require("../linkedRows/LinkController")
const { context } = require("@budibase/backend-core")
const { RelationshipTypes } = require("../../constants") const { RelationshipTypes } = require("../../constants")
const { cloneDeep } = require("lodash/fp") const { cloneDeep } = require("lodash/fp")
describe("test the link controller", () => { describe("test the link controller", () => {
let config = new TestConfig(false) let config = new TestConfig(false)
let table1, table2 let table1, table2, appId
beforeEach(async () => { beforeEach(async () => {
await config.init() const app = await config.init()
appId = app.appId
const { _id } = await config.createTable() const { _id } = await config.createTable()
table2 = await config.createLinkedTable(RelationshipTypes.MANY_TO_MANY, ["link", "link2"]) table2 = await config.createLinkedTable(RelationshipTypes.MANY_TO_MANY, ["link", "link2"])
// update table after creating link // update table after creating link
@ -18,18 +20,20 @@ describe("test the link controller", () => {
afterAll(config.end) afterAll(config.end)
function createLinkController(table, row = null, oldTable = null) { async function createLinkController(table, row = null, oldTable = null) {
const linkConfig = { return context.doInAppContext(appId, () => {
tableId: table._id, const linkConfig = {
table, tableId: table._id,
} table,
if (row) { }
linkConfig.row = row if (row) {
} linkConfig.row = row
if (oldTable) { }
linkConfig.oldTable = oldTable if (oldTable) {
} linkConfig.oldTable = oldTable
return new LinkController(linkConfig) }
return new LinkController(linkConfig)
})
} }
async function createLinkedRow(linkField = "link", t1 = table1, t2 = table2) { async function createLinkedRow(linkField = "link", t1 = table1, t2 = table2) {
@ -38,16 +42,16 @@ describe("test the link controller", () => {
return config.getRow(t1._id, _id) return config.getRow(t1._id, _id)
} }
it("should be able to confirm if two table schemas are equal", () => { it("should be able to confirm if two table schemas are equal", async () => {
const controller = createLinkController(table1) const controller = await createLinkController(table1)
let equal = controller.areLinkSchemasEqual(table2.schema.link, table2.schema.link) let equal = controller.areLinkSchemasEqual(table2.schema.link, table2.schema.link)
expect(equal).toEqual(true) expect(equal).toEqual(true)
equal = controller.areLinkSchemasEqual(table1.schema.link, table2.schema.link) equal = controller.areLinkSchemasEqual(table1.schema.link, table2.schema.link)
expect(equal).toEqual(false) expect(equal).toEqual(false)
}) })
it("should be able to check the relationship types across two fields", () => { it("should be able to check the relationship types across two fields", async () => {
const controller = createLinkController(table1) const controller = await createLinkController(table1)
// empty case // empty case
let output = controller.handleRelationshipType({}, {}) let output = controller.handleRelationshipType({}, {})
expect(output.linkedField.relationshipType).toEqual(RelationshipTypes.MANY_TO_MANY) expect(output.linkedField.relationshipType).toEqual(RelationshipTypes.MANY_TO_MANY)
@ -65,29 +69,33 @@ describe("test the link controller", () => {
it("should be able to delete a row", async () => { it("should be able to delete a row", async () => {
const row = await createLinkedRow() const row = await createLinkedRow()
const controller = createLinkController(table1, row) const controller = await createLinkController(table1, row)
// get initial count await context.doInAppContext(appId, async () => {
const beforeLinks = await controller.getRowLinkDocs(row._id) // get initial count
await controller.rowDeleted() const beforeLinks = await controller.getRowLinkDocs(row._id)
let afterLinks = await controller.getRowLinkDocs(row._id) await controller.rowDeleted()
expect(beforeLinks.length).toEqual(1) let afterLinks = await controller.getRowLinkDocs(row._id)
expect(afterLinks.length).toEqual(0) expect(beforeLinks.length).toEqual(1)
expect(afterLinks.length).toEqual(0)
})
}) })
it("shouldn't throw an error when deleting a row with no links", async () => { it("shouldn't throw an error when deleting a row with no links", async () => {
const row = await config.createRow(basicRow(table1._id)) const row = await config.createRow(basicRow(table1._id))
const controller = createLinkController(table1, row) const controller = await createLinkController(table1, row)
let error await context.doInAppContext(appId, async () => {
try { let error
await controller.rowDeleted() try {
} catch (err) { await controller.rowDeleted()
error = err } catch (err) {
} error = err
expect(error).toBeUndefined() }
expect(error).toBeUndefined()
})
}) })
it("should throw an error when validating a table which is invalid", () => { it("should throw an error when validating a table which is invalid", async () => {
const controller = createLinkController(table1) const controller = await createLinkController(table1)
const copyTable = { const copyTable = {
...table1 ...table1
} }
@ -110,32 +118,38 @@ describe("test the link controller", () => {
const row = await createLinkedRow() const row = await createLinkedRow()
// remove the link from the row // remove the link from the row
row.link = [] row.link = []
const controller = createLinkController(table1, row) const controller = await createLinkController(table1, row)
await controller.rowSaved() await context.doInAppContext(appId, async () => {
let links = await controller.getRowLinkDocs(row._id) await controller.rowSaved()
expect(links.length).toEqual(0) let links = await controller.getRowLinkDocs(row._id)
expect(links.length).toEqual(0)
})
}) })
it("should be able to delete a table and have links deleted", async () => { it("should be able to delete a table and have links deleted", async () => {
await createLinkedRow() await createLinkedRow()
const controller = createLinkController(table1) const controller = await createLinkController(table1)
let before = await controller.getTableLinkDocs() await context.doInAppContext(appId, async () => {
await controller.tableDeleted() let before = await controller.getTableLinkDocs()
let after = await controller.getTableLinkDocs() await controller.tableDeleted()
expect(before.length).toEqual(1) let after = await controller.getTableLinkDocs()
expect(after.length).toEqual(0) expect(before.length).toEqual(1)
expect(after.length).toEqual(0)
})
}) })
it("should be able to remove a linked field from a table", async () => { it("should be able to remove a linked field from a table", async () => {
await createLinkedRow() await createLinkedRow()
await createLinkedRow("link2") await createLinkedRow("link2")
const controller = createLinkController(table1, null, table1) const controller = await createLinkController(table1, null, table1)
let before = await controller.getTableLinkDocs() await context.doInAppContext(appId, async () => {
await controller.removeFieldFromTable("link") let before = await controller.getTableLinkDocs()
let after = await controller.getTableLinkDocs() await controller.removeFieldFromTable("link")
expect(before.length).toEqual(2) let after = await controller.getTableLinkDocs()
// shouldn't delete the other field expect(before.length).toEqual(2)
expect(after.length).toEqual(1) // shouldn't delete the other field
expect(after.length).toEqual(1)
})
}) })
it("should throw an error when overwriting a link column", async () => { it("should throw an error when overwriting a link column", async () => {
@ -143,7 +157,7 @@ describe("test the link controller", () => {
update.schema.link.relationshipType = RelationshipTypes.MANY_TO_ONE update.schema.link.relationshipType = RelationshipTypes.MANY_TO_ONE
let error let error
try { try {
const controller = createLinkController(update) const controller = await createLinkController(update)
await controller.tableSaved() await controller.tableSaved()
} catch (err) { } catch (err) {
error = err error = err
@ -156,10 +170,12 @@ describe("test the link controller", () => {
await createLinkedRow() await createLinkedRow()
const newTable = cloneDeep(table1) const newTable = cloneDeep(table1)
delete newTable.schema.link delete newTable.schema.link
const controller = createLinkController(newTable, null, table1) const controller = await createLinkController(newTable, null, table1)
await controller.tableUpdated() await context.doInAppContext(appId, async () => {
const links = await controller.getTableLinkDocs() await controller.tableUpdated()
expect(links.length).toEqual(0) const links = await controller.getTableLinkDocs()
expect(links.length).toEqual(0)
})
}) })
it("shouldn't allow one to many having many relationships against it", async () => { it("shouldn't allow one to many having many relationships against it", async () => {

View File

@ -40,6 +40,7 @@ function mockAuthWithNoCookie() {
return { return {
...core, ...core,
db: { db: {
...core.db,
dbExists: () => true, dbExists: () => true,
}, },
cache: { cache: {
@ -68,6 +69,7 @@ function mockAuthWithCookie() {
return { return {
...core, ...core,
db: { db: {
...core.db,
dbExists: () => true, dbExists: () => true,
}, },
utils: { utils: {
@ -183,6 +185,7 @@ describe("Current app middleware", () => {
return { return {
...core, ...core,
db: { db: {
...core.db,
dbExists: () => true, dbExists: () => true,
}, },
utils: { utils: {
@ -213,6 +216,7 @@ describe("Current app middleware", () => {
return { return {
...core, ...core,
db: { db: {
...core.db,
dbExists: () => true, dbExists: () => true,
}, },
utils: { utils: {