Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add examples and doctest for FactoryEx #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Config

config :factory_ex, :ecto_repos, [FactoryEx.Support.Repo]

config :factory_ex, FactoryEx.Support.Repo,
username: System.get_env("POSTGRES_USER") || "postgres",
password: System.get_env("POSTGRES_PASSWORD") || "postgres",
database: "factory_ex_test",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox,
pool_size: String.to_integer(System.get_env("POSTGRES_POOL_SIZE", "10"))
34 changes: 34 additions & 0 deletions lib/factory_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ defmodule FactoryEx do
@doc """
Builds the parameters for a schema `changeset/2` function given the factory
`module` and an optional list/map of `params`.

### Example

iex> FactoryEx.build_params(TestFactory)
%{foo: 21, bar: 42}

"""
@spec build_params(module()) :: map()
@spec build_params(module(), keyword() | map()) :: map()
Expand All @@ -46,6 +52,12 @@ defmodule FactoryEx do
@doc """
Builds a schema given the factory `module` and an optional
list/map of `params`.

### Example

iex> FactoryEx.build(TestFactory)
%MySchema{foo: 21, bar: 42}

"""
@spec build(module()) :: Ecto.Schema.t()
@spec build(module(), keyword() | map()) :: Ecto.Schema.t()
Expand All @@ -62,6 +74,14 @@ defmodule FactoryEx do
@doc """
Inserts a schema given the factory `module` and an optional list/map of
`params`. Fails on error.

### Example

iex> factory = FactoryEx.insert!(TestFactory)
%MySchema{foo: 21, bar: 42}
iex> MyRepo.get(factory.id)
factory

"""
@spec insert!(module()) :: Ecto.Schema.t() | no_return()
@spec insert!(module(), keyword() | map(), Keyword.t()) :: Ecto.Schema.t() | no_return()
Expand All @@ -80,6 +100,12 @@ defmodule FactoryEx do
@doc """
Insert as many as `count` schemas given the factory `module` and an optional
list/map of `params`.

### Example

iex> FactoryEx.insert_many!(2, TestFactory)
[%MySchema{foo: 21, bar: 42}, %MySchema{foo: 21, bar: 42}]

"""
@spec insert_many!(pos_integer(), module()) :: [Ecto.Schema.t()]
@spec insert_many!(pos_integer(), module(), keyword() | map()) :: [Ecto.Schema.t()]
Expand All @@ -90,6 +116,14 @@ defmodule FactoryEx do
@doc """
Removes all the instances of a schema from the database given its factory
`module`.

### Example

iex> FactoryEx.insert!(TestFactory)
%MySchema{foo: 21, bar: 42}
iex> MyRepo.cleanup(TestFactory)
{1, []}

"""
@spec cleanup(module) :: {integer(), nil | [term()]}
def cleanup(module, options \\ []) do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ defmodule FactoryEx.MixProject do
maintainers: ["Mika Kalathil"],
licenses: ["MIT"],
links: %{"GitHub" => "https://github.com/theblitzapp/factory_ex"},
files: ~w(mix.exs README.md CHANGELOG.md LICENSE lib config)
files: ~w(mix.exs mix.lock README.md CHANGELOG.md LICENSE lib)
]
end

Expand Down
31 changes: 19 additions & 12 deletions test/factory_ex_test.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
defmodule FactoryExTest do
use ExUnit.Case
doctest FactoryEx

defmodule MyRepo do
@moduledoc false
use Ecto.Repo,
otp_app: :my_repo,
adapter: Ecto.Adapters.Postgres
end

defmodule MySchema do
use Ecto.Schema
Expand All @@ -32,7 +24,7 @@ defmodule FactoryExTest do

def schema, do: MySchema

def repo, do: MyRepo
def repo, do: FactoryEx.Support.Repo

def build(params \\ %{}) do
default = %{
Expand All @@ -44,11 +36,26 @@ defmodule FactoryExTest do
end
end

test "can generate a factory" do
# assert %MySchema{foo: 21, bar: 42} = FactoryEx.insert!(TestFactory)
# assert %MySchema{foo: 21, bar: 10} = FactoryEx.insert!(TestFactory, bar: 10)
setup_all do
{:ok, _} = Ecto.Adapters.Postgres.ensure_all_started(FactoryEx.Support.Repo, :temporary)

_ = Ecto.Adapters.Postgres.storage_down(FactoryEx.Support.Repo.config())
:ok = Ecto.Adapters.Postgres.storage_up(FactoryEx.Support.Repo.config())

{:ok, _pid} = FactoryEx.Support.Repo.start_link()

%{}
end

doctest FactoryEx

test "can generate a factory" do
assert %{foo: 21, bar: 42} = TestFactory.build()
assert %{foo: 21, bar: 10} = TestFactory.build(%{bar: 10})
end

test "can insert a factory" do
assert %MySchema{foo: 21, bar: 42} = FactoryEx.insert!(TestFactory)
assert %MySchema{foo: 21, bar: 10} = FactoryEx.insert!(TestFactory, bar: 10)
end
end