Mishras

A powerful factory library for Elixir that simplifies test data creation for Ecto schemas with support for associations, embeds, and custom data generation.

Overview

Mishras provides a protocol-based factory system that automatically handles:

Installation

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

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

Configuration

Configure your repo in config/config.exs:

config :mishras, repo: MyApp.Repo

Usage

Basic Schema Factory

defmodule MyApp.User do
  use Ecto.Schema
  
  schema "users" do
    field :name, :string
    field :email, :string
  end
  
  def changeset(struct \\ %__MODULE__{}, attrs) do
    struct
    |> Ecto.Changeset.cast(attrs, [:name, :email])
    |> Ecto.Changeset.validate_required([:name, :email])
  end
end

# Implement the factory
defimpl Mishras.Factory, for: MyApp.User do
  use Mishras
  
  def build_map(_mode, _attrs) do
    %{
      name: "John Doe",
      email: "john@example.com"
    }
  end
end

Creating Test Data

alias Mishras.Factory
# Build a struct (no database insertion)
user = Factory.build(MyApp.User, %{name: "Jane"})

# Insert into database
user = Factory.insert(MyApp.User, %{email: "jane@example.com"})

Advanced Features

API

Main Functions

Implementation Callbacks

License

This project is licensed under the MIT License.