From 0e5de7f16de29e3f165647186a53b06c803ebef5 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 19 Jun 2024 13:36:34 +0100 Subject: [PATCH] Test updates. --- .../src/api/routes/tests/search.spec.ts | 46 ++++++------------- .../src/sdk/app/rows/search/external.ts | 10 ++-- .../server/src/sdk/app/rows/search/sqs.ts | 3 +- 3 files changed, 24 insertions(+), 35 deletions(-) diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index 534c08c7c9..7b7eb5000f 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -93,20 +93,7 @@ describe.each([ class SearchAssertion { constructor(private readonly query: RowSearchParams) {} - private async performSearch(): Promise { - if (isInMemory) { - return dataFilters.search(_.cloneDeep(rows), this.query) - } else { - return ( - await config.api.row.search(table._id!, { - ...this.query, - tableId: table._id!, - }) - ).rows - } - } - - private async performSearchFullResponse(): Promise> { + private async performSearch(): Promise> { if (isInMemory) { return { rows: dataFilters.search(_.cloneDeep(rows), this.query) } } else { @@ -187,7 +174,7 @@ describe.each([ // different to the one passed in will cause the assertion to fail. Extra // rows returned by the query will also cause the assertion to fail. async toMatchExactly(expectedRows: any[]) { - const foundRows = await this.performSearch() + const { rows: foundRows } = await this.performSearch() // eslint-disable-next-line jest/no-standalone-expect expect(foundRows).toHaveLength(expectedRows.length) @@ -203,7 +190,7 @@ describe.each([ // passed in. The order of the rows is not important, but extra rows will // cause the assertion to fail. async toContainExactly(expectedRows: any[]) { - const foundRows = await this.performSearch() + const { rows: foundRows } = await this.performSearch() // eslint-disable-next-line jest/no-standalone-expect expect(foundRows).toHaveLength(expectedRows.length) @@ -219,26 +206,23 @@ describe.each([ // Asserts that the query returns some property values - this cannot be used // to check row values, however this shouldn't be important for checking properties - async toHaveProperty( - properties: { - key: keyof SearchResponse - value?: any - }[] - ) { - const response = await this.performSearchFullResponse() - for (let property of properties) { + // typing for this has to be any, Jest doesn't expose types for matchers like expect.any(...) + async toMatch(properties: Record) { + const response = await this.performSearch() + const keys = Object.keys(properties) as Array> + for (let key of keys) { // eslint-disable-next-line jest/no-standalone-expect - expect(response[property.key]).toBeDefined() - if (property.value) { + expect(response[key]).toBeDefined() + if (properties[key]) { // eslint-disable-next-line jest/no-standalone-expect - expect(response[property.key]).toEqual(property.value) + expect(response[key]).toEqual(properties[key]) } } } // Asserts that the query doesn't return a property, e.g. pagination parameters. async toNotHaveProperty(properties: (keyof SearchResponse)[]) { - const response = await this.performSearchFullResponse() + const response = await this.performSearch() for (let property of properties) { // eslint-disable-next-line jest/no-standalone-expect expect(response[property]).toBeUndefined() @@ -249,7 +233,7 @@ describe.each([ // The order of the rows is not important. Extra rows will not cause the // assertion to fail. async toContain(expectedRows: any[]) { - const foundRows = await this.performSearch() + const { rows: foundRows } = await this.performSearch() // eslint-disable-next-line jest/no-standalone-expect expect([...foundRows]).toEqual( @@ -266,7 +250,7 @@ describe.each([ } async toHaveLength(length: number) { - const foundRows = await this.performSearch() + const { rows: foundRows } = await this.performSearch() // eslint-disable-next-line jest/no-standalone-expect expect(foundRows).toHaveLength(length) @@ -1860,7 +1844,7 @@ describe.each([ name: true, }, }, - }).toHaveProperty([{ key: "totalRows", value: 2 }, { key: "rows" }])) + }).toMatch({ totalRows: 2, rows: expect.any(Array) })) it("shouldn't count rows when option is not set", () => { expectSearch({ diff --git a/packages/server/src/sdk/app/rows/search/external.ts b/packages/server/src/sdk/app/rows/search/external.ts index 5e2074f56d..6c2e16dcc4 100644 --- a/packages/server/src/sdk/app/rows/search/external.ts +++ b/packages/server/src/sdk/app/rows/search/external.ts @@ -81,11 +81,15 @@ export async function search( paginate: paginateObj as PaginationJson, includeSqlRelationships: IncludeRelationship.INCLUDE, } - let rows = await handleRequest(Operation.READ, tableId, parameters) - let totalRows: number | undefined + const requests: Promise[] = [] + requests.push(handleRequest(Operation.READ, tableId, parameters)) if (countRows) { - totalRows = await handleRequest(Operation.COUNT, tableId, parameters) + requests.push(handleRequest(Operation.COUNT, tableId, parameters)) } + const responses = await Promise.all(requests) + let rows = responses[0] as Row[] + const totalRows = responses[1] ? (responses[1] as number) : undefined + let hasNextPage = false // remove the extra row if it's there if (paginate && limit && rows.length > limit) { diff --git a/packages/server/src/sdk/app/rows/search/sqs.ts b/packages/server/src/sdk/app/rows/search/sqs.ts index 525d414887..a93ae174b0 100644 --- a/packages/server/src/sdk/app/rows/search/sqs.ts +++ b/packages/server/src/sdk/app/rows/search/sqs.ts @@ -12,6 +12,7 @@ import { SortType, SqlClient, Table, + Datasource, } from "@budibase/types" import { buildInternalRelationships, @@ -117,7 +118,7 @@ async function runSqlQuery( if (opts?.countTotalRows) { json.endpoint.operation = Operation.COUNT } - const processSQLQuery = async (json: QueryJson) => { + const processSQLQuery = async (_: Datasource, json: QueryJson) => { const query = builder._query(json, { disableReturning: true, })