mirror of
https://github.com/ash-project/ash.git
synced 2024-09-19 13:03:02 +12:00
bug: Add failing test for lazy-reloaded calculations that aren't loading their dependencies (#1420)
This commit is contained in:
parent
b99ff3b3a0
commit
cc55670c7f
1 changed files with 43 additions and 0 deletions
|
@ -175,6 +175,8 @@ defmodule Ash.Test.Actions.LoadTest do
|
||||||
calculate :bio_union_calc, BioUnion, expr(bio_union) do
|
calculate :bio_union_calc, BioUnion, expr(bio_union) do
|
||||||
public?(true)
|
public?(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
calculate :campaign_upcase, :string, Ash.Test.Actions.LoadTest.UpcaseName
|
||||||
end
|
end
|
||||||
|
|
||||||
aggregates do
|
aggregates do
|
||||||
|
@ -202,6 +204,22 @@ defmodule Ash.Test.Actions.LoadTest do
|
||||||
end
|
end
|
||||||
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
|
defmodule PostsInSameCategory do
|
||||||
use Ash.Resource.ManualRelationship
|
use Ash.Resource.ManualRelationship
|
||||||
|
|
||||||
|
@ -753,6 +771,31 @@ defmodule Ash.Test.Actions.LoadTest do
|
||||||
assert author_after_load.name == "shouldn't change"
|
assert author_after_load.name == "shouldn't change"
|
||||||
end
|
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
|
test "nested lazy loads work" do
|
||||||
author =
|
author =
|
||||||
Author
|
Author
|
||||||
|
|
Loading…
Reference in a new issue