Get table deletion working.
This commit is contained in:
parent
be1517ae22
commit
eaad70d031
|
@ -74,6 +74,7 @@ export async function destroy(ctx: UserCtx) {
|
||||||
builderSocket?.emitDatasourceUpdate(ctx, datasource)
|
builderSocket?.emitDatasourceUpdate(ctx, datasource)
|
||||||
return table
|
return table
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
throw err
|
||||||
if (err instanceof Error) {
|
if (err instanceof Error) {
|
||||||
ctx.throw(400, err.message)
|
ctx.throw(400, err.message)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -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!)
|
await config.api.table.destroy(table._id!, table._rev!)
|
||||||
expect(mock.cell("A1")).toEqual(null)
|
expect(mock.sheet(table.name)).toBeUndefined()
|
||||||
expect(mock.cell("B1")).toEqual(null)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it.skip("can delete a row", async () => {
|
it.only("can delete a row", async () => {
|
||||||
const rows = await config.api.row.fetch(table._id!)
|
const rows = await config.api.row.fetch(table._id!)
|
||||||
expect(rows.length).toEqual(2)
|
expect(rows.length).toEqual(2)
|
||||||
|
|
||||||
|
|
|
@ -104,11 +104,17 @@ interface DeleteRangeRequest {
|
||||||
shiftDimension: WorksheetDimension
|
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
|
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request
|
||||||
interface BatchUpdateRequest {
|
interface BatchUpdateRequest {
|
||||||
requests: {
|
requests: {
|
||||||
addSheet?: AddSheetRequest
|
addSheet?: AddSheetRequest
|
||||||
deleteRange?: DeleteRangeRequest
|
deleteRange?: DeleteRangeRequest
|
||||||
|
deleteSheet?: DeleteSheetRequest
|
||||||
}[]
|
}[]
|
||||||
includeSpreadsheetInResponse: boolean
|
includeSpreadsheetInResponse: boolean
|
||||||
responseRanges: string[]
|
responseRanges: string[]
|
||||||
|
@ -445,6 +451,11 @@ export class GoogleSheetsMock {
|
||||||
}
|
}
|
||||||
if (request.deleteRange) {
|
if (request.deleteRange) {
|
||||||
this.handleDeleteRange(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")
|
throw new Error("Only row-based deletes are supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
this.iterateRange(range, (cell, value) => {
|
this.iterateRange(range, cell => {
|
||||||
cell.userEnteredValue = this.createValue(null)
|
cell.userEnteredValue = this.createValue(null)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private handleDeleteSheet(request: DeleteSheetRequest) {
|
||||||
|
const { sheetId } = request
|
||||||
|
this.spreadsheet.sheets.splice(sheetId, 1)
|
||||||
|
}
|
||||||
|
|
||||||
private handleGetSpreadsheet(): Spreadsheet {
|
private handleGetSpreadsheet(): Spreadsheet {
|
||||||
return this.spreadsheet
|
return this.spreadsheet
|
||||||
}
|
}
|
||||||
|
@ -514,17 +530,14 @@ export class GoogleSheetsMock {
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
private iterateRange(
|
private iterateRange(range: GridRange, cb: (cell: CellData) => void) {
|
||||||
range: Required<GridRange>,
|
|
||||||
cb: (cell: CellData) => void
|
|
||||||
) {
|
|
||||||
const {
|
const {
|
||||||
sheetId,
|
sheetId,
|
||||||
startRowIndex,
|
startRowIndex,
|
||||||
endRowIndex,
|
endRowIndex,
|
||||||
startColumnIndex,
|
startColumnIndex,
|
||||||
endColumnIndex,
|
endColumnIndex,
|
||||||
} = range
|
} = this.ensureGridRange(range)
|
||||||
|
|
||||||
for (let row = startRowIndex; row <= endRowIndex; row++) {
|
for (let row = startRowIndex; row <= endRowIndex; row++) {
|
||||||
for (let col = startColumnIndex; col <= endColumnIndex; col++) {
|
for (let col = startColumnIndex; col <= endColumnIndex; col++) {
|
||||||
|
@ -754,7 +767,7 @@ export class GoogleSheetsMock {
|
||||||
return this.getCellNumericIndexes(sheetId, startRowIndex, startColumnIndex)
|
return this.getCellNumericIndexes(sheetId, startRowIndex, startColumnIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
cell(cell: string): Value | undefined {
|
public cell(cell: string): Value | undefined {
|
||||||
const cellData = this.cellData(cell)
|
const cellData = this.cellData(cell)
|
||||||
if (!cellData) {
|
if (!cellData) {
|
||||||
return undefined
|
return undefined
|
||||||
|
@ -762,6 +775,13 @@ export class GoogleSheetsMock {
|
||||||
return this.cellValue(cellData)
|
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(
|
private getCellNumericIndexes(
|
||||||
sheet: Sheet | number,
|
sheet: Sheet | number,
|
||||||
row: number,
|
row: number,
|
||||||
|
|
Loading…
Reference in New Issue