api-test-data-gen-lib v0.0.60
Overview
Nodejs lib to generate JSON test data from JSON
:construction::construction::construction::construction::construction::construction:
This is very much a work in progress. Examples below are rather nonsensical and documention is lacking. Having said that the basic stuff works :smile:
:construction::construction::construction::construction::construction::construction:
Key features
- Built in generators for random numbers, dates, places, strings, bank account etc.
- Generate fields from user defined arrays
- Generate multiple JSON records from a single source
- Generate multiple nested arrays
- Flexible
- Localisation support
Example JSON
{
"vars": {
"firstName":"Frank",
"firstNames": ["Fred", "John", "Zara", "Sam", "Tom", "Claire", "Louise"],
"version":1,
"iterators": {
"APIDATAGEN_root": "${randomInt(min:1,max:10)}",
"addresses": "${randomInt(min:0,max:2)}",
"creditCards": 6
}
},
"seed": {
"randomInt": "${randomInt({min:1,max:120})}",
"timestamp": "{{$timestamp}}",
"timestamp2": "${timestamp()}",
"version": "${getField(source: version)}",
"testDate":"${randomDate(start:1975/02/01, end:2022-02-04T16:00:00.000Z, format:DD-MMM-YYYY)}",
"today": "${dateNow(format:MM-DD-YYYY)}",
"yesterday":"${dateTPlus(format:MM-DD-YYYY, offset: -1)}",
"tomorrow":"${dateTPlus(format:MM-DD-YYYY, offset: 1)}",
"firstName":"${randomFirstname()}",
"middleNme":"${randomMiddlename()}",
"lastName":"${randomLastname()}",
"name": "${randomString(source: firstNames)} ${randomInt()} / ${randomDecimal( min:200, max:230, style:currency,currency:EUR)}",
"age": "${randomInt(min:1,max:120):number}/${randomInt(min:200,max:230)}",
"addresses": [
{
"seq": "${seq(name:addressId,start : 10, step: 20)}",
"address_line_1": "${randomInt(min:1,max:30,style:currency,currency:EUR,minimumFractionDigits:2,maximumFractionDigits:2)} abc street",
"address_line_2": "abc",
"country":"${randomCountry()}",
"country2":"${randomCountry(field:alpha2)}",
"country3":"${randomCountry(field:alpha3)}",
"country4":"${randomCountry(field:regionCode)}",
"currency":"${randomCurrency()}",
"currency2":"${randomCurrency(field:name)}"
}
],
"creditCards": [
{
"cardNumber": "${randomInt(min:10000,max:2000000)}",
"bank": "abc",
"amount":"${randomDecimal(locale:de-DE, maximumFractionDigits: 2, min:0, max:1000000)}",
"accountNumber":"${randomIntFixedLength(length:9)}",
"accountNumber2":"${randomBankAccount()}",
"sortCode":"${randomSortCode()}",
"bankCode": "${randomBank(field:code)}",
"bankName": "${randomBank(field:name)}"
}
]
}
}
Example usage (Typescript)
import { parse } from "api-test-data-gen-lib";
const parseRequest = (req, res) => {
const vars = req.body.vars;
const seed = req.body.seed;
const parsedData: any = parse(seed, vars);
if (parsedData.length === 0) {
return [];
}
const response = JSON.parse(parsedData);
return response;
};
{
"randomProduct": {
"code": "001",
"desc": "Sponge"
},
"randomProductDesc": "Sponge",
"randomProductCode": "001",
"randomInt": 18,
"timestamp": "1650736291",
"timestamp2": "1650736291",
"version": 1,
"testDate": "12-Feb-2010",
"today": "04-23-2022",
"yesterday": "04-22-2022",
"tomorrow": "04-24-2022",
"firstName": "Theo",
"middleNme": "Massimo",
"lastName": "Loni",
"name": "Claire ${randomInt()} / €204.27",
"age": "${randomInt(min:1,max:120):number}/${randomInt(min:200,max:230)}",
"addresses": [
{
"seq": 10,
"address_line_1": "€8.00 abc street",
"address_line_2": "abc",
"countryName": {
"name": "Norfolk Island",
"alpha2": "NF",
"alpha3": "NFK",
"countryCode": "574",
"iso3166_2": "ISO 3166-2:NF",
"region": "Oceania",
"subRegion": "Australia and New Zealand",
"intermediateRegion": "",
"regionCode": "009",
"subRegionCode": "053",
"intermediateregionCode": ""
},
"countryRegion": "Oceania",
"country2": "CA",
"country3": "SOM",
"country4": "002",
"currency": "TOP",
"currency2": "Mauritian Rupee"
}
],
"creditCards": [
{
"cardNumber": 1166040,
"amount": "26.703,15",
"accountNumber": "764934816",
"accountNumber2": "439998711",
"sortCode": "65-57-72",
"bankCode": "MPCX:",
"bankName": "Sankheda Nagarik Sahakari Bank",
"bankCode2": "LATX:",
"bankName2": "Latur Urban Co-operative Bank Latur",
"bankCode3": "LATX:"
}
]
}
Generators
Numbers
name | params | Description | example |
---|---|---|---|
randomInt | min - lowest value max - highest value | Generate a random int | \${randomInt(min:1,max:120)} |
randomDecimal | min - lowest value max - highest value | Generate a random decimal | \${randomDecimal(min:1,max:120)} |
randomIntFixedLength | min - lowest value max - highest value length - max length of resulting int in chars | Generate a random int with a fixed length | \${randomIntFixedLength(min:1,max:120,length:2)} |
seq | name - name of the sequence start - start int of sequence step - the amount that the sequence is incremented | Generate a random int with a fixed length | \${seq(name:addressId,start:10,step: 20)} |
Strings
randomRecord(options, vars) ⇒ string
Kind: global function
Returns: string - a random string
Param | Type | Description |
---|---|---|
options | Object | |
options.source | string | property name in vars of the source array |
options.field | string | field name within the source to select |
vars | Object | object containing the source array |
INPUT
{
"vars": {
"products": [{"code":"001","desc":"Sponge"},{"code":"002","desc":"Hat"},{"code":"003","desc":"shell Suit"}],
"iterators": {
"APIDATAGEN_root": 2
}
},
"seed": {
"randomProduct":"${randomRecord(source:products)}"
}
}
OUPUT
[
{
"randomProduct": {
"code": "003",
"desc": "shell Suit"
}
},
{
"randomProduct": {
"code": "002",
"desc": "Hat"
}
}
]
Dates
Finance
Names
Places
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago