bug: Add failing test for lazy-reloaded calculations that aren't loading their dependencies (#1420)

This commit is contained in:
Rebecca Le 2024-08-31 06:29:38 +08:00 committed by GitHub
parent b99ff3b3a0
commit cc55670c7f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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