Glide (glide v0.9.0) View Source
Library to help generating test data using StreamData.
It adds several generators for commonly used values and some convenience wrappers for StreamData, allowing you to generate test data for your tests. The test data is reproducible, i.e. it will use the same seed ExUnit uses, so you'll get the same data if you supply the same seed.
The generators can be used as a stepping stone for property based tests at a later stage.
Installation
The package can be installed by adding glide
to your list of dependencies in mix.exs
:
def deps do
[
{:glide, "~> 0.9.0"}
]
end
Usage
In your tests you can either import or alias Glide depending on your preference
With import syntax it will look like
import Glide
gen(:uuid) # creates generator
val(:uuid) # builds val from generator
gen(:uuid) |> val() # builds val from generator
If you want to use an alias:
alias Glide, as: G
G.gen(:uuid) # creates generator
G.val(:uuid) # builds val from generator
G.gen(:uuid) |> G.val() # builds val from generator
Instead of the wrapper functions, you can also use StreamData directly for more control.
StreamData.integer() |> G.val
Examples
Create fixed map, with optional subtitle
gen(:fixed_map, %{post_id: gen(:uuid), subtitle: optional(gen(:string, [:ascii]))})
Link to this section Summary
Functions
Generates a version 4 (random) UUID in the binary format generator
Concats two StreamData structs, underlying datastructure should implement Glide.Concat protocol.
Generates a Date by default somewhere between 1970..2050
Generates a Date by default somewhere between 1970..2050
iex> Glide.fold_gen(["1","2","3"], fn doc, acc ->
...> Glide.concat([doc, "!", acc])
...> end) |> Glide.val
"1!2!3"
Create a generator.
Merges two StreamData structs, underlying datastructure should implement Glide.Merge protocol.
Generates a Date by default somewhere between 1970..2050
Create generator for nil constant
Creates generator for optional data
Generate value from StreamData
Generates a seed
Create generator for a struct
Generates a Date by default somewhere between 1970..2050
Generates a version 4 (random) UUID generator
Create a value from a generator.
Link to this section Functions
Generates a version 4 (random) UUID in the binary format generator
iex> Glide.binuuid() |> Glide.val
<<130, 204, 75, 232, 2, 161, 72, 182, 138, 181, 5, 244, 199, 120, 124, 155>>
Concats two StreamData structs, underlying datastructure should implement Glide.Concat protocol.
Will cast any non StreamData value to a StreamData.constant By default implemented for List and String
Generates a Date by default somewhere between 1970..2050
iex> Glide.date(1980..1985) |> Glide.val
~D[1984-09-18]
Generates a Date by default somewhere between 1970..2050
iex> Glide.datetime() |> Glide.val
~U[2050-09-02 22:08:07Z]
iex> Glide.fold_gen(["1","2","3"], fn doc, acc ->
...> Glide.concat([doc, "!", acc])
...> end) |> Glide.val
"1!2!3"
Create a generator.
Mostly wraps StreamData or when the function exists any of the Glide generators
Glide.gen(:uuid)
Glide.gen(:integer)
Glide.gen(:string, :alphanumeric)
Merges two StreamData structs, underlying datastructure should implement Glide.Merge protocol.
By default implemented for Map and Keyword lists
Generates a Date by default somewhere between 1970..2050
iex> Glide.naive_datetime() |> Glide.val
~N[2050-01-22 03:54:58]
Create generator for nil constant
Can also be called with Glide.gen(nil)
Creates generator for optional data
When creating a value from this generator it will either by nil
or of the type
passed in as the argument.
Glide.optional(Glide.string(:ascii))
Generate value from StreamData
Will use a preset seed (e.g. by ExUnit) if available, otherwise will create a new seed.
See https://hexdocs.pm/stream_data/ExUnitProperties.html#pick/1
Generates a seed
Create generator for a struct
struct_of(User, %{
name: string(:ascii),
address: optional(string(:ascii))
})
Generates a Date by default somewhere between 1970..2050
iex> Glide.time() |> Glide.val
~T[14:57:31]
Generates a version 4 (random) UUID generator
iex> Glide.uuid() |> Glide.val
"30192e4a-6d03-4f9a-86cb-f301454447c2"
Create a value from a generator.
Mostly wraps StreamData or when the function exists any of the Glide generators
Glide.val(:uuid)
Glide.val(:integer)
Glide.val(:string, :alphanumeric)