Fix tests

This commit is contained in:
adrinr 2023-02-06 17:30:33 +00:00
parent 3d9a208bfb
commit 29503d1244
2 changed files with 28 additions and 23 deletions

View File

@ -18,7 +18,6 @@ import { utils } from "@budibase/backend-core"
const config = setup.getConfig()! const config = setup.getConfig()!
jest.unmock("node-fetch")
jest.unmock("pg") jest.unmock("pg")
describe("row api - postgres", () => { describe("row api - postgres", () => {
@ -127,13 +126,11 @@ describe("row api - postgres", () => {
await config.end() await config.end()
}) })
const randomInteger = () => generator.integer({ min: 0, max: 10000 })
function makeRandomRow() { function makeRandomRow() {
return { return {
name: generator.name(), name: generator.name(),
description: generator.paragraph(), description: generator.paragraph(),
value: randomInteger(), value: generator.age(),
} }
} }
@ -245,8 +242,6 @@ describe("row api - postgres", () => {
...newRow, ...newRow,
} }
// TODO: check why this is being returned from the creation
delete expected.linkedField
expect(persistedRows).toEqual([expect.objectContaining(expected)]) expect(persistedRows).toEqual([expect.objectContaining(expected)])
}) })
@ -275,16 +270,13 @@ describe("row api - postgres", () => {
let { row } = _.sample(await populateRows(10))! let { row } = _.sample(await populateRows(10))!
const newName = generator.name() const newName = generator.name()
const newValue = randomInteger() const newValue = generator.age()
const updatedRow = { const updatedRow = {
...row, ...row,
name: newName, name: newName,
value: newValue, value: newValue,
} }
// TODO: check why this is being returned from the creation
delete (updatedRow as any).linkedField
const res = await updateRow(postgresTable._id, updatedRow) const res = await updateRow(postgresTable._id, updatedRow)
expect(res.status).toBe(200) expect(res.status).toBe(200)
@ -383,11 +375,8 @@ describe("row api - postgres", () => {
expect(res.status).toBe(200) 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({ expect(res.body).toEqual({
...expected, ...row,
_id: expect.any(String), _id: expect.any(String),
_rev: expect.any(String), _rev: expect.any(String),
}) })
@ -450,12 +439,12 @@ describe("row api - postgres", () => {
...Array(2).fill({ ...Array(2).fill({
name, name,
description: generator.paragraph(), description: generator.paragraph(),
value: randomInteger(), value: generator.age(),
}), }),
...Array(2).fill({ ...Array(2).fill({
name: `${name}${utils.newid()}`, name: `${name}${utils.newid()}`,
description: generator.paragraph(), description: generator.paragraph(),
value: randomInteger(), value: generator.age(),
}), }),
] ]

View File

@ -89,13 +89,22 @@ function parseFilters(filters: SearchFilters | undefined): SearchFilters {
function generateSelectStatement( function generateSelectStatement(
json: QueryJson, json: QueryJson,
knex: Knex knex: Knex,
excludeJoinColumns = false
): (string | Knex.Raw)[] { ): (string | Knex.Raw)[] {
const { resource, meta } = json const { resource, meta } = json
const schema = meta?.table?.schema 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 fieldNames = field.split(/\./g)
const tableName = fieldNames[0] const tableName = fieldNames[0]
if (
meta?.table?.name &&
excludeJoinColumns &&
tableName !== meta.table.name
) {
return p
}
const columnName = fieldNames[1] const columnName = fieldNames[1]
if ( if (
columnName && columnName &&
@ -104,13 +113,18 @@ function generateSelectStatement(
) { ) {
const externalType = schema[columnName].externalType const externalType = schema[columnName].externalType
if (externalType?.includes("money")) { if (externalType?.includes("money")) {
return knex.raw( p.push(
knex.raw(
`"${tableName}"."${columnName}"::money::numeric as "${field}"` `"${tableName}"."${columnName}"::money::numeric as "${field}"`
) )
)
return p
} }
} }
return `${field} as ${field}`
}) p.push(`${field} as ${field}`)
return p
}, [])
} }
class InternalBuilder { class InternalBuilder {
@ -396,7 +410,9 @@ class InternalBuilder {
if (opts.disableReturning) { if (opts.disableReturning) {
return query.insert(parsedBody) return query.insert(parsedBody)
} else { } else {
return query.insert(parsedBody).returning("*") return query
.insert(parsedBody)
.returning(generateSelectStatement(json, knex, true))
} }
} }