Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: prove that calculations can return a list of embedded resources #1548

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions test/calculations/calculation_with_embedded_resource_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
defmodule Ash.Test.Calculations.CalculationWithEmbeddedResourceTest do
@moduledoc false
use ExUnit.Case, async: true

alias Ash.Changeset

defmodule Profile do
use Ash.Resource, data_layer: :embedded

attributes do
uuid_primary_key :id do
generated? false
public? true
writable? true
end

attribute :platform, :string do
public?(true)
end
end
end

defmodule Calculation.Profile do
use Ash.Resource.Calculation

def calculate(records, _opts, _context) do
records |> Enum.map(&get_profile/1)
end

def get_profile(author) do
Changeset.for_create(
Profile,
:create,
%{id: author.id, platform: "Some platform"}
)
|> Ash.create!()
|> List.wrap()
end
end

defmodule Author do
use Ash.Resource,
domain: Ash.Test.Calculations.CalculationWithEmbeddedResourceTest.Domain,
data_layer: Ash.DataLayer.Ets

ets do
private?(true)
end

actions do
default_accept :*
defaults [:create, :read]
end

attributes do
uuid_primary_key :id, writable?: true
end

calculations do
calculate :profiles,
{:array, Profile},
Calculation.Profile do
public? true
end
end
end

defmodule Domain do
@moduledoc false
use Ash.Domain

resources do
resource Author
end
end

test "embedded resources can be returned by calculations" do
assert %Author{profiles: profiles} =
Changeset.for_create(Author, :create, %{}) |> Ash.create!() |> Ash.load!(:profiles)

assert [%{platform: "Some platform"}] = profiles
end
end
Loading