Merge pull request #10663 from Budibase/budi-6944-google-sheets-fetch-all-worksheets-when

Fetch table names from google datasource
This commit is contained in:
Adria Navarro 2023-05-23 10:30:18 +02:00 committed by GitHub
commit 7b603a35df
2 changed files with 45 additions and 13 deletions

View File

@ -148,7 +148,6 @@ class GoogleSheetsIntegration implements DatasourcePlus {
async testConnection(): Promise<ConnectionInfo> { async testConnection(): Promise<ConnectionInfo> {
try { try {
await this.connect() await this.connect()
await this.client.loadInfo()
return { connected: true } return { connected: true }
} catch (e: any) { } catch (e: any) {
return { return {
@ -243,9 +242,10 @@ class GoogleSheetsIntegration implements DatasourcePlus {
} }
} }
getTableNames(): Promise<string[]> { async getTableNames(): Promise<string[]> {
// TODO: implement await this.connect()
return Promise.resolve([]) const sheets = this.client.sheetsByIndex
return sheets.map(s => s.title)
} }
getTableSchema(title: string, headerValues: string[], id?: string) { getTableSchema(title: string, headerValues: string[], id?: string) {

View File

@ -17,14 +17,15 @@ jest.mock("google-spreadsheet")
const { GoogleSpreadsheet } = require("google-spreadsheet") const { GoogleSpreadsheet } = require("google-spreadsheet")
const sheetsByTitle: { [title: string]: GoogleSpreadsheetWorksheet } = {} const sheetsByTitle: { [title: string]: GoogleSpreadsheetWorksheet } = {}
const sheetsByIndex: GoogleSpreadsheetWorksheet[] = []
GoogleSpreadsheet.mockImplementation(() => { const mockGoogleIntegration = {
return {
useOAuth2Client: jest.fn(), useOAuth2Client: jest.fn(),
loadInfo: jest.fn(), loadInfo: jest.fn(),
sheetsByTitle, sheetsByTitle,
sheetsByIndex,
} }
})
GoogleSpreadsheet.mockImplementation(() => mockGoogleIntegration)
import { structures } from "@budibase/backend-core/tests" import { structures } from "@budibase/backend-core/tests"
import TestConfiguration from "../../tests/utilities/TestConfiguration" import TestConfiguration from "../../tests/utilities/TestConfiguration"
@ -53,6 +54,8 @@ describe("Google Sheets Integration", () => {
}, },
}) })
await config.init() await config.init()
jest.clearAllMocks()
}) })
function createBasicTable(name: string, columns: string[]): Table { function createBasicTable(name: string, columns: string[]): Table {
@ -88,7 +91,7 @@ describe("Google Sheets Integration", () => {
} }
describe("update table", () => { 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 () => { await config.doInContext(structures.uuid(), async () => {
const tableColumns = ["name", "description", "new field"] const tableColumns = ["name", "description", "new field"]
const table = createBasicTable(structures.uuid(), tableColumns) const table = createBasicTable(structures.uuid(), tableColumns)
@ -103,7 +106,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 sheet = await config.doInContext(structures.uuid(), async () => {
const tableColumns = ["name"] const tableColumns = ["name"]
const table = createBasicTable(structures.uuid(), tableColumns) const table = createBasicTable(structures.uuid(), tableColumns)
@ -123,4 +126,33 @@ describe("Google Sheets Integration", () => {
expect((sheet.setHeaderRow as any).mock.calls[0][0]).toHaveLength(1) 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(mockGoogleIntegration.loadInfo).toBeCalledTimes(1)
expect(res).toEqual(sheetNames)
})
})
})
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 })
})
})
})
}) })