Skip to content

Commit

Permalink
Better error message for cases when entity was put manually to the co…
Browse files Browse the repository at this point in the history
…ntext
  • Loading branch information
fuelen committed Apr 24, 2024
1 parent d4b2174 commit c731fed
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
11 changes: 9 additions & 2 deletions lib/seed_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,22 @@ defmodule SeedFactory do
else
%{by_name: trait_by_name} = fetch_traits!(context, entity_name)

trail =
context.__seed_factory_meta__.trails[binding_name] ||
raise """
Can't find trail for #{inspect(binding_name)} entity.
Please don't put entities that can have traits manually in the context.
"""

currently_executed =
if current_trait_names == [] do
%{hd(fetch_command_names_by_entity_name!(context, entity_name)) => %{}}
%{trail.produced_by => %{}}
else
current_trait_names
|> select_traits_with_dependencies_by_names(
trait_by_name,
entity_name,
context.__seed_factory_meta__.trails[entity_name]
trail
)
|> ensure_no_restrictions!(restrictions, entity_name, :current)
|> executed_commands_from_traits()
Expand Down
2 changes: 1 addition & 1 deletion lib/seed_factory/command.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule SeedFactory.Command do
@moduledoc false
@derive {Inspect, only: []}
@derive {Inspect, only: [:name]}

defstruct [
:name,
Expand Down
3 changes: 2 additions & 1 deletion test/seed_factory/trail_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule SeedFactory.TrailTest do
|> SeedFactory.Trail.add_updated_by(:update_profile)
|> SeedFactory.Trail.add_updated_by(:suspend_user)

assert inspect(trail) == "#trail[:invite_user -> :accept_invitation -> :update_profile -> :suspend_user]"
assert inspect(trail) ==
"#trail[:invite_user -> :accept_invitation -> :update_profile -> :suspend_user]"
end
end
26 changes: 26 additions & 0 deletions test/seed_factory_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,32 @@ defmodule SeedFactoryTest do
assert diff == %{added: [:org], deleted: [], updated: []}
end

describe "manually put entities to context" do
test "entity that can have traits", context do
context = Map.put(context, :user, %SchemaExample.User{id: "user-id-1"})

assert_raise(
RuntimeError,
"""
Can't find trail for :user entity.
Please don't put entities that can have traits manually in the context.
""",
fn -> exec(context, :publish_project) end
)
end

test "entity that cannot have traits", context do
context = Map.put(context, :org, %SchemaExample.Org{id: "org-id-1"})

{_context, diff} =
with_diff(context, fn ->
exec(context, :create_office)
end)

assert diff == %{added: [:office], deleted: [], updated: []}
end
end

defp assert_trait(context, binding_name, expected_traits) when is_list(expected_traits) do
assert Map.has_key?(context, binding_name),
"No produced entity bound to #{inspect(binding_name)}"
Expand Down

0 comments on commit c731fed

Please sign in to comment.