From 74ea851fa32998ac755c628c11117da1e40b07aa Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 May 2023 09:55:46 +0200 Subject: [PATCH 1/5] Fetch google sheets --- packages/server/src/integrations/googlesheets.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/server/src/integrations/googlesheets.ts b/packages/server/src/integrations/googlesheets.ts index e965ebc86c..5cef17dde8 100644 --- a/packages/server/src/integrations/googlesheets.ts +++ b/packages/server/src/integrations/googlesheets.ts @@ -243,9 +243,10 @@ class GoogleSheetsIntegration implements DatasourcePlus { } } - getTableNames(): Promise { - // TODO: implement - return Promise.resolve([]) + async getTableNames(): Promise { + await this.connect() + const sheets = this.client.sheetsByIndex + return sheets.map(s => s.title) } getTableSchema(title: string, headerValues: string[], id?: string) { From 5ea19986b13c21db69457d714c1194fe8050a667 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 May 2023 10:14:06 +0200 Subject: [PATCH 2/5] Add basic test --- .../integrations/tests/googlesheets.spec.ts | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/server/src/integrations/tests/googlesheets.spec.ts b/packages/server/src/integrations/tests/googlesheets.spec.ts index 0e7669a957..7f6c641aa9 100644 --- a/packages/server/src/integrations/tests/googlesheets.spec.ts +++ b/packages/server/src/integrations/tests/googlesheets.spec.ts @@ -17,12 +17,14 @@ jest.mock("google-spreadsheet") const { GoogleSpreadsheet } = require("google-spreadsheet") const sheetsByTitle: { [title: string]: GoogleSpreadsheetWorksheet } = {} +const sheetsByIndex: GoogleSpreadsheetWorksheet[] = [] GoogleSpreadsheet.mockImplementation(() => { return { useOAuth2Client: jest.fn(), loadInfo: jest.fn(), sheetsByTitle, + sheetsByIndex, } }) @@ -88,7 +90,7 @@ describe("Google Sheets Integration", () => { } describe("update table", () => { - test("adding a new field will be adding a new header row", async () => { + it("adding a new field will be adding a new header row", async () => { await config.doInContext(structures.uuid(), async () => { const tableColumns = ["name", "description", "new field"] const table = createBasicTable(structures.uuid(), tableColumns) @@ -103,7 +105,7 @@ describe("Google Sheets Integration", () => { }) }) - test("removing an existing field will remove the header from the google sheet", async () => { + it("removing an existing field will remove the header from the google sheet", async () => { const sheet = await config.doInContext(structures.uuid(), async () => { const tableColumns = ["name"] const table = createBasicTable(structures.uuid(), tableColumns) @@ -123,4 +125,20 @@ describe("Google Sheets Integration", () => { expect((sheet.setHeaderRow as any).mock.calls[0][0]).toHaveLength(1) }) }) + + describe("getTableNames", () => { + it("can fetch table names", async () => { + await config.doInContext(structures.uuid(), async () => { + const sheetNames: string[] = [] + for (let i = 0; i < 5; i++) { + const sheet = createSheet({ headerValues: [] }) + sheetsByIndex.push(sheet) + sheetNames.push(sheet.title) + } + + const res = await integration.getTableNames() + expect(res).toEqual(sheetNames) + }) + }) + }) }) From b7b604ca0005c8467aaa5f1e4713e6eb85ab536f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 May 2023 10:17:42 +0200 Subject: [PATCH 3/5] Improve test --- .../integrations/tests/googlesheets.spec.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/server/src/integrations/tests/googlesheets.spec.ts b/packages/server/src/integrations/tests/googlesheets.spec.ts index 7f6c641aa9..5aabb13afc 100644 --- a/packages/server/src/integrations/tests/googlesheets.spec.ts +++ b/packages/server/src/integrations/tests/googlesheets.spec.ts @@ -18,15 +18,14 @@ const { GoogleSpreadsheet } = require("google-spreadsheet") const sheetsByTitle: { [title: string]: GoogleSpreadsheetWorksheet } = {} const sheetsByIndex: GoogleSpreadsheetWorksheet[] = [] +const mockGoogleIntegration = { + useOAuth2Client: jest.fn(), + loadInfo: jest.fn(), + sheetsByTitle, + sheetsByIndex, +} -GoogleSpreadsheet.mockImplementation(() => { - return { - useOAuth2Client: jest.fn(), - loadInfo: jest.fn(), - sheetsByTitle, - sheetsByIndex, - } -}) +GoogleSpreadsheet.mockImplementation(() => mockGoogleIntegration) import { structures } from "@budibase/backend-core/tests" import TestConfiguration from "../../tests/utilities/TestConfiguration" @@ -55,6 +54,8 @@ describe("Google Sheets Integration", () => { }, }) await config.init() + + jest.clearAllMocks() }) function createBasicTable(name: string, columns: string[]): Table { @@ -137,6 +138,8 @@ describe("Google Sheets Integration", () => { } const res = await integration.getTableNames() + + expect(mockGoogleIntegration.loadInfo).toBeCalledTimes(1) expect(res).toEqual(sheetNames) }) }) From 023373bb254326ef96e30c25a8c8d597baeab871 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 May 2023 10:18:37 +0200 Subject: [PATCH 4/5] Remove unnecessary load info --- packages/server/src/integrations/googlesheets.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/server/src/integrations/googlesheets.ts b/packages/server/src/integrations/googlesheets.ts index 5cef17dde8..d1f3f9e950 100644 --- a/packages/server/src/integrations/googlesheets.ts +++ b/packages/server/src/integrations/googlesheets.ts @@ -148,7 +148,6 @@ class GoogleSheetsIntegration implements DatasourcePlus { async testConnection(): Promise { try { await this.connect() - await this.client.loadInfo() return { connected: true } } catch (e: any) { return { From e838a90d3e22914be68be43ace9e28a75fe56905 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 May 2023 10:22:26 +0200 Subject: [PATCH 5/5] Add small unit test --- .../src/integrations/tests/googlesheets.spec.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/server/src/integrations/tests/googlesheets.spec.ts b/packages/server/src/integrations/tests/googlesheets.spec.ts index 5aabb13afc..fcb24c152a 100644 --- a/packages/server/src/integrations/tests/googlesheets.spec.ts +++ b/packages/server/src/integrations/tests/googlesheets.spec.ts @@ -144,4 +144,15 @@ describe("Google Sheets Integration", () => { }) }) }) + + describe("testConnection", () => { + it("can test successful connections", async () => { + await config.doInContext(structures.uuid(), async () => { + const res = await integration.testConnection() + + expect(mockGoogleIntegration.loadInfo).toBeCalledTimes(1) + expect(res).toEqual({ connected: true }) + }) + }) + }) })