diff --git a/packages/server/src/automations/tests/queryRows.spec.ts b/packages/server/src/automations/tests/queryRows.spec.ts index 12611d3f90..18d2e2d6cd 100644 --- a/packages/server/src/automations/tests/queryRows.spec.ts +++ b/packages/server/src/automations/tests/queryRows.spec.ts @@ -1,5 +1,7 @@ -import { Table } from "@budibase/types" +import { EmptyFilterOption, SortOrder, Table } from "@budibase/types" import * as setup from "./utilities" +import { createAutomationBuilder } from "./utilities/AutomationTestBuilder" +import * as automation from "../index" const NAME = "Test" @@ -8,6 +10,7 @@ describe("Test a query step automation", () => { let config = setup.getConfig() beforeAll(async () => { + await automation.init() await config.init() table = await config.createTable() const row = { @@ -22,107 +25,132 @@ describe("Test a query step automation", () => { afterAll(setup.afterAll) it("should be able to run the query step", async () => { - const inputs = { - tableId: table._id, - filters: { - equal: { - name: NAME, - }, - }, - sortColumn: "name", - sortOrder: "ascending", - limit: 10, - } - const res = await setup.runStep( + const result = await createAutomationBuilder({ + name: "Basic Query Test", config, - setup.actions.QUERY_ROWS.stepId, - inputs - ) - expect(res.success).toBe(true) - expect(res.rows).toBeDefined() - expect(res.rows.length).toBe(2) - expect(res.rows[0].name).toBe(NAME) + }) + .appAction({ fields: {} }) + .queryRows( + { + tableId: table._id!, + filters: { + equal: { + name: NAME, + }, + }, + sortColumn: "name", + sortOrder: SortOrder.ASCENDING, + limit: 10, + }, + { stepName: "Query All Rows" } + ) + .run() + + expect(result.steps[0].outputs.success).toBe(true) + expect(result.steps[0].outputs.rows).toBeDefined() + expect(result.steps[0].outputs.rows.length).toBe(2) + expect(result.steps[0].outputs.rows[0].name).toBe(NAME) }) it("Returns all rows when onEmptyFilter has no value and no filters are passed", async () => { - const inputs = { - tableId: table._id, - filters: {}, - sortColumn: "name", - sortOrder: "ascending", - limit: 10, - } - const res = await setup.runStep( + const result = await createAutomationBuilder({ + name: "Empty Filter Test", config, - setup.actions.QUERY_ROWS.stepId, - inputs - ) - expect(res.success).toBe(true) - expect(res.rows).toBeDefined() - expect(res.rows.length).toBe(2) - expect(res.rows[0].name).toBe(NAME) + }) + .appAction({ fields: {} }) + .queryRows( + { + tableId: table._id!, + filters: {}, + sortColumn: "name", + sortOrder: SortOrder.ASCENDING, + limit: 10, + }, + { stepName: "Query With Empty Filter" } + ) + .run() + + expect(result.steps[0].outputs.success).toBe(true) + expect(result.steps[0].outputs.rows).toBeDefined() + expect(result.steps[0].outputs.rows.length).toBe(2) + expect(result.steps[0].outputs.rows[0].name).toBe(NAME) }) it("Returns no rows when onEmptyFilter is RETURN_NONE and theres no filters", async () => { - const inputs = { - tableId: table._id, - filters: {}, - "filters-def": [], - sortColumn: "name", - sortOrder: "ascending", - limit: 10, - onEmptyFilter: "none", - } - const res = await setup.runStep( + const result = await createAutomationBuilder({ + name: "Return None Test", config, - setup.actions.QUERY_ROWS.stepId, - inputs - ) - expect(res.success).toBe(false) - expect(res.rows).toBeDefined() - expect(res.rows.length).toBe(0) + }) + .appAction({ fields: {} }) + .queryRows( + { + tableId: table._id!, + filters: {}, + "filters-def": [], + sortColumn: "name", + sortOrder: SortOrder.ASCENDING, + limit: 10, + onEmptyFilter: EmptyFilterOption.RETURN_NONE, + }, + { stepName: "Query With Return None" } + ) + .run() + + expect(result.steps[0].outputs.success).toBe(false) + expect(result.steps[0].outputs.rows).toBeDefined() + expect(result.steps[0].outputs.rows.length).toBe(0) }) it("Returns no rows when onEmptyFilters RETURN_NONE and a filter is passed with a null value", async () => { - const inputs = { - tableId: table._id, - onEmptyFilter: "none", - filters: {}, - "filters-def": [ - { - value: null, - }, - ], - sortColumn: "name", - sortOrder: "ascending", - limit: 10, - } - const res = await setup.runStep( + const result = await createAutomationBuilder({ + name: "Null Filter Test", config, - setup.actions.QUERY_ROWS.stepId, - inputs - ) - expect(res.success).toBe(false) - expect(res.rows).toBeDefined() - expect(res.rows.length).toBe(0) + }) + .appAction({ fields: {} }) + .queryRows( + { + tableId: table._id!, + onEmptyFilter: EmptyFilterOption.RETURN_NONE, + filters: {}, + "filters-def": [ + { + value: null, + }, + ], + sortColumn: "name", + sortOrder: SortOrder.ASCENDING, + limit: 10, + }, + { stepName: "Query With Null Filter" } + ) + .run() + + expect(result.steps[0].outputs.success).toBe(false) + expect(result.steps[0].outputs.rows).toBeDefined() + expect(result.steps[0].outputs.rows.length).toBe(0) }) it("Returns rows when onEmptyFilter is RETURN_ALL and no filter is passed", async () => { - const inputs = { - tableId: table._id, - onEmptyFilter: "all", - filters: {}, - sortColumn: "name", - sortOrder: "ascending", - limit: 10, - } - const res = await setup.runStep( + const result = await createAutomationBuilder({ + name: "Return All Test", config, - setup.actions.QUERY_ROWS.stepId, - inputs - ) - expect(res.success).toBe(true) - expect(res.rows).toBeDefined() - expect(res.rows.length).toBe(2) + }) + .appAction({ fields: {} }) + .queryRows( + { + tableId: table._id!, + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + filters: {}, + sortColumn: "name", + sortOrder: SortOrder.ASCENDING, + limit: 10, + }, + { stepName: "Query With Return All" } + ) + .run() + + expect(result.steps[0].outputs.success).toBe(true) + expect(result.steps[0].outputs.rows).toBeDefined() + expect(result.steps[0].outputs.rows.length).toBe(2) }) })