From f74ecb5643eed63b6beed83c3d9a944f752d288f Mon Sep 17 00:00:00 2001 From: Geoff Lane Date: Tue, 5 Jul 2016 11:17:05 -0400 Subject: [PATCH 1/3] Only drop autogenerated ids and not domain ids --- lib/ex_machina/ecto.ex | 11 ++++++++++- test/ex_machina/ecto_test.exs | 7 +++++++ test/support/models/custom.ex | 9 +++++++++ test/support/test_factory.ex | 6 ++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/support/models/custom.ex diff --git a/lib/ex_machina/ecto.ex b/lib/ex_machina/ecto.ex index 33bbc7b..b80cc80 100644 --- a/lib/ex_machina/ecto.ex +++ b/lib/ex_machina/ecto.ex @@ -121,9 +121,18 @@ defmodule ExMachina.Ecto do |> Map.from_struct |> Map.delete(:__meta__) |> Map.drop(struct.__schema__(:associations)) - |> Map.drop(struct.__schema__(:primary_key)) + |> drop_autogenerated_ids(struct) end defp drop_ecto_fields(record) do raise ArgumentError, "#{inspect record} is not an Ecto model. Use `build` instead." end + + # Don't want to drop non-autogenrated ids because those are meaningful to the + # domain in some way and will be expected to be set explicitly + defp drop_autogenerated_ids(map, struct) do + case struct.__schema__(:autogenerate_id) do + nil -> map + {name, _type} -> Map.delete(map, name) + end + end end diff --git a/test/ex_machina/ecto_test.exs b/test/ex_machina/ecto_test.exs index 1586e49..477c533 100644 --- a/test/ex_machina/ecto_test.exs +++ b/test/ex_machina/ecto_test.exs @@ -37,6 +37,13 @@ defmodule ExMachina.EctoTest do } end + test "params_for/2 leaves ids that are not auto-generated" do + assert TestFactory.params_for(:custom) == %{ + custom_id: 1, + name: "Testing" + } + end + test "params_for/2 raises when passed a map" do assert_raise ArgumentError, fn -> TestFactory.params_for(:user_map) diff --git a/test/support/models/custom.ex b/test/support/models/custom.ex new file mode 100644 index 0000000..ed8563e --- /dev/null +++ b/test/support/models/custom.ex @@ -0,0 +1,9 @@ +defmodule ExMachina.Custom do + use Ecto.Schema + + @primary_key {:custom_id, :integer, []} + schema "customs" do + field :name, :string + end +end + diff --git a/test/support/test_factory.ex b/test/support/test_factory.ex index c1b8fae..06a7e19 100644 --- a/test/support/test_factory.ex +++ b/test/support/test_factory.ex @@ -1,6 +1,12 @@ defmodule ExMachina.TestFactory do use ExMachina.Ecto, repo: ExMachina.TestRepo + def custom_factory do + %ExMachina.Custom{ + custom_id: 1, + name: "Testing" + } + end def user_factory do %ExMachina.User{ name: "John Doe", From 05a2c0738c6e9bf0c23dcf216b75cba455be6b65 Mon Sep 17 00:00:00 2001 From: Geoff Lane Date: Tue, 5 Jul 2016 11:27:19 -0400 Subject: [PATCH 2/3] Cleanups from feedback --- test/ex_machina/ecto_test.exs | 2 +- test/support/models/custom.ex | 2 +- test/support/test_factory.ex | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/ex_machina/ecto_test.exs b/test/ex_machina/ecto_test.exs index 477c533..4c3c647 100644 --- a/test/ex_machina/ecto_test.exs +++ b/test/ex_machina/ecto_test.exs @@ -39,7 +39,7 @@ defmodule ExMachina.EctoTest do test "params_for/2 leaves ids that are not auto-generated" do assert TestFactory.params_for(:custom) == %{ - custom_id: 1, + non_autogenerated_id: 1, name: "Testing" } end diff --git a/test/support/models/custom.ex b/test/support/models/custom.ex index ed8563e..0a987fb 100644 --- a/test/support/models/custom.ex +++ b/test/support/models/custom.ex @@ -1,7 +1,7 @@ defmodule ExMachina.Custom do use Ecto.Schema - @primary_key {:custom_id, :integer, []} + @primary_key {:non_autogenerated_id, :integer, []} schema "customs" do field :name, :string end diff --git a/test/support/test_factory.ex b/test/support/test_factory.ex index 06a7e19..8518908 100644 --- a/test/support/test_factory.ex +++ b/test/support/test_factory.ex @@ -3,10 +3,11 @@ defmodule ExMachina.TestFactory do def custom_factory do %ExMachina.Custom{ - custom_id: 1, + non_autogenerated_id: 1, name: "Testing" } end + def user_factory do %ExMachina.User{ name: "John Doe", From 278b5700dbffacbc184ad9cc8d59ff64eeaa4b56 Mon Sep 17 00:00:00 2001 From: Geoff Lane Date: Tue, 5 Jul 2016 11:28:48 -0400 Subject: [PATCH 3/3] Remove comment explained by test --- lib/ex_machina/ecto.ex | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/ex_machina/ecto.ex b/lib/ex_machina/ecto.ex index b80cc80..1969f22 100644 --- a/lib/ex_machina/ecto.ex +++ b/lib/ex_machina/ecto.ex @@ -127,8 +127,6 @@ defmodule ExMachina.Ecto do raise ArgumentError, "#{inspect record} is not an Ecto model. Use `build` instead." end - # Don't want to drop non-autogenrated ids because those are meaningful to the - # domain in some way and will be expected to be set explicitly defp drop_autogenerated_ids(map, struct) do case struct.__schema__(:autogenerate_id) do nil -> map