Skip to content

Commit

Permalink
chore: add failing test for calculations on private relationships (#1008
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rbino authored Apr 10, 2024
1 parent 6ba0f50 commit cd4299d
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions test/calculations/calculation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,86 @@ defmodule Ash.Test.CalculationTest do
end
end

defmodule UserFirstNameForWarranty do
use Ash.Resource.Calculation

@impl true
def load(_, _, _) do
[product: [user: [:first_name]]]
end

@impl true
def calculate(records, _, _) do
Enum.map(records, & &1.product.user.first_name)
end
end

defmodule Product do
use Ash.Resource,
domain: Domain,
data_layer: Ash.DataLayer.Ets

actions do
default_accept :*
defaults([:read, :destroy, update: :*])

create :create do
primary? true

accept [:name, :user_id]
end
end

ets do
private?(true)
end

attributes do
uuid_primary_key(:id)
attribute :name, :string
end

relationships do
belongs_to :user, User
end
end

defmodule Warranty do
use Ash.Resource,
domain: Domain,
data_layer: Ash.DataLayer.Ets

actions do
default_accept :*
defaults([:read, :destroy, update: :*])

create :create do
primary? true

accept [:expiry, :product_id]
end
end

ets do
private?(true)
end

attributes do
uuid_primary_key(:id)
attribute :expiry, :datetime
end

calculations do
calculate :user_first_name, :string, UserFirstNameForWarranty do
public?(true)
end
end

relationships do
belongs_to :product, Product
end
end

setup do
user1 =
User
Expand Down Expand Up @@ -1318,4 +1398,18 @@ defmodule Ash.Test.CalculationTest do
|> Ash.Query.filter(full_name(separator: first_name) == "zachzachdaniel")
|> Ash.read!()
end

test "loads private relationships", %{user1: user1} do

Check failure on line 1402 in test/calculations/calculation_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (Picosat) / mix test

test loads private relationships (Ash.Test.CalculationTest)

Check failure on line 1402 in test/calculations/calculation_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (SimpleSat) / mix test

test loads private relationships (Ash.Test.CalculationTest)
product =
Product
|> Ash.Changeset.for_create(:create, %{name: "SuperFoo3000", user_id: user1.id})
|> Ash.create!()

warranty =
Warranty
|> Ash.Changeset.for_create(:create, %{product_id: product.id})
|> Ash.create!()

assert %{user_first_name: "zach"} = Ash.load!(warranty, :user_first_name)
end
end

0 comments on commit cd4299d

Please sign in to comment.