Data Generation
Let's understand what Data Generators are and how they are beneficial.
One of the design goals of stream_data is to provide a set of tools for data generation that could also work outside of property-based testing. For example, generating random data can be useful when seeding databases with fake data.
What are data generators?
At the core of data generation are generators. A generator is a data structure that contains logic that stream_data uses to generate data. Essentially, a generator is like a function that we can call to generate random terms. Let’s start with a simple instance of a generator, StreamData.integer/0. This generator produces random integers. We can use all the stream_data generators as Elixir streams since they implement the Enumerable protocol.
Executable
StreamData.integer() |> Enum.take(5)
Output
iex> StreamData.integer() |> Enum.take(5)
[0, 0, -3, 0, -1]
stream_data generators are infinite streams of random data, so we only had to take a few items out of the stream using Enum.take/2 in this example. If we had called Enum.to_list/1 passing the generator as the argument, we would’ve waited forever.
More on stream_data generators
stream_data comes equipped with a few generators for simple data types, like the ones for integers or booleans, plus a bunch of ways to combine ...