Skip to content

Commit 1eaf462

Browse files
committed
Failing tests for changed?/2 behaviour on assocs
Setting assoc to nil actually errors as it is not handled in changed?/2, while settin existing assoc to belongs_to assoc returns changed?/2 as false.
1 parent 0746c94 commit 1eaf462

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

test/ecto/changeset/belongs_to_test.exs

+27
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,31 @@ defmodule Ecto.Changeset.BelongsToTest do
569569
changeset2 = %{changeset | action: :delete}
570570
assert Relation.apply_changes(embed, changeset2) == nil
571571
end
572+
573+
test "changed?/2 with assocs" do
574+
new_profile = %Profile{name: "profile"}
575+
576+
changeset =
577+
%Author{}
578+
|> Changeset.change()
579+
|> Changeset.put_assoc(:profile, new_profile)
580+
581+
assert Changeset.changed?(changeset, :profile)
582+
583+
{:ok, existing_profile} = TestRepo.insert(%Profile{name: "profile"})
584+
585+
changeset =
586+
%Author{}
587+
|> Changeset.change()
588+
|> Changeset.put_assoc(:profile, existing_profile)
589+
590+
assert Changeset.changed?(changeset, :profile)
591+
592+
changeset =
593+
%Author{profile: existing_profile}
594+
|> Changeset.change()
595+
|> Changeset.put_assoc(:profile, nil)
596+
597+
assert Changeset.changed?(changeset, :profile)
598+
end
572599
end

0 commit comments

Comments
 (0)