0.0.60 • Published 2 years ago

api-test-data-gen-lib v0.0.60

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

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

nameparamsDescriptionexample
randomIntmin - lowest value max - highest valueGenerate a random int\${randomInt(min:1,max:120)}
randomDecimalmin - lowest value max - highest valueGenerate a random decimal\${randomDecimal(min:1,max:120)}
randomIntFixedLengthmin - lowest value max - highest value length - max length of resulting int in charsGenerate a random int with a fixed length\${randomIntFixedLength(min:1,max:120,length:2)}
seqname - name of the sequence start - start int of sequence step - the amount that the sequence is incrementedGenerate 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

ParamTypeDescription
optionsObject
options.sourcestringproperty name in vars of the source array
options.fieldstringfield name within the source to select
varsObjectobject 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

0.0.60

2 years ago

0.0.59

2 years ago

0.0.58

2 years ago

0.0.57

2 years ago

0.0.56

2 years ago

0.0.55

2 years ago

0.0.54

2 years ago

0.0.53

2 years ago

0.0.52

2 years ago

0.0.51

2 years ago

0.0.50

2 years ago

0.0.49

2 years ago

0.0.48

2 years ago

0.0.47

2 years ago

0.0.46

2 years ago

0.0.45

2 years ago

0.0.44

2 years ago

0.0.43

2 years ago

0.0.42

2 years ago

0.0.41

2 years ago

0.0.40

2 years ago

0.0.39

2 years ago

0.0.38

2 years ago

0.0.37

2 years ago

0.0.36

2 years ago

0.0.35

2 years ago

0.0.34

2 years ago

0.0.33

2 years ago

0.0.32

2 years ago

0.0.31

2 years ago

0.0.30

2 years ago

0.0.29

2 years ago

0.0.28

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago