Introduce integration tests for `POST /api/datasources/:datasourceId/schema`

This commit is contained in:
Sam Rose 2023-10-13 15:29:59 +01:00
parent 2f0e88c17c
commit e1af1a5be3
2 changed files with 63 additions and 11 deletions

View File

@ -69,18 +69,20 @@ async function buildFilteredSchema(
filter?: string[] filter?: string[]
): Promise<Schema> { ): Promise<Schema> {
let schema = await buildSchemaHelper(datasource) let schema = await buildSchemaHelper(datasource)
let filteredSchema: Schema = { tables: {}, errors: {} } if (!filter) {
if (filter) { return schema
for (let key in schema.tables) { }
if (filter.some(filter => filter.toLowerCase() === key.toLowerCase())) {
filteredSchema.tables[key] = schema.tables[key]
}
}
for (let key in schema.errors) { let filteredSchema: Schema = { tables: {}, errors: {} }
if (filter.some(filter => filter.toLowerCase() === key.toLowerCase())) { for (let key in schema.tables) {
filteredSchema.errors[key] = schema.errors[key] if (filter.some(filter => filter.toLowerCase() === key.toLowerCase())) {
} filteredSchema.tables[key] = schema.tables[key]
}
}
for (let key in schema.errors) {
if (filter.some(filter => filter.toLowerCase() === key.toLowerCase())) {
filteredSchema.errors[key] = schema.errors[key]
} }
} }
return filteredSchema return filteredSchema

View File

@ -18,6 +18,7 @@ import _ from "lodash"
import { generator } from "@budibase/backend-core/tests" import { generator } from "@budibase/backend-core/tests"
import { utils } from "@budibase/backend-core" import { utils } from "@budibase/backend-core"
import { databaseTestProviders } from "../integrations/tests/utils" import { databaseTestProviders } from "../integrations/tests/utils"
import { Client } from "pg"
const config = setup.getConfig()! const config = setup.getConfig()!
@ -1055,4 +1056,53 @@ describe("postgres integrations", () => {
expect(response.body.tableNames.indexOf(primaryName)).not.toBe(-1) expect(response.body.tableNames.indexOf(primaryName)).not.toBe(-1)
}) })
}) })
describe("POST /api/datasources/:datasourceId/schema", () => {
let client: Client
beforeAll(async () => {
client = new Client(
(await databaseTestProviders.postgres.getDsConfig()).config!
)
await client.connect()
})
afterAll(async () => {
await client.end()
})
it("recognises when a table has no primary key", async () => {
await client.query(`
CREATE TABLE table_without_primary_key (
id SERIAL
)
`)
const response = await makeRequest(
"post",
`/api/datasources/${postgresDatasource._id}/schema`
)
expect(response.body.errors).toMatchObject({
table_without_primary_key: "Table must have a primary key.",
})
})
it("recognises when a table is using a reserved column name", async () => {
await client.query(`
CREATE TABLE table_with_reserved_column_name (
_id SERIAL
)
`)
const response = await makeRequest(
"post",
`/api/datasources/${postgresDatasource._id}/schema`
)
expect(response.body.errors).toMatchObject({
table_without_primary_key: "Table must have a primary key.",
})
})
})
}) })