Skip to content

Commit 0343088

Browse files
committed
Add more tests for belongs_to
1 parent b4b65f8 commit 0343088

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

test/ecto/repo/belongs_to_test.exs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ defmodule Ecto.Repo.BelongsToTest do
3131
schema "my_composite_assoc" do
3232
field :id_1, :id, primary_key: true
3333
field :id_2, :string, primary_key: true
34+
field :name, :string
3435
timestamps()
3536
end
3637
end
@@ -297,7 +298,23 @@ defmodule Ecto.Repo.BelongsToTest do
297298
assert assoc.updated_at
298299
end
299300

300-
test "inserting assocs on update preserving parent schema prefix" do
301+
test "inserting assocs with composite keys on update" do
302+
sample = %MyCompositeAssoc{id_1: 123, id_2: "xyz"}
303+
304+
changeset =
305+
%MySchema{id: 1}
306+
|> Ecto.Changeset.change
307+
|> Ecto.Changeset.put_assoc(:composite_assoc, sample)
308+
schema = TestRepo.update!(changeset)
309+
assoc = schema.composite_assoc
310+
assert assoc.id_1 == 123
311+
assert assoc.id_2 == "xyz"
312+
assert assoc.id_1 == schema.composite_id_1
313+
assert assoc.id_2 == schema.composite_id_2
314+
assert assoc.inserted_at
315+
end
316+
317+
test "inserting assocs on update preserving parent schema prefix" do
301318
sample = %MyAssoc{x: "xyz"}
302319

303320
changeset =
@@ -404,6 +421,26 @@ defmodule Ecto.Repo.BelongsToTest do
404421
refute_received {:delete, _} # Same assoc should not emit delete
405422
end
406423

424+
test "changing assocs with composite keys on update" do
425+
sample = %MyCompositeAssoc{id_1: 13, id_2: "xyz", name: "old name"}
426+
sample = put_meta sample, state: :loaded
427+
428+
# Changing the assoc
429+
sample_changeset = Ecto.Changeset.change(sample, name: "new name")
430+
changeset =
431+
%MySchema{id: 1, composite_assoc: sample}
432+
|> Ecto.Changeset.change
433+
|> Ecto.Changeset.put_assoc(:composite_assoc, sample_changeset)
434+
schema = TestRepo.update!(changeset)
435+
assoc = schema.composite_assoc
436+
assert assoc.id_1 == 13
437+
assert assoc.id_2 == "xyz"
438+
assert assoc.name == "new name"
439+
refute assoc.inserted_at
440+
assert assoc.updated_at
441+
refute_received {:delete, _} # Same assoc should not emit delete
442+
end
443+
407444
test "removing assocs on update raises if there is no id" do
408445
assoc = %MyAssoc{x: "xyz"} |> Ecto.put_meta(state: :loaded)
409446

0 commit comments

Comments
 (0)