From 86d9de5a2dd5511313005038f82b7bfde4b70eac Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 19 Jun 2024 18:57:37 +0100 Subject: [PATCH] Fixing view test cases, adding SQS to it and correcting the default sort order. --- .../src/api/routes/tests/viewV2.spec.ts | 36 ++++++++++++++----- .../server/src/sdk/app/rows/search/sqs.ts | 7 ++-- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 06921037dd..99ff4f8db7 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -7,6 +7,7 @@ import { INTERNAL_TABLE_SOURCE_ID, PermissionLevel, QuotaUsageType, + Row, SaveTableRequest, SearchFilterOperator, SortOrder, @@ -17,6 +18,7 @@ import { UpdateViewRequest, ViewUIFieldMetadata, ViewV2, + SearchResponse, } from "@budibase/types" import { generator, mocks } from "@budibase/backend-core/tests" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" @@ -25,17 +27,21 @@ import { quotas } from "@budibase/pro" import { db, roles } from "@budibase/backend-core" describe.each([ - ["internal", undefined], + ["lucene", undefined], + ["sqs", undefined], [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], -])("/v2/views (%s)", (_, dsProvider) => { +])("/v2/views (%s)", (name, dsProvider) => { const config = setup.getConfig() - const isInternal = !dsProvider + const isSqs = name === "sqs" + const isLucene = name === "lucene" + const isInternal = isSqs || isLucene let table: Table let datasource: Datasource + let envCleanup: (() => void) | undefined function saveTableRequest( ...overrides: Partial>[] @@ -82,6 +88,9 @@ describe.each([ } beforeAll(async () => { + if (isSqs) { + envCleanup = config.setEnv({ SQS_SEARCH_ENABLE: "true" }) + } await config.init() if (dsProvider) { @@ -94,6 +103,9 @@ describe.each([ afterAll(async () => { setup.afterAll() + if (envCleanup) { + envCleanup() + } }) beforeEach(() => { @@ -1252,12 +1264,13 @@ describe.each([ paginate: true, limit: 4, query: {}, + countRows: true, }) expect(page1).toEqual({ rows: expect.arrayContaining(rows.slice(0, 4)), - totalRows: isInternal ? 10 : undefined, hasNextPage: true, bookmark: expect.anything(), + totalRows: 10, }) const page2 = await config.api.viewV2.search(view.id, { @@ -1265,12 +1278,13 @@ describe.each([ limit: 4, bookmark: page1.bookmark, query: {}, + countRows: true, }) expect(page2).toEqual({ rows: expect.arrayContaining(rows.slice(4, 8)), - totalRows: isInternal ? 10 : undefined, hasNextPage: true, bookmark: expect.anything(), + totalRows: 10, }) const page3 = await config.api.viewV2.search(view.id, { @@ -1278,13 +1292,17 @@ describe.each([ limit: 4, bookmark: page2.bookmark, query: {}, + countRows: true, }) - expect(page3).toEqual({ + const expectation: SearchResponse = { rows: expect.arrayContaining(rows.slice(8)), - totalRows: isInternal ? 10 : undefined, hasNextPage: false, - bookmark: expect.anything(), - }) + totalRows: 10, + } + if (isLucene) { + expectation.bookmark = expect.anything() + } + expect(page3).toEqual(expectation) }) const sortTestOptions: [ diff --git a/packages/server/src/sdk/app/rows/search/sqs.ts b/packages/server/src/sdk/app/rows/search/sqs.ts index 0ba2adb570..bb1d62affc 100644 --- a/packages/server/src/sdk/app/rows/search/sqs.ts +++ b/packages/server/src/sdk/app/rows/search/sqs.ts @@ -154,7 +154,7 @@ export async function search( options: RowSearchParams, table: Table ): Promise> { - const { paginate, query, ...params } = options + let { paginate, query, ...params } = options const allTables = await sdk.tables.getAllInternalTables() const allTablesMap = buildTableMap(allTables) @@ -196,7 +196,7 @@ export async function search( sortField.type === FieldType.NUMBER ? SortType.NUMBER : SortType.STRING request.sort = { [sortField.name]: { - direction: params.sortOrder || SortOrder.DESCENDING, + direction: params.sortOrder || SortOrder.ASCENDING, type: sortType as SortType, }, } @@ -207,7 +207,8 @@ export async function search( } const bookmark: number = (params.bookmark as number) || 0 - if (paginate && params.limit) { + if (params.limit) { + paginate = true request.paginate = { limit: params.limit + 1, offset: bookmark * params.limit,