Skip to content

Commit 49192a3

Browse files
committed
feat: add basic test
1 parent ce9506c commit 49192a3

File tree

6 files changed

+103
-5
lines changed

6 files changed

+103
-5
lines changed

config/config.exs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Config
2+
3+
config :factory_ex, FactoryExTest.MyRepo,
4+
username: System.get_env("POSTGRES_USER") || "postgres",
5+
password: System.get_env("POSTGRES_PASSWORD") || "postgres",
6+
database: "factory_ex_test",
7+
hostname: "localhost",
8+
pool: Ecto.Adapters.SQL.Sandbox,
9+
pool_size: String.to_integer(System.get_env("POSTGRES_POOL_SIZE", "10"))

lib/factory_ex.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ defmodule FactoryEx do
4040
def build_params(module, params) do
4141
params
4242
|> module.build()
43-
|> SharedUtils.Map.deep_struct_to_map()
43+
|> FactoryEx.Utils.deep_struct_to_map()
4444
end
4545

4646
@doc """

lib/factory_ex/utils.ex

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
defmodule FactoryEx.Utils do
2+
@struct_fields [:__meta__]
3+
@whitelisted_modules [DateTime]
4+
5+
@doc """
6+
Changes structs into maps all the way down, excluding
7+
things like DateTime.
8+
"""
9+
@spec deep_struct_to_map(any) :: any
10+
def deep_struct_to_map(%module{} = struct) when module in @whitelisted_modules do
11+
struct
12+
end
13+
14+
def deep_struct_to_map(struct) when is_struct(struct) do
15+
struct
16+
|> Map.from_struct()
17+
|> Map.drop(@struct_fields)
18+
|> deep_struct_to_map()
19+
end
20+
21+
def deep_struct_to_map(map) when is_map(map) do
22+
Map.new(map, fn {k, v} -> {k, deep_struct_to_map(v)} end)
23+
end
24+
25+
def deep_struct_to_map(list) when is_list(list) do
26+
Enum.map(list, &deep_struct_to_map/1)
27+
end
28+
29+
def deep_struct_to_map(elem) do
30+
elem
31+
end
32+
33+
end

mix.exs

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ defmodule FactoryEx.MixProject do
2525
# Run "mix help deps" to learn about dependencies.
2626
defp deps do
2727
[
28-
# {:dep_from_hexpm, "~> 0.3.0"},
29-
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
28+
{:ecto, "~> 3.0", only: [:test, :dev], optional: true},
29+
{:ecto_sql, "~> 3.0", only: [:test, :dev], optional: true},
30+
{:postgrex, "~> 0.16", only: [:test, :dev], optional: true}
3031
]
3132
end
3233

mix.lock

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
%{
2+
"connection": {:hex, :connection, "1.1.0", "ff2a49c4b75b6fb3e674bfc5536451607270aac754ffd1bdfe175abe4a6d7a68", [:mix], [], "hexpm", "722c1eb0a418fbe91ba7bd59a47e28008a189d47e37e0e7bb85585a016b2869c"},
3+
"db_connection": {:hex, :db_connection, "2.4.2", "f92e79aff2375299a16bcb069a14ee8615c3414863a6fef93156aee8e86c2ff3", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4fe53ca91b99f55ea249693a0229356a08f4d1a7931d8ffa79289b145fe83668"},
4+
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
5+
"ecto": {:hex, :ecto, "3.8.1", "35e0bd8c8eb772e14a5191a538cd079706ecb45164ea08a7523b4fc69ab70f56", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f1b68f8d5fe3ab89e24f57c03db5b5d0aed3602077972098b3a6006a1be4b69b"},
6+
"ecto_sql": {:hex, :ecto_sql, "3.8.1", "1acaaba32ca0551fd19e492fc7c80414e72fc1a7140fc9395aaa53c2e8629798", [:mix], [{:db_connection, "~> 2.5 or ~> 2.4.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.8.1", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 0.16.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ba7fc75882edce6f2ceca047315d5db27ead773cafea47f1724e35f1e7964525"},
7+
"postgrex": {:hex, :postgrex, "0.16.3", "fac79a81a9a234b11c44235a4494d8565303fa4b9147acf57e48978a074971db", [:mix], [{:connection, "~> 1.1", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "aeaae1d2d1322da4e5fe90d241b0a564ce03a3add09d7270fb85362166194590"},
8+
"telemetry": {:hex, :telemetry, "1.1.0", "a589817034a27eab11144ad24d5c0f9fab1f58173274b1e9bae7074af9cbee51", [:rebar3], [], "hexpm", "b727b2a1f75614774cff2d7565b64d0dfa5bd52ba517f16543e6fc7efcc0df48"},
9+
}

test/factory_ex_test.exs

+48-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,53 @@ defmodule FactoryExTest do
22
use ExUnit.Case
33
doctest FactoryEx
44

5-
test "greets the world" do
6-
assert FactoryEx.hello() == :world
5+
defmodule MyRepo do
6+
@moduledoc false
7+
use Ecto.Repo,
8+
otp_app: :my_repo,
9+
adapter: Ecto.Adapters.Postgres
10+
end
11+
12+
defmodule MySchema do
13+
use Ecto.Schema
14+
import Ecto.Changeset
15+
16+
schema "my_schmeas" do
17+
field :foo, :integer
18+
field :bar, :integer
19+
end
20+
21+
@required_params [:foo, :bar]
22+
23+
def changeset(%__MODULE__{} = user, attrs \\ %{}) do
24+
user
25+
|> cast(attrs, @required_params)
26+
|> validate_required(@required_params)
27+
end
28+
end
29+
30+
defmodule TestFactory do
31+
@behaviour FactoryEx
32+
33+
def schema, do: MySchema
34+
35+
def repo, do: MyRepo
36+
37+
def build(params \\ %{}) do
38+
default = %{
39+
foo: 21,
40+
bar: 42
41+
}
42+
43+
Map.merge(default, params)
44+
end
45+
end
46+
47+
test "can generate a factory" do
48+
# assert %MySchema{foo: 21, bar: 42} = FactoryEx.insert!(TestFactory)
49+
# assert %MySchema{foo: 21, bar: 10} = FactoryEx.insert!(TestFactory, bar: 10)
50+
51+
assert %{foo: 21, bar: 42} = TestFactory.build()
52+
assert %{foo: 21, bar: 10} = TestFactory.build(%{bar: 10})
753
end
854
end

0 commit comments

Comments
 (0)