1
0
Fork 0
mirror of synced 2024-07-13 10:15:49 +12:00
budibase/packages/datastores/tests/records.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-02-26 04:21:23 +13:00
import { action, iterateActionTimes } from "./helpers"
import { isUndefined, union } from "lodash"
const createClient = (apis, getState) => async i => {
const client = apis.recordApi.getNew("/clients", "client")
client.FamilyName = "Humperdink"
client.Address1 = `${i} Mainitucetts Avenue`
client.Address2 = "Longerton Road South"
client.Address3 = "Chalico City"
client.Address4 = "Northern Humranistan"
client.Postcode = "BY71 5FR"
client.CreatedDate = new Date()
const state = getState()
if (isUndefined(state.clientKeys)) state.clientKeys = []
state.clientKeys.push(client.key())
await apis.recordApi.save(client)
return client.key()
2019-06-08 01:18:10 +12:00
}
const listClients = (apis, getState) => async () => {
const clients = await apis.viewApi.listItems("/clients/default")
const state = getState()
if (state.clientKeys.length !== clients.length) {
throw new Error(
"list CLients, expected " +
state.clientKeys.length.toString() +
" clients, actual " +
clients.length.toString()
)
}
2019-06-08 01:18:10 +12:00
}
export default apis => {
const state = {}
const getState = () => state
2019-06-08 01:18:10 +12:00
const noOfRecords = 10000
const recordsPerIteration = 10
const noOfIterations = noOfRecords / recordsPerIteration
2019-06-08 01:18:10 +12:00
const actionsInOneIteration = () => [
action(
"Create client",
createClient(apis, getState),
iterateActionTimes(recordsPerIteration)
),
2019-06-08 01:18:10 +12:00
action("List Clients", listClients(apis, getState)),
]
2019-06-08 01:18:10 +12:00
let actions = []
for (let index = 0; index < noOfIterations; index++) {
actions = union(actions, actionsInOneIteration())
}
2019-06-08 01:18:10 +12:00
let actionIndex = 0
return () => {
if (actionIndex == actions.length) {
return { done: true }
}
const result = { action: actions[actionIndex], done: false }
actionIndex++
return result
}
}