From 1d23e151a5575c7d01730760fb854842a36ea957 Mon Sep 17 00:00:00 2001 From: MikaAK Date: Thu, 14 Jul 2022 22:20:13 -0700 Subject: [PATCH] chore: add examples and doctest for FactoryEx --- lib/config/config.exs | 11 +++++++++++ lib/factory_ex.ex | 34 ++++++++++++++++++++++++++++++++++ mix.exs | 2 +- test/factory_ex_test.exs | 31 +++++++++++++++++++------------ 4 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 lib/config/config.exs diff --git a/lib/config/config.exs b/lib/config/config.exs new file mode 100644 index 0000000..4b60469 --- /dev/null +++ b/lib/config/config.exs @@ -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")) diff --git a/lib/factory_ex.ex b/lib/factory_ex.ex index 941899e..6094c7c 100644 --- a/lib/factory_ex.ex +++ b/lib/factory_ex.ex @@ -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() @@ -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() @@ -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() @@ -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()] @@ -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 diff --git a/mix.exs b/mix.exs index 842c4fb..ede51e4 100644 --- a/mix.exs +++ b/mix.exs @@ -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 diff --git a/test/factory_ex_test.exs b/test/factory_ex_test.exs index d06b755..a6f5af1 100644 --- a/test/factory_ex_test.exs +++ b/test/factory_ex_test.exs @@ -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 @@ -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 = %{ @@ -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