From ebd93eb109fc85c25a2dece72da47c881a3bee63 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 18 Jul 2023 14:34:23 +0200 Subject: [PATCH] Persist queries on crud views --- .../src/api/routes/tests/viewV2.spec.ts | 50 ++++++++++++++++++- .../src/tests/utilities/TestConfiguration.ts | 16 ++++-- packages/types/src/documents/app/view.ts | 2 + 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index e395e59044..23f9918c16 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -53,7 +53,9 @@ describe("/v2/views", () => { }) it("can filter by table id", async () => { - const newTable = await config.createTable(priceTable()) + const newTable = await config.createTable(priceTable(), { + skipReassigning: true, + }) const newViews = [] for (let id = 0; id < 5; id++) { newViews.push(await config.api.viewV2.create({ tableId: newTable._id })) @@ -79,12 +81,32 @@ describe("/v2/views", () => { expect(res.body.message).toBe("tableId type is not valid") }) + + it("returns views with query info", async () => { + const newView = await config.api.viewV2.create({ + query: { allOr: false, equal: { field: "value" } }, + }) + views.push(newView) + const res = await request + .get(`/api/v2/views?tableId=${config.table!._id}`) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + + expect(res.body.views.length).toBe(11) + expect(newView.query).toEqual({ allOr: false, equal: { field: "value" } }) + expect(res.body.views).toEqual( + expect.arrayContaining([expect.objectContaining(newView)]) + ) + }) }) describe("getView", () => { let view: ViewV2 beforeAll(async () => { - view = await config.api.viewV2.create() + view = await config.api.viewV2.create({ + query: { allOr: false, notEqual: { field: "value" } }, + }) }) it("can fetch the expected view", async () => { @@ -123,6 +145,30 @@ describe("/v2/views", () => { _rev: expect.any(String), }) }) + + it("can persist views with queries", async () => { + const query = { allOr: false, notContains: { name: ["a", "b"] } } + const newView: ViewV2 = { + name: generator.name(), + tableId: config.table!._id!, + query, + } + const res = await request + .post(`/api/v2/views`) + .send(newView) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(201) + + expect(res.body).toEqual({ + data: { + ...newView, + query, + _id: expect.any(String), + _rev: expect.any(String), + }, + }) + }) }) describe("delete", () => { diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 7cf60e6cf7..a93c78d5fc 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -527,17 +527,23 @@ class TestConfiguration { // TABLE - async updateTable(config?: any): Promise { + async updateTable( + config?: any, + { skipReassigning } = { skipReassigning: false } + ): Promise
{ config = config || basicTable() - this.table = await this._req(config, null, controllers.table.save) - return this.table! + const response = await this._req(config, null, controllers.table.save) + if (!skipReassigning) { + this.table = response + } + return response } - async createTable(config?: Table) { + async createTable(config?: Table, options = { skipReassigning: false }) { if (config != null && config._id) { delete config._id } - return this.updateTable(config) + return this.updateTable(config, options) } async getTable(tableId?: string) { diff --git a/packages/types/src/documents/app/view.ts b/packages/types/src/documents/app/view.ts index f3aad235cc..d8c09ae1e0 100644 --- a/packages/types/src/documents/app/view.ts +++ b/packages/types/src/documents/app/view.ts @@ -1,3 +1,4 @@ +import { SearchFilters } from "../../sdk" import { Document } from "../document" export interface View { @@ -15,6 +16,7 @@ export interface View { export interface ViewV2 extends Document { name: string tableId: string + query?: SearchFilters } export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema