Test updates.
This commit is contained in:
parent
abfab054d7
commit
0e5de7f16d
|
@ -93,20 +93,7 @@ describe.each([
|
||||||
class SearchAssertion {
|
class SearchAssertion {
|
||||||
constructor(private readonly query: RowSearchParams) {}
|
constructor(private readonly query: RowSearchParams) {}
|
||||||
|
|
||||||
private async performSearch(): Promise<Row[]> {
|
private async performSearch(): Promise<SearchResponse<Row>> {
|
||||||
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<SearchResponse<Row>> {
|
|
||||||
if (isInMemory) {
|
if (isInMemory) {
|
||||||
return { rows: dataFilters.search(_.cloneDeep(rows), this.query) }
|
return { rows: dataFilters.search(_.cloneDeep(rows), this.query) }
|
||||||
} else {
|
} else {
|
||||||
|
@ -187,7 +174,7 @@ describe.each([
|
||||||
// different to the one passed in will cause the assertion to fail. Extra
|
// 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.
|
// rows returned by the query will also cause the assertion to fail.
|
||||||
async toMatchExactly(expectedRows: any[]) {
|
async toMatchExactly(expectedRows: any[]) {
|
||||||
const foundRows = await this.performSearch()
|
const { rows: foundRows } = await this.performSearch()
|
||||||
|
|
||||||
// eslint-disable-next-line jest/no-standalone-expect
|
// eslint-disable-next-line jest/no-standalone-expect
|
||||||
expect(foundRows).toHaveLength(expectedRows.length)
|
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
|
// passed in. The order of the rows is not important, but extra rows will
|
||||||
// cause the assertion to fail.
|
// cause the assertion to fail.
|
||||||
async toContainExactly(expectedRows: any[]) {
|
async toContainExactly(expectedRows: any[]) {
|
||||||
const foundRows = await this.performSearch()
|
const { rows: foundRows } = await this.performSearch()
|
||||||
|
|
||||||
// eslint-disable-next-line jest/no-standalone-expect
|
// eslint-disable-next-line jest/no-standalone-expect
|
||||||
expect(foundRows).toHaveLength(expectedRows.length)
|
expect(foundRows).toHaveLength(expectedRows.length)
|
||||||
|
@ -219,26 +206,23 @@ describe.each([
|
||||||
|
|
||||||
// Asserts that the query returns some property values - this cannot be used
|
// 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
|
// to check row values, however this shouldn't be important for checking properties
|
||||||
async toHaveProperty(
|
// typing for this has to be any, Jest doesn't expose types for matchers like expect.any(...)
|
||||||
properties: {
|
async toMatch(properties: Record<string, any>) {
|
||||||
key: keyof SearchResponse<Row>
|
const response = await this.performSearch()
|
||||||
value?: any
|
const keys = Object.keys(properties) as Array<keyof SearchResponse<Row>>
|
||||||
}[]
|
for (let key of keys) {
|
||||||
) {
|
|
||||||
const response = await this.performSearchFullResponse()
|
|
||||||
for (let property of properties) {
|
|
||||||
// eslint-disable-next-line jest/no-standalone-expect
|
// eslint-disable-next-line jest/no-standalone-expect
|
||||||
expect(response[property.key]).toBeDefined()
|
expect(response[key]).toBeDefined()
|
||||||
if (property.value) {
|
if (properties[key]) {
|
||||||
// eslint-disable-next-line jest/no-standalone-expect
|
// 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.
|
// Asserts that the query doesn't return a property, e.g. pagination parameters.
|
||||||
async toNotHaveProperty(properties: (keyof SearchResponse<Row>)[]) {
|
async toNotHaveProperty(properties: (keyof SearchResponse<Row>)[]) {
|
||||||
const response = await this.performSearchFullResponse()
|
const response = await this.performSearch()
|
||||||
for (let property of properties) {
|
for (let property of properties) {
|
||||||
// eslint-disable-next-line jest/no-standalone-expect
|
// eslint-disable-next-line jest/no-standalone-expect
|
||||||
expect(response[property]).toBeUndefined()
|
expect(response[property]).toBeUndefined()
|
||||||
|
@ -249,7 +233,7 @@ describe.each([
|
||||||
// The order of the rows is not important. Extra rows will not cause the
|
// The order of the rows is not important. Extra rows will not cause the
|
||||||
// assertion to fail.
|
// assertion to fail.
|
||||||
async toContain(expectedRows: any[]) {
|
async toContain(expectedRows: any[]) {
|
||||||
const foundRows = await this.performSearch()
|
const { rows: foundRows } = await this.performSearch()
|
||||||
|
|
||||||
// eslint-disable-next-line jest/no-standalone-expect
|
// eslint-disable-next-line jest/no-standalone-expect
|
||||||
expect([...foundRows]).toEqual(
|
expect([...foundRows]).toEqual(
|
||||||
|
@ -266,7 +250,7 @@ describe.each([
|
||||||
}
|
}
|
||||||
|
|
||||||
async toHaveLength(length: number) {
|
async toHaveLength(length: number) {
|
||||||
const foundRows = await this.performSearch()
|
const { rows: foundRows } = await this.performSearch()
|
||||||
|
|
||||||
// eslint-disable-next-line jest/no-standalone-expect
|
// eslint-disable-next-line jest/no-standalone-expect
|
||||||
expect(foundRows).toHaveLength(length)
|
expect(foundRows).toHaveLength(length)
|
||||||
|
@ -1860,7 +1844,7 @@ describe.each([
|
||||||
name: true,
|
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", () => {
|
it("shouldn't count rows when option is not set", () => {
|
||||||
expectSearch({
|
expectSearch({
|
||||||
|
|
|
@ -81,11 +81,15 @@ export async function search(
|
||||||
paginate: paginateObj as PaginationJson,
|
paginate: paginateObj as PaginationJson,
|
||||||
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
includeSqlRelationships: IncludeRelationship.INCLUDE,
|
||||||
}
|
}
|
||||||
let rows = await handleRequest(Operation.READ, tableId, parameters)
|
const requests: Promise<Row[] | number>[] = []
|
||||||
let totalRows: number | undefined
|
requests.push(handleRequest(Operation.READ, tableId, parameters))
|
||||||
if (countRows) {
|
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
|
let hasNextPage = false
|
||||||
// remove the extra row if it's there
|
// remove the extra row if it's there
|
||||||
if (paginate && limit && rows.length > limit) {
|
if (paginate && limit && rows.length > limit) {
|
||||||
|
|
|
@ -12,6 +12,7 @@ import {
|
||||||
SortType,
|
SortType,
|
||||||
SqlClient,
|
SqlClient,
|
||||||
Table,
|
Table,
|
||||||
|
Datasource,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import {
|
import {
|
||||||
buildInternalRelationships,
|
buildInternalRelationships,
|
||||||
|
@ -117,7 +118,7 @@ async function runSqlQuery(
|
||||||
if (opts?.countTotalRows) {
|
if (opts?.countTotalRows) {
|
||||||
json.endpoint.operation = Operation.COUNT
|
json.endpoint.operation = Operation.COUNT
|
||||||
}
|
}
|
||||||
const processSQLQuery = async (json: QueryJson) => {
|
const processSQLQuery = async (_: Datasource, json: QueryJson) => {
|
||||||
const query = builder._query(json, {
|
const query = builder._query(json, {
|
||||||
disableReturning: true,
|
disableReturning: true,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue