Some test updates to make the fetchSchema tableFilter prop usable.

This commit is contained in:
mike12345567 2024-06-07 17:13:06 +01:00
parent 9cd7c144f4
commit 5de2dc8382
3 changed files with 55 additions and 57 deletions

View File

@ -341,7 +341,7 @@ describe("/datasources", () => {
) )
const persisted = await config.api.datasource.get(datasourceId) const persisted = await config.api.datasource.get(datasourceId)
await config.api.datasource.fetchSchema(datasourceId) await config.api.datasource.fetchSchema({ datasourceId })
const updated = await config.api.datasource.get(datasourceId) const updated = await config.api.datasource.get(datasourceId)
const expected: Datasource = { const expected: Datasource = {

View File

@ -1097,12 +1097,11 @@ describe("postgres integrations", () => {
it("recognises when a table has no primary key", async () => { it("recognises when a table has no primary key", async () => {
await rawQuery(rawDatasource, `CREATE TABLE "${tableName}" (id SERIAL)`) await rawQuery(rawDatasource, `CREATE TABLE "${tableName}" (id SERIAL)`)
const response = await makeRequest( const response = await config.api.datasource.fetchSchema({
"post", datasourceId: datasource._id!,
`/api/datasources/${datasource._id}/schema` })
)
expect(response.body.errors).toEqual({ expect(response.errors).toEqual({
[tableName]: "Table must have a primary key.", [tableName]: "Table must have a primary key.",
}) })
}) })
@ -1113,12 +1112,11 @@ describe("postgres integrations", () => {
`CREATE TABLE "${tableName}" (_id SERIAL PRIMARY KEY) ` `CREATE TABLE "${tableName}" (_id SERIAL PRIMARY KEY) `
) )
const response = await makeRequest( const response = await config.api.datasource.fetchSchema({
"post", datasourceId: datasource._id!,
`/api/datasources/${datasource._id}/schema` })
)
expect(response.body.errors).toEqual({ expect(response.errors).toEqual({
[tableName]: "Table contains invalid columns.", [tableName]: "Table contains invalid columns.",
}) })
}) })
@ -1143,15 +1141,14 @@ describe("postgres integrations", () => {
` `
) )
const response = await makeRequest( const response = await config.api.datasource.fetchSchema({
"post", datasourceId: datasource._id!,
`/api/datasources/${datasource._id}/schema` })
)
const table = response.body.datasource.entities[tableName] const table = response.datasource.entities?.[tableName]
expect(table).toBeDefined() expect(table).toBeDefined()
expect(table.schema[enumColumnName].type).toEqual(FieldType.OPTIONS) expect(table?.schema[enumColumnName].type).toEqual(FieldType.OPTIONS)
}) })
}) })
@ -1215,20 +1212,16 @@ describe("postgres integrations", () => {
rawDatasource, rawDatasource,
`CREATE TABLE "${schema2}".${repeated_table_name} (id2 SERIAL PRIMARY KEY, val2 TEXT);` `CREATE TABLE "${schema2}".${repeated_table_name} (id2 SERIAL PRIMARY KEY, val2 TEXT);`
) )
const response = await makeRequest(
"post", const response = await config.api.datasource.fetchSchema({
`/api/datasources/${datasource._id}/schema`, datasourceId: datasource._id!,
{ tablesFilter: [repeated_table_name],
tablesFilter: [repeated_table_name], })
}
)
expect(response.status).toBe(200)
expect( expect(
response.body.datasource.entities[repeated_table_name].schema response.datasource.entities?.[repeated_table_name].schema
).toBeDefined() ).toBeDefined()
const schema = const schema = response.datasource.entities?.[repeated_table_name].schema
response.body.datasource.entities[repeated_table_name].schema expect(Object.keys(schema || {}).sort()).toEqual(["id", "val1"])
expect(Object.keys(schema).sort()).toEqual(["id", "val1"])
}) })
}) })
@ -1246,16 +1239,14 @@ describe("postgres integrations", () => {
}) })
it("should handle binary columns", async () => { it("should handle binary columns", async () => {
const response = await makeRequest( const response = await config.api.datasource.fetchSchema({
"post", datasourceId: datasource._id!,
`/api/datasources/${datasource._id}/schema` })
) expect(response.datasource.entities).toBeDefined()
expect(response.body).toBeDefined() const table = response.datasource.entities?.["binarytable"]
expect(response.body.datasource.entities).toBeDefined()
const table = response.body.datasource.entities["binarytable"]
expect(table).toBeDefined() expect(table).toBeDefined()
expect(table.schema.id.externalType).toBe("bytea") expect(table?.schema.id.externalType).toBe("bytea")
const row = await config.api.row.save(table._id, { const row = await config.api.row.save(table?._id!, {
id: "1111", id: "1111",
column1: "hello", column1: "hello",
column2: 222, column2: 222,
@ -1279,33 +1270,31 @@ describe("postgres integrations", () => {
}) })
it("should be able to change the table to allow nullable and refetch this", async () => { it("should be able to change the table to allow nullable and refetch this", async () => {
const response = await makeRequest( const response = await config.api.datasource.fetchSchema({
"post", datasourceId: datasource._id!,
`/api/datasources/${datasource._id}/schema` })
) const entities = response.datasource.entities
const entities = response.body.datasource.entities
expect(entities).toBeDefined() expect(entities).toBeDefined()
const nullableTable = entities["nullabletable"] const nullableTable = entities?.["nullabletable"]
expect(nullableTable).toBeDefined() expect(nullableTable).toBeDefined()
expect(nullableTable.schema["order_number"].constraints.presence).toEqual( expect(
true nullableTable?.schema["order_number"].constraints?.presence
) ).toEqual(true)
await rawQuery( await rawQuery(
rawDatasource, rawDatasource,
`ALTER TABLE nullableTable `ALTER TABLE nullableTable
ALTER COLUMN order_number DROP NOT NULL; ALTER COLUMN order_number DROP NOT NULL;
` `
) )
const responseAfter = await makeRequest( const responseAfter = await config.api.datasource.fetchSchema({
"post", datasourceId: datasource._id!,
`/api/datasources/${datasource._id}/schema` })
) const entitiesAfter = responseAfter.datasource.entities
const entitiesAfter = responseAfter.body.datasource.entities
expect(entitiesAfter).toBeDefined() expect(entitiesAfter).toBeDefined()
const nullableTableAfter = entitiesAfter["nullabletable"] const nullableTableAfter = entitiesAfter?.["nullabletable"]
expect(nullableTableAfter).toBeDefined() expect(nullableTableAfter).toBeDefined()
expect( expect(
nullableTableAfter.schema["order_number"].constraints.presence nullableTableAfter?.schema["order_number"].constraints?.presence
).toEqual(false) ).toEqual(false)
}) })
}) })

View File

@ -71,11 +71,20 @@ export class DatasourceAPI extends TestAPI {
}) })
} }
fetchSchema = async (id: string, expectations?: Expectations) => { fetchSchema = async (
{
datasourceId,
tablesFilter,
}: { datasourceId: string; tablesFilter?: string[] },
expectations?: Expectations
) => {
return await this._post<BuildSchemaFromSourceResponse>( return await this._post<BuildSchemaFromSourceResponse>(
`/api/datasources/${id}/schema`, `/api/datasources/${datasourceId}/schema`,
{ {
expectations, expectations: expectations,
body: {
tablesFilter: tablesFilter,
},
} }
) )
} }