mirror of
https://github.com/ash-project/ash_postgres.git
synced 2024-09-20 13:33:50 +12:00
Implement test and resources to show reference bug
This commit is contained in:
parent
48e8a414da
commit
4278f31736
5 changed files with 163 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
||||||
defmodule AshPostgres.Test.ComplexCalculationsTest do
|
defmodule AshPostgres.Test.ComplexCalculationsTest do
|
||||||
|
alias AshPostgres.Test.ComplexCalculations.Channel
|
||||||
use AshPostgres.RepoCase, async: false
|
use AshPostgres.RepoCase, async: false
|
||||||
|
|
||||||
test "complex calculation" do
|
test "complex calculation" do
|
||||||
|
@ -112,4 +113,76 @@ defmodule AshPostgres.Test.ComplexCalculationsTest do
|
||||||
|
|
||||||
assert channel.name == user_2.name
|
assert channel.name == user_2.name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "complex calculation while using actor on related resource passes reference" do
|
||||||
|
dm_channel =
|
||||||
|
AshPostgres.Test.ComplexCalculations.DMChannel
|
||||||
|
|> Ash.Changeset.new()
|
||||||
|
|> AshPostgres.Test.ComplexCalculations.Api.create!()
|
||||||
|
|
||||||
|
user_1 =
|
||||||
|
AshPostgres.Test.User
|
||||||
|
|> Ash.Changeset.for_create(:create, %{name: "User 1"})
|
||||||
|
|> AshPostgres.Test.Api.create!()
|
||||||
|
|
||||||
|
user_2 =
|
||||||
|
AshPostgres.Test.User
|
||||||
|
|> Ash.Changeset.for_create(:create, %{name: "User 2"})
|
||||||
|
|> AshPostgres.Test.Api.create!()
|
||||||
|
|
||||||
|
channel_member_1 =
|
||||||
|
AshPostgres.Test.ComplexCalculations.ChannelMember
|
||||||
|
|> Ash.Changeset.for_create(:create, %{channel_id: dm_channel.id, user_id: user_1.id})
|
||||||
|
|> AshPostgres.Test.ComplexCalculations.Api.create!()
|
||||||
|
|
||||||
|
channel_member_2 =
|
||||||
|
AshPostgres.Test.ComplexCalculations.ChannelMember
|
||||||
|
|> Ash.Changeset.new()
|
||||||
|
|> Ash.Changeset.manage_relationship(:channel, dm_as_channel, type: :append)
|
||||||
|
|> Ash.Changeset.manage_relationship(:user, user_2, type: :append)
|
||||||
|
|> AshPostgres.Test.ComplexCalculations.Api.create!()
|
||||||
|
|
||||||
|
dm_channel =
|
||||||
|
dm_channel
|
||||||
|
|> AshPostgres.Test.ComplexCalculations.Api.load!([
|
||||||
|
:first_member,
|
||||||
|
:second_member
|
||||||
|
])
|
||||||
|
|
||||||
|
assert dm_channel.first_member.id == channel_member_1.id
|
||||||
|
assert dm_channel.second_member.id == channel_member_2.id
|
||||||
|
|
||||||
|
dm_channel =
|
||||||
|
dm_channel
|
||||||
|
|> AshPostgres.Test.ComplexCalculations.Api.load!(:name, actor: user_1)
|
||||||
|
|
||||||
|
assert dm_channel.name == user_1.name
|
||||||
|
|
||||||
|
dm_channel =
|
||||||
|
dm_channel
|
||||||
|
|> AshPostgres.Test.ComplexCalculations.Api.load!(:name, actor: user_2)
|
||||||
|
|
||||||
|
assert dm_channel.name == user_2.name
|
||||||
|
|
||||||
|
channels =
|
||||||
|
AshPostgres.Test.ComplexCalculations.Channel
|
||||||
|
|> Ash.Query.for_read(:read)
|
||||||
|
|> AshPostgres.Test.ComplexCalculations.Api.read!()
|
||||||
|
|
||||||
|
channels =
|
||||||
|
channels
|
||||||
|
|> AshPostgres.Test.ComplexCalculations.Api.load!([dm_channel: :name],
|
||||||
|
actor: user_1
|
||||||
|
)
|
||||||
|
|
||||||
|
[channel | _] = channels
|
||||||
|
|
||||||
|
assert channel.dm_channel.name == user_1.name
|
||||||
|
|
||||||
|
channel =
|
||||||
|
channel
|
||||||
|
|> AshPostgres.Test.ComplexCalculations.Api.load(:dm_name, actor: user_1)
|
||||||
|
|
||||||
|
assert channel.dm_name == user_1.name
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,6 +7,7 @@ defmodule AshPostgres.Test.ComplexCalculations.Registry do
|
||||||
entry(AshPostgres.Test.ComplexCalculations.Skill)
|
entry(AshPostgres.Test.ComplexCalculations.Skill)
|
||||||
entry(AshPostgres.Test.ComplexCalculations.Documentation)
|
entry(AshPostgres.Test.ComplexCalculations.Documentation)
|
||||||
entry(AshPostgres.Test.ComplexCalculations.Channel)
|
entry(AshPostgres.Test.ComplexCalculations.Channel)
|
||||||
|
entry(AshPostgres.Test.ComplexCalculations.DMChannel)
|
||||||
entry(AshPostgres.Test.ComplexCalculations.ChannelMember)
|
entry(AshPostgres.Test.ComplexCalculations.ChannelMember)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -36,11 +36,17 @@ defmodule AshPostgres.Test.ComplexCalculations.Channel do
|
||||||
from_many?(true)
|
from_many?(true)
|
||||||
sort(created_at: :desc)
|
sort(created_at: :desc)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
has_one :dm_channel, AshPostgres.Test.ComplexCalculations.DMChannel do
|
||||||
|
api(AshPostgres.Test.ComplexCalculations.Api)
|
||||||
|
destination_attribute(:id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
aggregates do
|
aggregates do
|
||||||
first(:first_member_name, [:first_member, :user], :name)
|
first(:first_member_name, [:first_member, :user], :name)
|
||||||
first(:second_member_name, [:second_member, :user], :name)
|
first(:second_member_name, [:second_member, :user], :name)
|
||||||
|
first(:dm_channel_name, [:dm_channel], :name)
|
||||||
end
|
end
|
||||||
|
|
||||||
calculations do
|
calculations do
|
||||||
|
@ -60,5 +66,13 @@ defmodule AshPostgres.Test.ComplexCalculations.Channel do
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
calculate(:dm_name, :string, expr(dm_channel_name))
|
||||||
|
end
|
||||||
|
|
||||||
|
policies do
|
||||||
|
policy always() do
|
||||||
|
authorize_if(always())
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,7 +21,8 @@ defmodule AshPostgres.Test.ComplexCalculations.ChannelMember do
|
||||||
end
|
end
|
||||||
|
|
||||||
relationships do
|
relationships do
|
||||||
belongs_to(:user, AshPostgres.Test.User, api: AshPostgres.Test.Api)
|
belongs_to(:user, AshPostgres.Test.User, api: AshPostgres.Test.Api, attribute_writable?: true)
|
||||||
belongs_to(:channel, AshPostgres.Test.ComplexCalculations.Channel)
|
|
||||||
|
belongs_to(:channel, AshPostgres.Test.ComplexCalculations.Channel, attribute_writable?: true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
72
test/support/complex_calculations/resources/dm_channel.ex
Normal file
72
test/support/complex_calculations/resources/dm_channel.ex
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
defmodule AshPostgres.Test.ComplexCalculations.DMChannel do
|
||||||
|
@moduledoc false
|
||||||
|
use Ash.Resource,
|
||||||
|
data_layer: AshPostgres.DataLayer,
|
||||||
|
authorizers: [Ash.Policy.Authorizer]
|
||||||
|
|
||||||
|
require Ash.Expr
|
||||||
|
|
||||||
|
actions do
|
||||||
|
defaults([:create, :read, :update, :destroy])
|
||||||
|
end
|
||||||
|
|
||||||
|
attributes do
|
||||||
|
uuid_primary_key(:id)
|
||||||
|
|
||||||
|
create_timestamp(:created_at, private?: false)
|
||||||
|
update_timestamp(:updated_at, private?: false)
|
||||||
|
end
|
||||||
|
|
||||||
|
postgres do
|
||||||
|
table "complex_calculations_channels"
|
||||||
|
repo(AshPostgres.TestRepo)
|
||||||
|
end
|
||||||
|
|
||||||
|
relationships do
|
||||||
|
has_many :channel_members, AshPostgres.Test.ComplexCalculations.ChannelMember do
|
||||||
|
destination_attribute(:channel_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
has_one :first_member, AshPostgres.Test.ComplexCalculations.ChannelMember do
|
||||||
|
destination_attribute(:channel_id)
|
||||||
|
from_many?(true)
|
||||||
|
sort(created_at: :asc)
|
||||||
|
end
|
||||||
|
|
||||||
|
has_one :second_member, AshPostgres.Test.ComplexCalculations.ChannelMember do
|
||||||
|
destination_attribute(:channel_id)
|
||||||
|
from_many?(true)
|
||||||
|
sort(created_at: :desc)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
aggregates do
|
||||||
|
first(:first_member_name, [:first_member, :user], :name)
|
||||||
|
first(:second_member_name, [:second_member, :user], :name)
|
||||||
|
end
|
||||||
|
|
||||||
|
calculations do
|
||||||
|
calculate :name, :string do
|
||||||
|
calculation(
|
||||||
|
expr(
|
||||||
|
cond do
|
||||||
|
first_member.user_id == ^actor(:id) ->
|
||||||
|
first_member_name
|
||||||
|
|
||||||
|
second_member.user_id == ^actor(:id) ->
|
||||||
|
second_member_name
|
||||||
|
|
||||||
|
true ->
|
||||||
|
first_member_name <> ", " <> second_member_name
|
||||||
|
end
|
||||||
|
)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
policies do
|
||||||
|
policy always() do
|
||||||
|
authorize_if(always())
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue