Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename search.spec.ts helpers to better reflect their purpose. #13484

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
114 changes: 77 additions & 37 deletions packages/server/src/api/routes/tests/search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ describe.each([
class SearchAssertion {
constructor(private readonly query: RowSearchParams) {}

async toMatch(expectedRows: any[]) {
// Asserts that the query returns rows matching exactly the set of rows
// passed in. The order of the rows matters. Rows returned in an order
// 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 { rows: foundRows } = await config.api.row.search(table._id!, {
...this.query,
tableId: table._id!,
Expand All @@ -82,7 +86,10 @@ describe.each([
)
}

async toContain(expectedRows: any[]) {
// Asserts that the query returns rows matching exactly the set of rows
// passed in. The order of the rows is not important, but extra rows will
// cause the assertion to fail.
async toContainExactly(expectedRows: any[]) {
const { rows: foundRows } = await config.api.row.search(table._id!, {
...this.query,
tableId: table._id!,
Expand All @@ -102,8 +109,29 @@ describe.each([
)
}

// Asserts that the query returns rows matching the set of rows passed in.
// The order of the rows is not important. Extra rows will not cause the
// assertion to fail.
async toContain(expectedRows: any[]) {
const { rows: foundRows } = await config.api.row.search(table._id!, {
...this.query,
tableId: table._id!,
})

// eslint-disable-next-line jest/no-standalone-expect
expect(foundRows).toEqual(
expect.arrayContaining(
expectedRows.map((expectedRow: any) =>
expect.objectContaining(
foundRows.find(foundRow => _.isMatch(foundRow, expectedRow))
)
)
)
)
}

async toFindNothing() {
await this.toContain([])
await this.toContainExactly([])
}

async toHaveLength(length: number) {
Expand Down Expand Up @@ -135,18 +163,18 @@ describe.each([

describe("misc", () => {
it("should return all if no query is passed", () =>
expectSearch({} as RowSearchParams).toContain([
expectSearch({} as RowSearchParams).toContainExactly([
{ name: "foo" },
{ name: "bar" },
]))

it("should return all if empty query is passed", () =>
expectQuery({}).toContain([{ name: "foo" }, { name: "bar" }]))
expectQuery({}).toContainExactly([{ name: "foo" }, { name: "bar" }]))

it("should return all if onEmptyFilter is RETURN_ALL", () =>
expectQuery({
onEmptyFilter: EmptyFilterOption.RETURN_ALL,
}).toContain([{ name: "foo" }, { name: "bar" }]))
}).toContainExactly([{ name: "foo" }, { name: "bar" }]))

it("should return nothing if onEmptyFilter is RETURN_NONE", () =>
expectQuery({
Expand All @@ -159,31 +187,41 @@ describe.each([

describe("equal", () => {
it("successfully finds a row", () =>
expectQuery({ equal: { name: "foo" } }).toContain([{ name: "foo" }]))
expectQuery({ equal: { name: "foo" } }).toContainExactly([
{ name: "foo" },
]))

it("fails to find nonexistent row", () =>
expectQuery({ equal: { name: "none" } }).toFindNothing())
})

describe("notEqual", () => {
it("successfully finds a row", () =>
expectQuery({ notEqual: { name: "foo" } }).toContain([{ name: "bar" }]))
expectQuery({ notEqual: { name: "foo" } }).toContainExactly([
{ name: "bar" },
]))

it("fails to find nonexistent row", () =>
expectQuery({ notEqual: { name: "bar" } }).toContain([{ name: "foo" }]))
expectQuery({ notEqual: { name: "bar" } }).toContainExactly([
{ name: "foo" },
]))
})

describe("oneOf", () => {
it("successfully finds a row", () =>
expectQuery({ oneOf: { name: ["foo"] } }).toContain([{ name: "foo" }]))
expectQuery({ oneOf: { name: ["foo"] } }).toContainExactly([
{ name: "foo" },
]))

it("fails to find nonexistent row", () =>
expectQuery({ oneOf: { name: ["none"] } }).toFindNothing())
})

describe("fuzzy", () => {
it("successfully finds a row", () =>
expectQuery({ fuzzy: { name: "oo" } }).toContain([{ name: "foo" }]))
expectQuery({ fuzzy: { name: "oo" } }).toContainExactly([
{ name: "foo" },
]))

it("fails to find nonexistent row", () =>
expectQuery({ fuzzy: { name: "none" } }).toFindNothing())
Expand All @@ -195,14 +233,14 @@ describe.each([
query: {},
sort: "name",
sortOrder: SortOrder.ASCENDING,
}).toMatch([{ name: "bar" }, { name: "foo" }]))
}).toMatchExactly([{ name: "bar" }, { name: "foo" }]))

it("sorts descending", () =>
expectSearch({
query: {},
sort: "name",
sortOrder: SortOrder.DESCENDING,
}).toMatch([{ name: "foo" }, { name: "bar" }]))
}).toMatchExactly([{ name: "foo" }, { name: "bar" }]))

describe("sortType STRING", () => {
it("sorts ascending", () =>
Expand All @@ -211,15 +249,15 @@ describe.each([
sort: "name",
sortType: SortType.STRING,
sortOrder: SortOrder.ASCENDING,
}).toMatch([{ name: "bar" }, { name: "foo" }]))
}).toMatchExactly([{ name: "bar" }, { name: "foo" }]))

it("sorts descending", () =>
expectSearch({
query: {},
sort: "name",
sortType: SortType.STRING,
sortOrder: SortOrder.DESCENDING,
}).toMatch([{ name: "foo" }, { name: "bar" }]))
}).toMatchExactly([{ name: "foo" }, { name: "bar" }]))
})
})
})
Expand All @@ -234,23 +272,23 @@ describe.each([

describe("equal", () => {
it("successfully finds a row", () =>
expectQuery({ equal: { age: 1 } }).toContain([{ age: 1 }]))
expectQuery({ equal: { age: 1 } }).toContainExactly([{ age: 1 }]))

it("fails to find nonexistent row", () =>
expectQuery({ equal: { age: 2 } }).toFindNothing())
})

describe("notEqual", () => {
it("successfully finds a row", () =>
expectQuery({ notEqual: { age: 1 } }).toContain([{ age: 10 }]))
expectQuery({ notEqual: { age: 1 } }).toContainExactly([{ age: 10 }]))

it("fails to find nonexistent row", () =>
expectQuery({ notEqual: { age: 10 } }).toContain([{ age: 1 }]))
expectQuery({ notEqual: { age: 10 } }).toContainExactly([{ age: 1 }]))
})

describe("oneOf", () => {
it("successfully finds a row", () =>
expectQuery({ oneOf: { age: [1] } }).toContain([{ age: 1 }]))
expectQuery({ oneOf: { age: [1] } }).toContainExactly([{ age: 1 }]))

it("fails to find nonexistent row", () =>
expectQuery({ oneOf: { age: [2] } }).toFindNothing())
Expand All @@ -260,17 +298,17 @@ describe.each([
it("successfully finds a row", () =>
expectQuery({
range: { age: { low: 1, high: 5 } },
}).toContain([{ age: 1 }]))
}).toContainExactly([{ age: 1 }]))

it("successfully finds multiple rows", () =>
expectQuery({
range: { age: { low: 1, high: 10 } },
}).toContain([{ age: 1 }, { age: 10 }]))
}).toContainExactly([{ age: 1 }, { age: 10 }]))

it("successfully finds a row with a high bound", () =>
expectQuery({
range: { age: { low: 5, high: 10 } },
}).toContain([{ age: 10 }]))
}).toContainExactly([{ age: 10 }]))
})

describe("sort", () => {
Expand All @@ -279,14 +317,14 @@ describe.each([
query: {},
sort: "age",
sortOrder: SortOrder.ASCENDING,
}).toMatch([{ age: 1 }, { age: 10 }]))
}).toMatchExactly([{ age: 1 }, { age: 10 }]))

it("sorts descending", () =>
expectSearch({
query: {},
sort: "age",
sortOrder: SortOrder.DESCENDING,
}).toMatch([{ age: 10 }, { age: 1 }]))
}).toMatchExactly([{ age: 10 }, { age: 1 }]))
})

describe("sortType NUMBER", () => {
Expand All @@ -296,15 +334,15 @@ describe.each([
sort: "age",
sortType: SortType.NUMBER,
sortOrder: SortOrder.ASCENDING,
}).toMatch([{ age: 1 }, { age: 10 }]))
}).toMatchExactly([{ age: 1 }, { age: 10 }]))

it("sorts descending", () =>
expectSearch({
query: {},
sort: "age",
sortType: SortType.NUMBER,
sortOrder: SortOrder.DESCENDING,
}).toMatch([{ age: 10 }, { age: 1 }]))
}).toMatchExactly([{ age: 10 }, { age: 1 }]))
})
})

Expand All @@ -324,27 +362,29 @@ describe.each([

describe("equal", () => {
it("successfully finds a row", () =>
expectQuery({ equal: { dob: JAN_1ST } }).toContain([{ dob: JAN_1ST }]))
expectQuery({ equal: { dob: JAN_1ST } }).toContainExactly([
{ dob: JAN_1ST },
]))

it("fails to find nonexistent row", () =>
expectQuery({ equal: { dob: JAN_2ND } }).toFindNothing())
})

describe("notEqual", () => {
it("successfully finds a row", () =>
expectQuery({ notEqual: { dob: JAN_1ST } }).toContain([
expectQuery({ notEqual: { dob: JAN_1ST } }).toContainExactly([
{ dob: JAN_10TH },
]))

it("fails to find nonexistent row", () =>
expectQuery({ notEqual: { dob: JAN_10TH } }).toContain([
expectQuery({ notEqual: { dob: JAN_10TH } }).toContainExactly([
{ dob: JAN_1ST },
]))
})

describe("oneOf", () => {
it("successfully finds a row", () =>
expectQuery({ oneOf: { dob: [JAN_1ST] } }).toContain([
expectQuery({ oneOf: { dob: [JAN_1ST] } }).toContainExactly([
{ dob: JAN_1ST },
]))

Expand All @@ -356,17 +396,17 @@ describe.each([
it("successfully finds a row", () =>
expectQuery({
range: { dob: { low: JAN_1ST, high: JAN_5TH } },
}).toContain([{ dob: JAN_1ST }]))
}).toContainExactly([{ dob: JAN_1ST }]))

it("successfully finds multiple rows", () =>
expectQuery({
range: { dob: { low: JAN_1ST, high: JAN_10TH } },
}).toContain([{ dob: JAN_1ST }, { dob: JAN_10TH }]))
}).toContainExactly([{ dob: JAN_1ST }, { dob: JAN_10TH }]))

it("successfully finds a row with a high bound", () =>
expectQuery({
range: { dob: { low: JAN_5TH, high: JAN_10TH } },
}).toContain([{ dob: JAN_10TH }]))
}).toContainExactly([{ dob: JAN_10TH }]))
})

describe("sort", () => {
Expand All @@ -375,14 +415,14 @@ describe.each([
query: {},
sort: "dob",
sortOrder: SortOrder.ASCENDING,
}).toMatch([{ dob: JAN_1ST }, { dob: JAN_10TH }]))
}).toMatchExactly([{ dob: JAN_1ST }, { dob: JAN_10TH }]))

it("sorts descending", () =>
expectSearch({
query: {},
sort: "dob",
sortOrder: SortOrder.DESCENDING,
}).toMatch([{ dob: JAN_10TH }, { dob: JAN_1ST }]))
}).toMatchExactly([{ dob: JAN_10TH }, { dob: JAN_1ST }]))

describe("sortType STRING", () => {
it("sorts ascending", () =>
Expand All @@ -391,15 +431,15 @@ describe.each([
sort: "dob",
sortType: SortType.STRING,
sortOrder: SortOrder.ASCENDING,
}).toMatch([{ dob: JAN_1ST }, { dob: JAN_10TH }]))
}).toMatchExactly([{ dob: JAN_1ST }, { dob: JAN_10TH }]))

it("sorts descending", () =>
expectSearch({
query: {},
sort: "dob",
sortType: SortType.STRING,
sortOrder: SortOrder.DESCENDING,
}).toMatch([{ dob: JAN_10TH }, { dob: JAN_1ST }]))
}).toMatchExactly([{ dob: JAN_10TH }, { dob: JAN_1ST }]))
})
})
})
Expand Down