Get table deletion working.

This commit is contained in:
Sam Rose 2024-09-16 11:59:06 +01:00
parent be1517ae22
commit eaad70d031
No known key found for this signature in database
3 changed files with 31 additions and 11 deletions

View File

@ -74,6 +74,7 @@ export async function destroy(ctx: UserCtx) {
builderSocket?.emitDatasourceUpdate(ctx, datasource)
return table
} catch (err: any) {
throw err
if (err instanceof Error) {
ctx.throw(400, err.message)
} else {

View File

@ -429,13 +429,12 @@ describe("Google Sheets Integration", () => {
})
})
it.skip("can delete a table", async () => {
it("can delete a table", async () => {
await config.api.table.destroy(table._id!, table._rev!)
expect(mock.cell("A1")).toEqual(null)
expect(mock.cell("B1")).toEqual(null)
expect(mock.sheet(table.name)).toBeUndefined()
})
it.skip("can delete a row", async () => {
it.only("can delete a row", async () => {
const rows = await config.api.row.fetch(table._id!)
expect(rows.length).toEqual(2)

View File

@ -104,11 +104,17 @@ interface DeleteRangeRequest {
shiftDimension: WorksheetDimension
}
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#DeleteSheetRequest
interface DeleteSheetRequest {
sheetId: number
}
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request
interface BatchUpdateRequest {
requests: {
addSheet?: AddSheetRequest
deleteRange?: DeleteRangeRequest
deleteSheet?: DeleteSheetRequest
}[]
includeSpreadsheetInResponse: boolean
responseRanges: string[]
@ -445,6 +451,11 @@ export class GoogleSheetsMock {
}
if (request.deleteRange) {
this.handleDeleteRange(request.deleteRange)
response.replies.push({})
}
if (request.deleteSheet) {
this.handleDeleteSheet(request.deleteSheet)
response.replies.push({})
}
}
@ -489,11 +500,16 @@ export class GoogleSheetsMock {
throw new Error("Only row-based deletes are supported")
}
this.iterateRange(range, (cell, value) => {
this.iterateRange(range, cell => {
cell.userEnteredValue = this.createValue(null)
})
}
private handleDeleteSheet(request: DeleteSheetRequest) {
const { sheetId } = request
this.spreadsheet.sheets.splice(sheetId, 1)
}
private handleGetSpreadsheet(): Spreadsheet {
return this.spreadsheet
}
@ -514,17 +530,14 @@ export class GoogleSheetsMock {
return response
}
private iterateRange(
range: Required<GridRange>,
cb: (cell: CellData) => void
) {
private iterateRange(range: GridRange, cb: (cell: CellData) => void) {
const {
sheetId,
startRowIndex,
endRowIndex,
startColumnIndex,
endColumnIndex,
} = range
} = this.ensureGridRange(range)
for (let row = startRowIndex; row <= endRowIndex; row++) {
for (let col = startColumnIndex; col <= endColumnIndex; col++) {
@ -754,7 +767,7 @@ export class GoogleSheetsMock {
return this.getCellNumericIndexes(sheetId, startRowIndex, startColumnIndex)
}
cell(cell: string): Value | undefined {
public cell(cell: string): Value | undefined {
const cellData = this.cellData(cell)
if (!cellData) {
return undefined
@ -762,6 +775,13 @@ export class GoogleSheetsMock {
return this.cellValue(cellData)
}
public sheet(name: string | number): Sheet | undefined {
if (typeof name === "number") {
return this.getSheetById(name)
}
return this.getSheetByName(name)
}
private getCellNumericIndexes(
sheet: Sheet | number,
row: number,