Skip to content

Commit aca48c1

Browse files
author
Leo B
committed
Add tests for updating a has_one/has_many
1 parent d2fdb20 commit aca48c1

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

lib/ecto/association.ex

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ defmodule Ecto.Association do
325325
end
326326

327327
defp where_expr(keys, values, binding) do
328-
# TODO make sure this has a test case
329328
or_exprs = fn
330329
false, r -> r
331330
l, r -> {:or, [], [l, r]}
@@ -373,8 +372,6 @@ defmodule Ecto.Association do
373372

374373
table_list = case refl do
375374
%{join_through: join_through, join_keys: join_keys, join_where: join_where, where: where} ->
376-
# [[{owner_join_key, owner_key}], [{related_join_key, related_key}]] = join_keys
377-
# TODO does this support more than one key on many to many?
378375
%{
379376
owner_keys: owner_keys,
380377
owner_join_keys: owner_join_keys,
@@ -880,7 +877,6 @@ defmodule Ecto.Association.Has do
880877
|> Ecto.Association.combine_joins_query(assoc.where, 1)
881878
end
882879

883-
# TODO add a test case for composite keys here
884880
@impl true
885881
def assoc_query(%{related_key: related_key, queryable: queryable} = assoc, query, values) do
886882
from(x in (query || queryable))
@@ -1132,7 +1128,6 @@ defmodule Ecto.Association.BelongsTo do
11321128
{:error, "associated module #{inspect queryable} is not an Ecto schema"}
11331129
[] != (missing_fields = Ecto.Association.missing_fields(queryable, related_key)) ->
11341130
{:error, "associated schema #{inspect queryable} does not have field(s) `#{inspect missing_fields}`"}
1135-
# TODO how can this be triggered in tests?
11361131
[] != (missing_pks = Ecto.Association.missing_primary_keys(queryable, related_key)) ->
11371132
{:error, "associated schema #{inspect queryable} has primary keys #{inspect missing_pks} not included in association"}
11381133
true ->
@@ -1142,7 +1137,6 @@ defmodule Ecto.Association.BelongsTo do
11421137

11431138
@impl true
11441139
def struct(module, name, opts) do
1145-
# TODO his should ideally not be hard coded to `:id` but set to use whatever primary key `related` defines
11461140
ref = if ref = opts[:references], do: ref, else: :id
11471141
queryable = Keyword.fetch!(opts, :queryable)
11481142
related = Ecto.Association.related_from_query(queryable, name)
@@ -1307,7 +1301,6 @@ defmodule Ecto.Association.ManyToMany do
13071301
related = Ecto.Association.related_from_query(queryable, name)
13081302

13091303
join_keys = opts[:join_keys]
1310-
validate_join_keys!(name, join_keys)
13111304
join_through = opts[:join_through]
13121305
validate_join_through!(name, join_through)
13131306

@@ -1448,7 +1441,6 @@ defmodule Ecto.Association.ManyToMany do
14481441
def preload_info(%{join_keys: [join_through_keys, _], owner: owner} = refl) do
14491442
join_owner_keys = Keyword.keys(join_through_keys)
14501443
owner_key_types = join_through_keys |> Keyword.values |> Enum.map(& owner.__schema__(:type, &1))
1451-
# owner_key_type = owner.__schema__(:type, owner_key)
14521444

14531445
# When preloading use the last bound table (which is the join table) and the join_owner_key
14541446
# to filter out related entities to the owner structs we're preloading with.
@@ -1493,7 +1485,6 @@ defmodule Ecto.Association.ManyToMany do
14931485
[join_through_keys, join_related_keys] = join_keys
14941486
owner_keys = Keyword.values(join_through_keys)
14951487
related_keys = Keyword.values(join_related_keys)
1496-
# [[{join_owner_key, owner_key}], [{join_related_key, related_key}]] = join_keys
14971488

14981489
if insert_join?(parent_changeset, changeset, field, related_keys) do
14991490
owner_values = dump! :insert, join_through, owner, owner_keys, adapter
@@ -1519,10 +1510,6 @@ defmodule Ecto.Association.ManyToMany do
15191510
end
15201511
end
15211512

1522-
defp validate_join_keys!(_name, _join_keys) do
1523-
# TODO
1524-
:ok
1525-
end
15261513
defp validate_join_through!(name, nil) do
15271514
raise ArgumentError, "many_to_many #{inspect name} associations require the :join_through option to be given"
15281515
end

test/ecto/repo/has_assoc_test.exs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,5 +622,31 @@ defmodule Ecto.Repo.HasAssocTest do
622622
assert assoc.inserted_at
623623
end
624624

625-
# TODO add tests for update
625+
test "handles assocs with composite keys on update" do
626+
sample = %MyCompositeAssoc{name: "xyz"}
627+
628+
changeset =
629+
%MyCompositeSchema{x: 3, y: "a"}
630+
|> Ecto.Changeset.change
631+
|> Ecto.Changeset.put_assoc(:assoc, sample)
632+
schema = TestRepo.update!(changeset)
633+
assoc = schema.assoc
634+
assert assoc.id
635+
assert assoc.name == "xyz"
636+
assert assoc.composite_x == schema.x
637+
assert assoc.composite_y == schema.y
638+
assert assoc.inserted_at
639+
640+
changeset =
641+
%MyCompositeSchema{x: 4, y: "b"}
642+
|> Ecto.Changeset.change
643+
|> Ecto.Changeset.put_assoc(:assocs, [sample])
644+
schema = TestRepo.update!(changeset)
645+
[assoc] = schema.assocs
646+
assert assoc.id
647+
assert assoc.name == "xyz"
648+
assert assoc.composite_x == schema.x
649+
assert assoc.composite_y == schema.y
650+
assert assoc.inserted_at
651+
end
626652
end

0 commit comments

Comments
 (0)