From cc55670c7f835f51fa860aac4aea8cfdfffc2f61 Mon Sep 17 00:00:00 2001 From: Rebecca Le <543859+sevenseacat@users.noreply.github.com> Date: Sat, 31 Aug 2024 06:29:38 +0800 Subject: [PATCH] bug: Add failing test for lazy-reloaded calculations that aren't loading their dependencies (#1420) --- test/actions/load_test.exs | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/actions/load_test.exs b/test/actions/load_test.exs index e4b68407..d78dabe0 100644 --- a/test/actions/load_test.exs +++ b/test/actions/load_test.exs @@ -175,6 +175,8 @@ defmodule Ash.Test.Actions.LoadTest do calculate :bio_union_calc, BioUnion, expr(bio_union) do public?(true) end + + calculate :campaign_upcase, :string, Ash.Test.Actions.LoadTest.UpcaseName end aggregates do @@ -202,6 +204,22 @@ defmodule Ash.Test.Actions.LoadTest do end end + defmodule UpcaseName do + use Ash.Resource.Calculation + + @impl true + def load(_, _, _), do: [campaign: :name] + + @impl true + def calculate(authors, _, _) do + Enum.map(authors, fn author -> + author.campaign.name + |> to_string() + |> String.upcase() + end) + end + end + defmodule PostsInSameCategory do use Ash.Resource.ManualRelationship @@ -753,6 +771,31 @@ defmodule Ash.Test.Actions.LoadTest do assert author_after_load.name == "shouldn't change" end + test "it allows lazy-reloading calculations that have dependencies" do + Campaign + |> Ash.Changeset.for_create(:create, %{name: "hello World"}) + |> Ash.create!() + + author = + Author + |> Ash.Changeset.for_create(:create, %{name: "zerg", campaign_name: "hello World"}) + |> Ash.create!() + + post = + Post + |> Ash.Changeset.for_create(:create, %{title: "post1"}) + |> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove) + |> Ash.create!() + + post = Ash.load!(post, author: :campaign_upcase) + author = post.author + + assert %{campaign_upcase: "HELLO WORLD"} = author + + assert %{campaign_upcase: "HELLO WORLD"} = + Ash.load!(author, [:campaign_upcase], lazy?: true) + end + test "nested lazy loads work" do author = Author