Skip to content

Commit

Permalink
Don't treat structs as maps in deep comparison/merging
Browse files Browse the repository at this point in the history
  • Loading branch information
fuelen committed Apr 21, 2024
1 parent 6e28494 commit 00577cc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
18 changes: 13 additions & 5 deletions lib/seed_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1000,9 +1000,14 @@ defmodule SeedFactory do

defp deep_merge_maps!(map1, map2, path) do
Map.merge(map1, map2, fn
key, v1, v2 when is_map(v1) and is_map(v2) -> deep_merge_maps!(v1, v2, path ++ [key])
_key, v, v -> v
key, v1, v2 -> throw({:conflict, v1, v2, path ++ [key]})
key, v1, v2 when is_map(v1) and is_map(v2) and not is_struct(v1) and not is_struct(v2) ->
deep_merge_maps!(v1, v2, path ++ [key])

_key, v, v ->
v

key, v1, v2 ->
throw({:conflict, v1, v2, path ++ [key]})
end)
end

Expand Down Expand Up @@ -1450,8 +1455,11 @@ defmodule SeedFactory do
# checks whether all values from map1 are present in map2.
defp deep_equal_maps?(map1, map2) do
Enum.all?(map1, fn
{key, value} when is_map(value) -> deep_equal_maps?(value, map2[key])
{key, value} -> map2[key] == value
{key, value} when is_map(value) and not is_struct(value) ->
deep_equal_maps?(value, map2[key])

{key, value} ->
map2[key] == value
end)
end

Expand Down
2 changes: 1 addition & 1 deletion test/seed_factory/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ defmodule SeedFactory.SchemaTest do
params: %{},
type: :entity,
value: nil,
with_traits: nil
with_traits: [:not_expired]
}
},
producing_instructions: [
Expand Down
10 changes: 10 additions & 0 deletions test/seed_factory_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,16 @@ defmodule SeedFactoryTest do
|> produce(:profile)
|> produce(profile: [:contacts_confirmed])
end

# comparison of structs doesn't fail with
# (Protocol.UndefinedError) protocol Enumerable not implemented for ~D[2024-04-28] of type Date (a struct)
assert_raise ArgumentError,
~r"Args to previously executed command :publish_project do not match",
fn ->
context
|> produce(project: [:expired])
|> produce(:virtual_file)
end
end

test "entity can be produced by non-default command using traits",
Expand Down
6 changes: 3 additions & 3 deletions test/support/schema_example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ defmodule SchemaExample do
command :create_virtual_file do
param :content, generate: &random_string/0
param :privacy, value: :private
param :project, entity: :project
param :project, entity: :project, with_traits: [:not_expired]
param :author, entity: :user, with_traits: [:active, :admin]

resolve(fn args ->
Expand Down Expand Up @@ -400,8 +400,8 @@ defmodule SchemaExample do
today = Date.utc_today()

%{
start_date: Date.add(today, -Enum.random(21..42)),
expiry_date: Date.add(today, -Enum.random(1..21))
start_date: Date.add(today, Enum.random(-42..-21)),
expiry_date: Date.add(today, Enum.random(-21..-1))
}
end)
end
Expand Down

0 comments on commit 00577cc

Please sign in to comment.