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

View File

@ -17,14 +17,15 @@ jest.mock("google-spreadsheet")
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,
}
})
GoogleSpreadsheet.mockImplementation(() => mockGoogleIntegration)
import { structures } from "@budibase/backend-core/tests"
import TestConfiguration from "../../tests/utilities/TestConfiguration"
@ -53,6 +54,8 @@ describe("Google Sheets Integration", () => {
},
})
await config.init()
jest.clearAllMocks()
})
function createBasicTable(name: string, columns: string[]): Table {
@ -88,7 +91,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 +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 tableColumns = ["name"]
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)
})
})
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 })
})
})
})
})