August 16, 2016

Populating Ecto Database with Test Data Using Faker

When building a web application there is almost always some form of a database being utilized to persist the application's data.

Populating Ecto Database with Test Data Using Faker

Generally for the early stages of project development, it is more than sufficient to use the terminal or application views to manually populate the database for the testing with dynamic data. As a project matures though, the need to simulate how the application performs under load and testing the efficiency of database queries requires many more database entries than one would like to create manually. The solution to this problem is the creation of a elixir script that populates the database with many entries automatically to quickly allow developers more realistic testing of their applications. Below is what such a script would look like.

defmodule Project.Populater do
alias Project.Repo

  def user_attrs do
    %{
      first_name: Faker.Name.first_name,
      last_name: Faker.Name.last_name,
      company: Faker.Company.name,
      email: Faker.Internet.email,
      password: "secretsecret",
      address_1: Faker.Address.street_address,
      city: Faker.Address.city,
      state: Faker.Address.state_abbr,
      zipcode: Faker.Address.zip_code,
    }
  end

  def insert_user(attrs \\ %{}) do
    changes = Dict.merge(user_attrs, attrs)
    %Project.User{}
    |> Project.User.update_changeset(changes)
    |> Repo.insert!()
  end
end

config = %{
  users: 1000
}

for _u <- 1..config[:users] do u
  Project.Populater.insert_user
end

In the above script we utilize the Faker library to generate realistic test data randomly to better test our application and allow for easier third-party review. In order to utilize Faker you'll need to ensure that it is added to your mix.exs file like so...

{:faker, "~> 0.5", only: [:dev, :test]} 

Additionally, utilizing the config struct allows for easy modification of the scripts behavior to suit changing database testing needs. The script above could easily be expanded to populate multiple model types other than just a user to fit the needs of any application. Once such a script is added to a project, it allows developers the freedom to drop their existing database at anytime, and recreate it with new test data quickly by running mix run script_name.