bentonam / fakeit

Generates JSON documents based on models defined in YAML and adds them to a Couchbase Bucket
MIT License
86 stars 21 forks source link

Allow the usage of seeds #108

Closed tjbenton closed 7 years ago

tjbenton commented 7 years ago

Using a seed will produce the same set of data each time, and can be useful to some. Both Chance and Faker allow you to set a seed to return the same data set each time at the point the random data was called.

We would still generate a random number of documents not using the seed. This way the randomness is still there unaffected by the seed.

The seed can be set locally on the models, or globally. This would work in the same way that the count variable is set. If set globally then it will be used instead of the local seed. If a seed isn't set then a random seed will be set.

In the document loop we would create a new instance of faker and chance and set their seed by using the current seed plus the current document_index this way each document is still random but based off the seed and current index.

Example:

import Fakeit from 'fakeit'
import test from 'ava-spec'

test(async (seed) => { 
  const fakeit = new Fakeit({ seed: 'abc', count: 5 })
  let data_sets = []
  for (let i = 0; i < 10; i++) {
    data_sets.push(fakeit.generate('models/**/*.yaml'))
  }
  data_sets = await Promise.all(data_sets)
  // array of 10 data sets, with 5 different document 
  data_sets = data_sets.map((data_set) => JSON.parse(data_set))

  // the documents in the data_sets are the same, and in the same order
  data_sets.reduce((prev, next) => {
    prev.forEach((document, i) => {
      // each document is the same as first document
      t.deepEqual(next[i], document)
    })
    return prev
  }, data_sets[0])

  # each document in the data set is different
  t.notDeepEqual(data_sets[0][0], data_sets[0][1])
  # but the second item will always be the same in each set
  t.notDeepEqual(data_sets[0][1], data_sets[1][1])
})