QuickFactory.Counters (quick_factory v0.2.3)

View Source

This is a counter module that wraps :counters

Setup

We can use this in our application by calling QuickFactory.Counters.start() inside our test/test_helper.exs file.

Using Counters

We can then use this module to add unique integers to each field, each value will only be used once provided we continue to call QuickFactory.Counters.next

QuickFactory.Counters.next("my_schema_field")

Summary

Functions

Gets the counter for a specific key

Increments the counter for a specific key

Increments and gets the current value for a specific key

Puts the counter for a specific key

This starts the Counters, you need to call this before calling any of the other functions in this module. Most commonly this will go in your test/test_helper.exs file

Functions

get(key)

@spec get(key :: any()) :: integer()

Gets the counter for a specific key

Example

iex> key = Enum.random(1..10_000_000)
iex> QuickFactory.Counters.increment(key)
:ok
iex> QuickFactory.Counters.get(key)
1

inc(key)

increment(key)

@spec increment(key :: any()) :: :ok

Increments the counter for a specific key

Example

iex> key = Enum.random(1..10_000_000)
iex> QuickFactory.Counters.increment(key)
:ok
iex> QuickFactory.Counters.get(key)
1

next(key)

@spec next(key :: any()) :: integer()

Increments and gets the current value for a specific key

Example

iex> key = Enum.random(1..10_000_000)
iex> QuickFactory.Counters.next(key)
0
iex> QuickFactory.Counters.next(key)
1
iex> QuickFactory.Counters.next(key)
2

put(key, value)

@spec put(key :: any(), value :: integer()) :: :ok

Puts the counter for a specific key

Example

iex> key = Enum.random(1..10_000_000)
iex> QuickFactory.Counters.increment(key)
:ok
iex> QuickFactory.Counters.put(key, 1234)
:ok
iex> QuickFactory.Counters.get(key)
1234

reset()

start()

@spec start() :: :ok

This starts the Counters, you need to call this before calling any of the other functions in this module. Most commonly this will go in your test/test_helper.exs file