Skip to content

Use this code to build your own Elixir module that helps build Ecto test data.

Notifications You must be signed in to change notification settings

marick/ecto_test_data_builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

README

This provides support code for writing Ecto test data builders that are used like this:

repo = 
  empty_repo()
  |> procedure("haltering", frequency: "twice per week")
  |> reservation_for(["bossie"], ["haltering"], date: @wed)
  |> reservation_for(["bossie"], ["haltering"], date: @mon)

That code constructs test data for a database configuration like this:

I claim that code is a better way to set up test data than other approaches.

In addition to creating rows in database tables, the functions produce a "repo cache", conventionally bound to repo, that contains a view into the database that makes common testing operations simpler.

For example, it's straightforward to have the repo structure contain top-level fields that point directly to important values. This allows you to avoid the busywork of keeping track of database ids. Instead, there's only one "source of truth" and you use that:

# setup

repo = 
  ...
  |> animal("bossie", ...)
...

# The function under test

... VM.Animal.fetch(:one_for_edit, repo.bossie.id) ...
                                   ^^^^^^^^^^^^^^

That's surprisingly useful.

More information

Online documentation

See USE.md for a description of using this package to create a custom test-data builder.

Installation

Add ecto_test_data_builder to your list of dependencies in mix.exs:

def deps do
  [
    {:ecto_test_data_builder, "~> 0.1.0"}
  ]
end

About

Use this code to build your own Elixir module that helps build Ecto test data.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages