From f8fdae06040d0536d9127ff62741f60df1bafa4d Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 17 Jun 2021 16:35:58 +0100 Subject: [PATCH] automation server tests updated --- .../server/src/api/controllers/row/index.js | 4 +- .../__snapshots__/datasource.spec.js.snap | 92 +++++++++++++++++++ .../src/api/routes/tests/datasource.spec.js | 15 +-- .../server/src/api/routes/tests/table.spec.js | 2 +- .../routes/tests/utilities/TestFunctions.js | 2 +- .../src/tests/utilities/TestConfiguration.js | 2 +- 6 files changed, 101 insertions(+), 16 deletions(-) create mode 100644 packages/server/src/api/routes/tests/__snapshots__/datasource.spec.js.snap diff --git a/packages/server/src/api/controllers/row/index.js b/packages/server/src/api/controllers/row/index.js index 1d4b868e0a..a50afa77ea 100644 --- a/packages/server/src/api/controllers/row/index.js +++ b/packages/server/src/api/controllers/row/index.js @@ -31,9 +31,10 @@ exports.patch = async ctx => { } try { const { row, table } = await pickApi(tableId).patch(ctx) + ctx.status = 200 ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:update`, appId, row, table) - ctx.message = `${table.name} updated successfully` + ctx.message = `${table.name} updated successfully.` ctx.body = row } catch (err) { ctx.throw(400, err) @@ -50,6 +51,7 @@ exports.save = async function (ctx) { } try { const { row, table } = await pickApi(tableId).save(ctx) + ctx.status = 200 ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:save`, appId, row, table) ctx.message = `${table.name} saved successfully` ctx.body = row diff --git a/packages/server/src/api/routes/tests/__snapshots__/datasource.spec.js.snap b/packages/server/src/api/routes/tests/__snapshots__/datasource.spec.js.snap new file mode 100644 index 0000000000..89d860b4ef --- /dev/null +++ b/packages/server/src/api/routes/tests/__snapshots__/datasource.spec.js.snap @@ -0,0 +1,92 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`/datasources fetch returns all the datasources from the server 1`] = ` +Array [ + Object { + "_id": "bb_internal", + "config": Object {}, + "entities": Array [ + Object { + "_id": "ta_users", + "_rev": "1-039883a06c1f9cb3945731d79838181e", + "name": "Users", + "primaryDisplay": "email", + "schema": Object { + "email": Object { + "constraints": Object { + "email": true, + "length": Object { + "maximum": "", + }, + "presence": true, + "type": "string", + }, + "fieldName": "email", + "name": "email", + "type": "string", + }, + "firstName": Object { + "constraints": Object { + "presence": false, + "type": "string", + }, + "fieldName": "firstName", + "name": "firstName", + "type": "string", + }, + "lastName": Object { + "constraints": Object { + "presence": false, + "type": "string", + }, + "fieldName": "lastName", + "name": "lastName", + "type": "string", + }, + "roleId": Object { + "constraints": Object { + "inclusion": Array [ + "ADMIN", + "POWER", + "BASIC", + "PUBLIC", + ], + "presence": false, + "type": "string", + }, + "fieldName": "roleId", + "name": "roleId", + "type": "options", + }, + "status": Object { + "constraints": Object { + "inclusion": Array [ + "active", + "inactive", + ], + "presence": false, + "type": "string", + }, + "fieldName": "status", + "name": "status", + "type": "options", + }, + }, + "type": "table", + "views": Object {}, + }, + ], + "name": "Budibase DB", + "source": "BUDIBASE", + "type": "budibase", + }, + Object { + "_id": "datasource_f8f81b1f0893478580b863fe96f1f3da", + "_rev": "1-1df90f81a2294ba7349f690f4a6df092", + "config": Object {}, + "name": "Test", + "source": "POSTGRES", + "type": "datasource", + }, +] +`; diff --git a/packages/server/src/api/routes/tests/datasource.spec.js b/packages/server/src/api/routes/tests/datasource.spec.js index 94235f8d35..7926925ccb 100644 --- a/packages/server/src/api/routes/tests/datasource.spec.js +++ b/packages/server/src/api/routes/tests/datasource.spec.js @@ -40,13 +40,7 @@ describe("/datasources", () => { .expect(200) const datasources = res.body - expect(datasources).toEqual([ - { - "_id": datasources[0]._id, - "_rev": datasources[0]._rev, - ...basicDatasource() - } - ]) + expect(datasources).toMatchSnapshot() }) it("should apply authorization to endpoint", async () => { @@ -93,10 +87,7 @@ describe("/datasources", () => { .expect(200) // this is mock data, can't test it expect(res.body).toBeDefined() - expect(pg.queryMock).toHaveBeenCalledWith({ - bindings: ["John%", 5000], - sql: `select "name", "age" from "users" where "name" like $1 limit $2` - }) + expect(pg.queryMock).toHaveBeenCalledWith(`select "name", "age" from "users" where "name" like $1 limit $2`, ["John%", 5000]) }) }) @@ -115,7 +106,7 @@ describe("/datasources", () => { .expect('Content-Type', /json/) .expect(200) - expect(res.body).toEqual([]) + expect(res.body.length).toEqual(1) }) it("should apply authorization to endpoint", async () => { diff --git a/packages/server/src/api/routes/tests/table.spec.js b/packages/server/src/api/routes/tests/table.spec.js index df28eed0c2..794e35e040 100644 --- a/packages/server/src/api/routes/tests/table.spec.js +++ b/packages/server/src/api/routes/tests/table.spec.js @@ -108,7 +108,7 @@ describe("/tables", () => { .expect(200) const fetchedTable = res.body[0] expect(fetchedTable.name).toEqual(testTable.name) - expect(fetchedTable.type).toEqual("table") + expect(fetchedTable.type).toEqual("internal") }) it("should apply authorization to endpoint", async () => { diff --git a/packages/server/src/api/routes/tests/utilities/TestFunctions.js b/packages/server/src/api/routes/tests/utilities/TestFunctions.js index dfd77eec7a..dabb3ab40c 100644 --- a/packages/server/src/api/routes/tests/utilities/TestFunctions.js +++ b/packages/server/src/api/routes/tests/utilities/TestFunctions.js @@ -11,7 +11,7 @@ function Request(appId, params) { exports.getAllTableRows = async config => { const req = new Request(config.appId, { tableId: config.table._id }) - await rowController.fetchTableRows(req) + await rowController.fetch(req) return req.body } diff --git a/packages/server/src/tests/utilities/TestConfiguration.js b/packages/server/src/tests/utilities/TestConfiguration.js index 7e56b66dc9..0f458a31bd 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.js +++ b/packages/server/src/tests/utilities/TestConfiguration.js @@ -215,7 +215,7 @@ class TestConfiguration { if (!tableId && this.table) { tableId = this.table._id } - return this._req(null, { tableId }, controllers.row.fetchTableRows) + return this._req(null, { tableId }, controllers.row.fetch) } async createRole(config = null) {