Add operations on rows and columns
This commit is contained in:
parent
eb1e85fb46
commit
814670cbed
|
@ -6,8 +6,9 @@ import FormData from "form-data"
|
|||
import { RouteConfig } from "../fixtures/types/routing"
|
||||
import { AppPackageResponse } from "../fixtures/types/appPackage"
|
||||
import { DeployConfig } from "../fixtures/types/deploy"
|
||||
import { responseMessage } from "../fixtures/types/responseMessage"
|
||||
|
||||
|
||||
type messageResponse = { message: string }
|
||||
|
||||
export default class AppApi {
|
||||
api: InternalAPIClient
|
||||
|
@ -54,7 +55,7 @@ export default class AppApi {
|
|||
return [response, json.data]
|
||||
}
|
||||
|
||||
async sync(appId: string): Promise<[Response, messageResponse]> {
|
||||
async sync(appId: string): Promise<[Response, responseMessage]> {
|
||||
const response = await this.api.post(`/applications/${appId}/sync`)
|
||||
const json = await response.json()
|
||||
return [response, json]
|
||||
|
@ -72,7 +73,7 @@ export default class AppApi {
|
|||
return [response, json]
|
||||
}
|
||||
|
||||
async revert(appId: string): Promise<[Response, messageResponse]> {
|
||||
async revert(appId: string): Promise<[Response, responseMessage]> {
|
||||
const response = await this.api.post(`/dev/${appId}/revert`)
|
||||
const json = await response.json()
|
||||
return [response, json]
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { Response } from "node-fetch"
|
||||
import { Table } from "@budibase/types"
|
||||
import { Row, Table } from "@budibase/types"
|
||||
import InternalAPIClient from "./InternalAPIClient"
|
||||
import { responseMessage } from "../fixtures/types/responseMessage"
|
||||
|
||||
|
||||
export default class TablesApi {
|
||||
|
@ -10,28 +11,54 @@ export default class TablesApi {
|
|||
this.api = apiClient
|
||||
}
|
||||
|
||||
async getTables(): Promise<[Response, Table[]]> {
|
||||
async getTables(expectedNumber: Number): Promise<[Response, Table[]]> {
|
||||
const response = await this.api.get(`/tables`)
|
||||
const json = await response.json()
|
||||
expect(response).toHaveStatusCode(200)
|
||||
expect(json.length).toBe(expectedNumber)
|
||||
return [response, json]
|
||||
}
|
||||
|
||||
async getTable(tableId: string): Promise<[Response, Table]> {
|
||||
const response = await this.api.get(`/tables/${tableId}`)
|
||||
async getTableById(id: string): Promise<[Response, Table]> {
|
||||
const response = await this.api.get(`/tables/${id}`)
|
||||
const json = await response.json()
|
||||
return [response, json]
|
||||
}
|
||||
|
||||
async createTable(body: any): Promise<[Response, Table]> {
|
||||
async create(body: any): Promise<[Response, Table]> {
|
||||
const response = await this.api.post(`/tables`, { body })
|
||||
const json = await response.json()
|
||||
return [response, json]
|
||||
}
|
||||
|
||||
async deleteTable(tableId: string, revId: string): Promise<[Response, Table]> {
|
||||
const response = await this.api.del(`/tables/${tableId}/${revId}`)
|
||||
async deleteTable(id: string, revId: string): Promise<[Response, responseMessage]> {
|
||||
const response = await this.api.del(`/tables/${id}/${revId}`)
|
||||
const json = await response.json()
|
||||
return [response, json]
|
||||
}
|
||||
|
||||
async update(body: any): Promise<[Response, Table]> {
|
||||
const response = await this.api.put(`/tables`, { body })
|
||||
const json = await response.json()
|
||||
return [response, json]
|
||||
}
|
||||
|
||||
async getRows(id: string): Promise<[Response, Row[]]> {
|
||||
const response = await this.api.get(`/${id}/rows`)
|
||||
const json = await response.json()
|
||||
return [response, json]
|
||||
}
|
||||
async addRow(id: string, body: any): Promise<[Response, Row]> {
|
||||
const response = await this.api.post(`/${id}/rows`, { body })
|
||||
const json = await response.json()
|
||||
return [response, json]
|
||||
}
|
||||
|
||||
async deleteRow(id: string, body: any): Promise<[Response, Row[]]> {
|
||||
const response = await this.api.del(`/${id}/rows/`, { body })
|
||||
const json = await response.json()
|
||||
return [response, json]
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
import { Row } from "@budibase/types"
|
||||
|
||||
type Table = {
|
||||
type?: string
|
||||
views?: { [key: string]: any }
|
||||
name: string
|
||||
primary?: string[]
|
||||
schema: { [key: string]: any }
|
||||
primaryDisplay?: string
|
||||
sourceId?: string
|
||||
relatedFormula?: string[]
|
||||
constrained?: string[]
|
||||
_id?: string
|
||||
_rev?: string
|
||||
createdAt?: string
|
||||
updatedAt?: string
|
||||
indexes?: { [key: string]: any }
|
||||
dataImport?: { [key: string]: any }
|
||||
|
||||
}
|
||||
|
||||
export const generateTable = (): Table => {
|
||||
return {
|
||||
name: "Test Table",
|
||||
schema: {},
|
||||
sourceId: "bb_internal",
|
||||
type: "internal",
|
||||
dataImport: {
|
||||
valid: true,
|
||||
schema: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const generateNewColumnForTable = (tableData: any): Table => {
|
||||
const newColumn = tableData
|
||||
newColumn.schema = {
|
||||
TestColumn: {
|
||||
type: "string",
|
||||
name: "TestColumn",
|
||||
constraints: {
|
||||
presence: { allowEmpty: false },
|
||||
length: { maximum: null },
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
newColumn.indexes = {
|
||||
0: "TestColumn"
|
||||
}
|
||||
newColumn.updatedAt = new Date().toISOString()
|
||||
return newColumn
|
||||
}
|
||||
|
||||
export const generateNewRowForTable = (tableId: string): Row => {
|
||||
const newRow = {
|
||||
TestColumn: "TestRow",
|
||||
tableId: tableId
|
||||
}
|
||||
return newRow
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
export interface responseMessage { message: string }
|
|
@ -5,6 +5,7 @@ import InternalAPIClient from "../../../config/internal-api/TestConfiguration/In
|
|||
import generateApp from "../../../config/internal-api/fixtures/applications"
|
||||
import generator from "../../../config/generator"
|
||||
import generateScreen from "../../../config/internal-api/fixtures/screens"
|
||||
import { generateTable, generateNewColumnForTable, generateNewRowForTable } from "../../../config/internal-api/fixtures/table"
|
||||
|
||||
describe("Internal API - /applications endpoints", () => {
|
||||
const api = new InternalAPIClient()
|
||||
|
@ -175,4 +176,67 @@ describe("Internal API - /applications endpoints", () => {
|
|||
const [deleteResponse] = await config.applications.delete(<string>app.appId)
|
||||
expect(deleteResponse).toHaveStatusCode(200)
|
||||
})
|
||||
|
||||
it("Operations on Tables", async () => {
|
||||
// create the app
|
||||
const appName = generator.word()
|
||||
const app = await createAppFromTemplate()
|
||||
config.applications.api.appId = app.appId
|
||||
|
||||
// Get current tables: expect 2 in this template
|
||||
await config.tables.getTables(2)
|
||||
|
||||
// Add new table
|
||||
const [createdTableResponse, createdTableData] = await config.tables.create(generateTable())
|
||||
expect(createdTableResponse).toHaveStatusCode(200)
|
||||
expect(createdTableData._id).toBeDefined()
|
||||
expect(createdTableData._rev).toBeDefined()
|
||||
|
||||
//Table was added
|
||||
await config.tables.getTables(3)
|
||||
|
||||
//Get information about the table
|
||||
const [tableInfoResponse, tableInfo] = await config.tables.getTableById(<string>createdTableData._id)
|
||||
expect(tableInfoResponse).toHaveStatusCode(200)
|
||||
expect(tableInfo._id).toEqual(createdTableData._id)
|
||||
|
||||
//Add Column to table
|
||||
const newColumn = generateNewColumnForTable(createdTableData)
|
||||
const [addColumnResponse, addColumnData] = await config.tables.create(newColumn)
|
||||
expect(addColumnResponse).toHaveStatusCode(200)
|
||||
expect(addColumnData._id).toEqual(createdTableData._id)
|
||||
expect(addColumnData.schema.TestColumn).toBeDefined()
|
||||
|
||||
//Add Row to table
|
||||
const newRow = generateNewRowForTable(<string>addColumnData._id)
|
||||
const [addRowResponse, addRowData] = await config.tables.addRow(<string>addColumnData._id, newRow)
|
||||
console.log(addRowData)
|
||||
expect(addRowResponse).toHaveStatusCode(200)
|
||||
expect(addRowData._id).toBeDefined()
|
||||
expect(addRowData._rev).toBeDefined()
|
||||
expect(addRowData.tableId).toEqual(addColumnData._id)
|
||||
|
||||
//Get Row from table
|
||||
const [getRowResponse, getRowData] = await config.tables.getRows(<string>addColumnData._id)
|
||||
expect(getRowResponse).toHaveStatusCode(200)
|
||||
expect(getRowData.length).toEqual(1)
|
||||
|
||||
//Delete Row from table
|
||||
const rowToDelete = {
|
||||
rows: [
|
||||
getRowData[0]
|
||||
]
|
||||
}
|
||||
const [deleteRowResponse, deleteRowData] = await config.tables.deleteRow(<string>addColumnData._id, rowToDelete)
|
||||
expect(deleteRowResponse).toHaveStatusCode(200)
|
||||
expect(deleteRowData[0]._id).toEqual(getRowData[0]._id)
|
||||
|
||||
//Delete the table
|
||||
const [deleteTableResponse, deleteTable] = await config.tables.deleteTable(<string>addColumnData._id, <string>addColumnData._rev)
|
||||
expect(deleteTableResponse).toHaveStatusCode(200)
|
||||
expect(deleteTable.message).toEqual(`Table ${createdTableData._id} deleted.`)
|
||||
|
||||
//Table was deleted
|
||||
await config.tables.getTables(2)
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue