From 29503d1244bece876b6bf2ca2e4b34a82dac2382 Mon Sep 17 00:00:00 2001 From: adrinr Date: Mon, 6 Feb 2023 17:30:33 +0000 Subject: [PATCH] Fix tests --- .../src/integration-test/postgres.spec.ts | 21 ++++--------- packages/server/src/integrations/base/sql.ts | 30 ++++++++++++++----- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/packages/server/src/integration-test/postgres.spec.ts b/packages/server/src/integration-test/postgres.spec.ts index 4c69d790e3..24671498b9 100644 --- a/packages/server/src/integration-test/postgres.spec.ts +++ b/packages/server/src/integration-test/postgres.spec.ts @@ -18,7 +18,6 @@ import { utils } from "@budibase/backend-core" const config = setup.getConfig()! -jest.unmock("node-fetch") jest.unmock("pg") describe("row api - postgres", () => { @@ -127,13 +126,11 @@ describe("row api - postgres", () => { await config.end() }) - const randomInteger = () => generator.integer({ min: 0, max: 10000 }) - function makeRandomRow() { return { name: generator.name(), description: generator.paragraph(), - value: randomInteger(), + value: generator.age(), } } @@ -245,8 +242,6 @@ describe("row api - postgres", () => { ...newRow, } - // TODO: check why this is being returned from the creation - delete expected.linkedField expect(persistedRows).toEqual([expect.objectContaining(expected)]) }) @@ -275,16 +270,13 @@ describe("row api - postgres", () => { let { row } = _.sample(await populateRows(10))! const newName = generator.name() - const newValue = randomInteger() + const newValue = generator.age() const updatedRow = { ...row, name: newName, value: newValue, } - // TODO: check why this is being returned from the creation - delete (updatedRow as any).linkedField - const res = await updateRow(postgresTable._id, updatedRow) expect(res.status).toBe(200) @@ -383,11 +375,8 @@ describe("row api - postgres", () => { expect(res.status).toBe(200) - const expected = { ...row } - // TODO: check why this is being returned from the creation - delete expected.linkedField expect(res.body).toEqual({ - ...expected, + ...row, _id: expect.any(String), _rev: expect.any(String), }) @@ -450,12 +439,12 @@ describe("row api - postgres", () => { ...Array(2).fill({ name, description: generator.paragraph(), - value: randomInteger(), + value: generator.age(), }), ...Array(2).fill({ name: `${name}${utils.newid()}`, description: generator.paragraph(), - value: randomInteger(), + value: generator.age(), }), ] diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index 1d3c57414c..fe96248ce5 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -89,13 +89,22 @@ function parseFilters(filters: SearchFilters | undefined): SearchFilters { function generateSelectStatement( json: QueryJson, - knex: Knex + knex: Knex, + excludeJoinColumns = false ): (string | Knex.Raw)[] { const { resource, meta } = json const schema = meta?.table?.schema - return resource!.fields.map(field => { + + return resource!.fields.reduce<(string | Knex.Raw)[]>((p, field) => { const fieldNames = field.split(/\./g) const tableName = fieldNames[0] + if ( + meta?.table?.name && + excludeJoinColumns && + tableName !== meta.table.name + ) { + return p + } const columnName = fieldNames[1] if ( columnName && @@ -104,13 +113,18 @@ function generateSelectStatement( ) { const externalType = schema[columnName].externalType if (externalType?.includes("money")) { - return knex.raw( - `"${tableName}"."${columnName}"::money::numeric as "${field}"` + p.push( + knex.raw( + `"${tableName}"."${columnName}"::money::numeric as "${field}"` + ) ) + return p } } - return `${field} as ${field}` - }) + + p.push(`${field} as ${field}`) + return p + }, []) } class InternalBuilder { @@ -396,7 +410,9 @@ class InternalBuilder { if (opts.disableReturning) { return query.insert(parsedBody) } else { - return query.insert(parsedBody).returning("*") + return query + .insert(parsedBody) + .returning(generateSelectStatement(json, knex, true)) } }