ash_postgres/test/complex_calculations_test.exs

280 lines
8.8 KiB
Elixir
Raw Permalink Normal View History

test: add failing test to demonstrate potential bug (#164) 1) test complex calculation (AshPostgres.Test.ComplexCalculationsTest) test/complex_calculations_test.exs:5 ** (RuntimeError) Error while building reference: latest_documentation_status code: |> AshPostgres.Test.ComplexCalculations.Api.load!([ stacktrace: (ash_postgres 1.3.41) lib/expr.ex:846: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:109: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:356: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:968: anonymous fn/6 in AshPostgres.Expr.do_dynamic_expr/5 (ecto 3.10.3) lib/ecto/query/builder/dynamic.ex:76: Ecto.Query.Builder.Dynamic.expand/3 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (stdlib 5.0.2) lists.erl:1707: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (stdlib 5.0.2) lists.erl:1707: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (ecto 3.10.3) lib/ecto/query/builder/dynamic.ex:59: Ecto.Query.Builder.Dynamic.partially_expand/6 (ecto 3.10.3) lib/ecto/query/builder/select.ex:235: Ecto.Query.Builder.Select.expand_nested/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:274: Ecto.Query.Builder.Select.expand_nested_pair/3 (elixir 1.15.4) lib/enum.ex:1825: anonymous fn/3 in Enum.map_reduce/3 (stdlib 5.0.2) maps.erl:416: :maps.fold_1/4 (elixir 1.15.4) lib/enum.ex:2522: Enum.map_reduce/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:257: Ecto.Query.Builder.Select.expand_nested/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:205: Ecto.Query.Builder.Select.select!/5 (elixir 1.15.4) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3 (ash_postgres 1.3.41) lib/aggregate.ex:121: anonymous fn/6 in AshPostgres.Aggregate.add_aggregates/6 (elixir 1.15.4) lib/enum.ex:4830: Enumerable.List.reduce/3 (elixir 1.15.4) lib/enum.ex:2564: Enum.reduce_while/3 (ash_postgres 1.3.41) lib/aggregate.ex:53: AshPostgres.Aggregate.add_aggregates/6 (ash 2.13.3) lib/ash/query/aggregate.ex:570: anonymous fn/6 in Ash.Query.Aggregate.value_request/9 (ash 2.13.3) lib/ash/engine/engine.ex:537: anonymous fn/2 in Ash.Engine.run_iteration/1 (ash 2.13.3) lib/ash/engine/engine.ex:558: anonymous fn/4 in Ash.Engine.async/2 (elixir 1.15.4) lib/task/supervised.ex:101: Task.Supervised.invoke_mfa/2 (elixir 1.15.4) lib/task/supervised.ex:36: Task.Supervised.reply/4 (ash 2.13.3) lib/ash/engine/engine.ex:552: Ash.Engine.async/2 (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2 (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2 (ash 2.13.3) lib/ash/engine/engine.ex:702: Ash.Engine.start_pending_tasks/1 (ash 2.13.3) lib/ash/engine/engine.ex:323: Ash.Engine.run_to_completion/1 (ash 2.13.3) lib/ash/engine/engine.ex:252: Ash.Engine.do_run/2 (ash 2.13.3) lib/ash/engine/engine.ex:148: Ash.Engine.run/2 (ash 2.13.3) lib/ash/actions/read.ex:173: Ash.Actions.Read.do_run/3 (ash 2.13.3) lib/ash/actions/read.ex:96: Ash.Actions.Read.run/3 (ash 2.13.3) lib/ash/api/api.ex:1733: Ash.Api.load/4 (ash 2.13.3) lib/ash/api/api.ex:1707: Ash.Api.load!/4 test/complex_calculations_test.exs:55: (test)
2023-08-17 12:49:18 +12:00
defmodule AshPostgres.Test.ComplexCalculationsTest do
use AshPostgres.RepoCase, async: false
require Ash.Query
test: add failing test to demonstrate potential bug (#164) 1) test complex calculation (AshPostgres.Test.ComplexCalculationsTest) test/complex_calculations_test.exs:5 ** (RuntimeError) Error while building reference: latest_documentation_status code: |> AshPostgres.Test.ComplexCalculations.Api.load!([ stacktrace: (ash_postgres 1.3.41) lib/expr.ex:846: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:109: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:356: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:968: anonymous fn/6 in AshPostgres.Expr.do_dynamic_expr/5 (ecto 3.10.3) lib/ecto/query/builder/dynamic.ex:76: Ecto.Query.Builder.Dynamic.expand/3 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (stdlib 5.0.2) lists.erl:1707: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (stdlib 5.0.2) lists.erl:1707: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (ecto 3.10.3) lib/ecto/query/builder/dynamic.ex:59: Ecto.Query.Builder.Dynamic.partially_expand/6 (ecto 3.10.3) lib/ecto/query/builder/select.ex:235: Ecto.Query.Builder.Select.expand_nested/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:274: Ecto.Query.Builder.Select.expand_nested_pair/3 (elixir 1.15.4) lib/enum.ex:1825: anonymous fn/3 in Enum.map_reduce/3 (stdlib 5.0.2) maps.erl:416: :maps.fold_1/4 (elixir 1.15.4) lib/enum.ex:2522: Enum.map_reduce/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:257: Ecto.Query.Builder.Select.expand_nested/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:205: Ecto.Query.Builder.Select.select!/5 (elixir 1.15.4) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3 (ash_postgres 1.3.41) lib/aggregate.ex:121: anonymous fn/6 in AshPostgres.Aggregate.add_aggregates/6 (elixir 1.15.4) lib/enum.ex:4830: Enumerable.List.reduce/3 (elixir 1.15.4) lib/enum.ex:2564: Enum.reduce_while/3 (ash_postgres 1.3.41) lib/aggregate.ex:53: AshPostgres.Aggregate.add_aggregates/6 (ash 2.13.3) lib/ash/query/aggregate.ex:570: anonymous fn/6 in Ash.Query.Aggregate.value_request/9 (ash 2.13.3) lib/ash/engine/engine.ex:537: anonymous fn/2 in Ash.Engine.run_iteration/1 (ash 2.13.3) lib/ash/engine/engine.ex:558: anonymous fn/4 in Ash.Engine.async/2 (elixir 1.15.4) lib/task/supervised.ex:101: Task.Supervised.invoke_mfa/2 (elixir 1.15.4) lib/task/supervised.ex:36: Task.Supervised.reply/4 (ash 2.13.3) lib/ash/engine/engine.ex:552: Ash.Engine.async/2 (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2 (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2 (ash 2.13.3) lib/ash/engine/engine.ex:702: Ash.Engine.start_pending_tasks/1 (ash 2.13.3) lib/ash/engine/engine.ex:323: Ash.Engine.run_to_completion/1 (ash 2.13.3) lib/ash/engine/engine.ex:252: Ash.Engine.do_run/2 (ash 2.13.3) lib/ash/engine/engine.ex:148: Ash.Engine.run/2 (ash 2.13.3) lib/ash/actions/read.ex:173: Ash.Actions.Read.do_run/3 (ash 2.13.3) lib/ash/actions/read.ex:96: Ash.Actions.Read.run/3 (ash 2.13.3) lib/ash/api/api.ex:1733: Ash.Api.load/4 (ash 2.13.3) lib/ash/api/api.ex:1707: Ash.Api.load!/4 test/complex_calculations_test.exs:55: (test)
2023-08-17 12:49:18 +12:00
test "complex calculation" do
certification =
AshPostgres.Test.ComplexCalculations.Certification
|> Ash.Changeset.new()
|> AshPostgres.Test.ComplexCalculations.Api.create!()
skill =
AshPostgres.Test.ComplexCalculations.Skill
|> Ash.Changeset.new()
|> Ash.Changeset.manage_relationship(:certification, certification, type: :append)
|> AshPostgres.Test.ComplexCalculations.Api.create!()
_documentation =
AshPostgres.Test.ComplexCalculations.Documentation
|> Ash.Changeset.new(%{status: :demonstrated})
|> Ash.Changeset.manage_relationship(:skill, skill, type: :append)
|> AshPostgres.Test.ComplexCalculations.Api.create!()
skill =
skill
|> AshPostgres.Test.ComplexCalculations.Api.load!([:latest_documentation_status])
assert skill.latest_documentation_status == :demonstrated
certification =
certification
|> AshPostgres.Test.ComplexCalculations.Api.load!([
:count_of_skills
])
assert certification.count_of_skills == 1
certification =
certification
|> AshPostgres.Test.ComplexCalculations.Api.load!([
:count_of_approved_skills
])
assert certification.count_of_approved_skills == 0
certification =
certification
|> AshPostgres.Test.ComplexCalculations.Api.load!([
:count_of_documented_skills
])
assert certification.count_of_documented_skills == 1
certification =
certification
|> AshPostgres.Test.ComplexCalculations.Api.load!([
:count_of_documented_skills,
:all_documentation_approved,
test: add failing test to demonstrate potential bug (#164) 1) test complex calculation (AshPostgres.Test.ComplexCalculationsTest) test/complex_calculations_test.exs:5 ** (RuntimeError) Error while building reference: latest_documentation_status code: |> AshPostgres.Test.ComplexCalculations.Api.load!([ stacktrace: (ash_postgres 1.3.41) lib/expr.ex:846: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:109: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:356: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:968: anonymous fn/6 in AshPostgres.Expr.do_dynamic_expr/5 (ecto 3.10.3) lib/ecto/query/builder/dynamic.ex:76: Ecto.Query.Builder.Dynamic.expand/3 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (stdlib 5.0.2) lists.erl:1707: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (stdlib 5.0.2) lists.erl:1707: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (ecto 3.10.3) lib/ecto/query/builder/dynamic.ex:59: Ecto.Query.Builder.Dynamic.partially_expand/6 (ecto 3.10.3) lib/ecto/query/builder/select.ex:235: Ecto.Query.Builder.Select.expand_nested/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:274: Ecto.Query.Builder.Select.expand_nested_pair/3 (elixir 1.15.4) lib/enum.ex:1825: anonymous fn/3 in Enum.map_reduce/3 (stdlib 5.0.2) maps.erl:416: :maps.fold_1/4 (elixir 1.15.4) lib/enum.ex:2522: Enum.map_reduce/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:257: Ecto.Query.Builder.Select.expand_nested/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:205: Ecto.Query.Builder.Select.select!/5 (elixir 1.15.4) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3 (ash_postgres 1.3.41) lib/aggregate.ex:121: anonymous fn/6 in AshPostgres.Aggregate.add_aggregates/6 (elixir 1.15.4) lib/enum.ex:4830: Enumerable.List.reduce/3 (elixir 1.15.4) lib/enum.ex:2564: Enum.reduce_while/3 (ash_postgres 1.3.41) lib/aggregate.ex:53: AshPostgres.Aggregate.add_aggregates/6 (ash 2.13.3) lib/ash/query/aggregate.ex:570: anonymous fn/6 in Ash.Query.Aggregate.value_request/9 (ash 2.13.3) lib/ash/engine/engine.ex:537: anonymous fn/2 in Ash.Engine.run_iteration/1 (ash 2.13.3) lib/ash/engine/engine.ex:558: anonymous fn/4 in Ash.Engine.async/2 (elixir 1.15.4) lib/task/supervised.ex:101: Task.Supervised.invoke_mfa/2 (elixir 1.15.4) lib/task/supervised.ex:36: Task.Supervised.reply/4 (ash 2.13.3) lib/ash/engine/engine.ex:552: Ash.Engine.async/2 (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2 (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2 (ash 2.13.3) lib/ash/engine/engine.ex:702: Ash.Engine.start_pending_tasks/1 (ash 2.13.3) lib/ash/engine/engine.ex:323: Ash.Engine.run_to_completion/1 (ash 2.13.3) lib/ash/engine/engine.ex:252: Ash.Engine.do_run/2 (ash 2.13.3) lib/ash/engine/engine.ex:148: Ash.Engine.run/2 (ash 2.13.3) lib/ash/actions/read.ex:173: Ash.Actions.Read.do_run/3 (ash 2.13.3) lib/ash/actions/read.ex:96: Ash.Actions.Read.run/3 (ash 2.13.3) lib/ash/api/api.ex:1733: Ash.Api.load/4 (ash 2.13.3) lib/ash/api/api.ex:1707: Ash.Api.load!/4 test/complex_calculations_test.exs:55: (test)
2023-08-17 12:49:18 +12:00
:some_documentation_created
])
assert certification.some_documentation_created
test: add failing test to demonstrate potential bug (#164) 1) test complex calculation (AshPostgres.Test.ComplexCalculationsTest) test/complex_calculations_test.exs:5 ** (RuntimeError) Error while building reference: latest_documentation_status code: |> AshPostgres.Test.ComplexCalculations.Api.load!([ stacktrace: (ash_postgres 1.3.41) lib/expr.ex:846: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:109: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:356: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:968: anonymous fn/6 in AshPostgres.Expr.do_dynamic_expr/5 (ecto 3.10.3) lib/ecto/query/builder/dynamic.ex:76: Ecto.Query.Builder.Dynamic.expand/3 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (stdlib 5.0.2) lists.erl:1707: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (stdlib 5.0.2) lists.erl:1707: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (ecto 3.10.3) lib/ecto/query/builder/dynamic.ex:59: Ecto.Query.Builder.Dynamic.partially_expand/6 (ecto 3.10.3) lib/ecto/query/builder/select.ex:235: Ecto.Query.Builder.Select.expand_nested/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:274: Ecto.Query.Builder.Select.expand_nested_pair/3 (elixir 1.15.4) lib/enum.ex:1825: anonymous fn/3 in Enum.map_reduce/3 (stdlib 5.0.2) maps.erl:416: :maps.fold_1/4 (elixir 1.15.4) lib/enum.ex:2522: Enum.map_reduce/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:257: Ecto.Query.Builder.Select.expand_nested/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:205: Ecto.Query.Builder.Select.select!/5 (elixir 1.15.4) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3 (ash_postgres 1.3.41) lib/aggregate.ex:121: anonymous fn/6 in AshPostgres.Aggregate.add_aggregates/6 (elixir 1.15.4) lib/enum.ex:4830: Enumerable.List.reduce/3 (elixir 1.15.4) lib/enum.ex:2564: Enum.reduce_while/3 (ash_postgres 1.3.41) lib/aggregate.ex:53: AshPostgres.Aggregate.add_aggregates/6 (ash 2.13.3) lib/ash/query/aggregate.ex:570: anonymous fn/6 in Ash.Query.Aggregate.value_request/9 (ash 2.13.3) lib/ash/engine/engine.ex:537: anonymous fn/2 in Ash.Engine.run_iteration/1 (ash 2.13.3) lib/ash/engine/engine.ex:558: anonymous fn/4 in Ash.Engine.async/2 (elixir 1.15.4) lib/task/supervised.ex:101: Task.Supervised.invoke_mfa/2 (elixir 1.15.4) lib/task/supervised.ex:36: Task.Supervised.reply/4 (ash 2.13.3) lib/ash/engine/engine.ex:552: Ash.Engine.async/2 (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2 (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2 (ash 2.13.3) lib/ash/engine/engine.ex:702: Ash.Engine.start_pending_tasks/1 (ash 2.13.3) lib/ash/engine/engine.ex:323: Ash.Engine.run_to_completion/1 (ash 2.13.3) lib/ash/engine/engine.ex:252: Ash.Engine.do_run/2 (ash 2.13.3) lib/ash/engine/engine.ex:148: Ash.Engine.run/2 (ash 2.13.3) lib/ash/actions/read.ex:173: Ash.Actions.Read.do_run/3 (ash 2.13.3) lib/ash/actions/read.ex:96: Ash.Actions.Read.run/3 (ash 2.13.3) lib/ash/api/api.ex:1733: Ash.Api.load/4 (ash 2.13.3) lib/ash/api/api.ex:1707: Ash.Api.load!/4 test/complex_calculations_test.exs:55: (test)
2023-08-17 12:49:18 +12:00
end
test "channel: first_member and second member" do
channel =
AshPostgres.Test.ComplexCalculations.Channel
|> 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.new()
|> Ash.Changeset.manage_relationship(:channel, channel, type: :append)
|> Ash.Changeset.manage_relationship(:user, user_1, type: :append)
|> AshPostgres.Test.ComplexCalculations.Api.create!()
channel_member_2 =
AshPostgres.Test.ComplexCalculations.ChannelMember
|> Ash.Changeset.new()
|> Ash.Changeset.manage_relationship(:channel, channel, type: :append)
|> Ash.Changeset.manage_relationship(:user, user_2, type: :append)
|> AshPostgres.Test.ComplexCalculations.Api.create!()
channel =
channel
|> AshPostgres.Test.ComplexCalculations.Api.load!([
:first_member,
:second_member
])
assert channel.first_member.id == channel_member_1.id
assert channel.second_member.id == channel_member_2.id
channel =
channel
|> AshPostgres.Test.ComplexCalculations.Api.load!(:name, actor: user_1)
assert channel.name == user_1.name
channel =
channel
|> AshPostgres.Test.ComplexCalculations.Api.load!(:name, actor: user_2)
assert channel.name == user_2.name
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.for_create(:create, %{channel_id: dm_channel.id, user_id: user_2.id})
|> 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, :foo], actor: user_2)
assert channel.dm_name == user_2.name
end
test "calculations with parent filters can be filtered on themselves" do
AshPostgres.Test.ComplexCalculations.DMChannel
|> Ash.Changeset.new()
|> AshPostgres.Test.ComplexCalculations.Api.create!()
assert [%{foo: "foobar"}] =
AshPostgres.Test.ComplexCalculations.Channel
|> Ash.Query.filter(foo == "foobar")
|> AshPostgres.Test.ComplexCalculations.Api.read!(load: :foo)
end
test "calculations with aggregates can be referenced from aggregates" do
author =
AshPostgres.Test.Author
|> Ash.Changeset.new(%{first_name: "is", last_name: "match"})
|> AshPostgres.Test.Api.create!()
AshPostgres.Test.Post
|> Ash.Changeset.new(%{title: "match"})
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|> AshPostgres.Test.Api.create!()
assert [%{author_count_of_posts: 1}] =
AshPostgres.Test.Post
|> Ash.Query.load(:author_count_of_posts)
|> AshPostgres.Test.Api.read!()
assert [%{author_count_of_posts: 1}] =
AshPostgres.Test.Post
|> AshPostgres.Test.Api.read!()
|> AshPostgres.Test.Api.load!(:author_count_of_posts)
assert [_] =
AshPostgres.Test.Post
|> Ash.Query.filter(author_count_of_posts == 1)
|> AshPostgres.Test.Api.read!()
end
test "calculations can reference aggregates from optimizable first aggregates" do
author =
AshPostgres.Test.Author
|> Ash.Changeset.new(%{first_name: "is", last_name: "match"})
|> AshPostgres.Test.Api.create!()
AshPostgres.Test.Post
|> Ash.Changeset.new(%{title: "match"})
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|> AshPostgres.Test.Api.create!()
assert [%{author_count_of_posts_agg: 1}] =
AshPostgres.Test.Post
|> Ash.Query.load(:author_count_of_posts_agg)
|> AshPostgres.Test.Api.read!()
assert [%{author_count_of_posts_agg: 1}] =
AshPostgres.Test.Post
|> AshPostgres.Test.Api.read!()
|> AshPostgres.Test.Api.load!(:author_count_of_posts_agg)
assert [_] =
AshPostgres.Test.Post
|> Ash.Query.filter(author_count_of_posts_agg == 1)
|> AshPostgres.Test.Api.read!()
end
test "calculations can reference aggregates from non optimizable aggregates" do
author =
AshPostgres.Test.Author
|> Ash.Changeset.new(%{first_name: "is", last_name: "match"})
|> AshPostgres.Test.Api.create!()
AshPostgres.Test.Post
|> Ash.Changeset.new(%{title: "match"})
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|> AshPostgres.Test.Api.create!()
assert [%{sum_of_author_count_of_posts: 1}] =
AshPostgres.Test.Post
|> Ash.Query.load(:sum_of_author_count_of_posts)
|> AshPostgres.Test.Api.read!()
assert [%{sum_of_author_count_of_posts: 1}] =
AshPostgres.Test.Post
|> AshPostgres.Test.Api.read!()
|> AshPostgres.Test.Api.load!(:sum_of_author_count_of_posts)
assert [_] =
AshPostgres.Test.Post
|> Ash.Query.filter(sum_of_author_count_of_posts == 1)
|> AshPostgres.Test.Api.read!()
end
test: add failing test to demonstrate potential bug (#164) 1) test complex calculation (AshPostgres.Test.ComplexCalculationsTest) test/complex_calculations_test.exs:5 ** (RuntimeError) Error while building reference: latest_documentation_status code: |> AshPostgres.Test.ComplexCalculations.Api.load!([ stacktrace: (ash_postgres 1.3.41) lib/expr.ex:846: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:109: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:356: AshPostgres.Expr.do_dynamic_expr/5 (ash_postgres 1.3.41) lib/expr.ex:968: anonymous fn/6 in AshPostgres.Expr.do_dynamic_expr/5 (ecto 3.10.3) lib/ecto/query/builder/dynamic.ex:76: Ecto.Query.Builder.Dynamic.expand/3 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (stdlib 5.0.2) lists.erl:1707: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (stdlib 5.0.2) lists.erl:1707: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3 (elixir 1.15.4) lib/macro.ex:653: Macro.do_traverse/4 (ecto 3.10.3) lib/ecto/query/builder/dynamic.ex:59: Ecto.Query.Builder.Dynamic.partially_expand/6 (ecto 3.10.3) lib/ecto/query/builder/select.ex:235: Ecto.Query.Builder.Select.expand_nested/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:274: Ecto.Query.Builder.Select.expand_nested_pair/3 (elixir 1.15.4) lib/enum.ex:1825: anonymous fn/3 in Enum.map_reduce/3 (stdlib 5.0.2) maps.erl:416: :maps.fold_1/4 (elixir 1.15.4) lib/enum.ex:2522: Enum.map_reduce/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:257: Ecto.Query.Builder.Select.expand_nested/3 (ecto 3.10.3) lib/ecto/query/builder/select.ex:205: Ecto.Query.Builder.Select.select!/5 (elixir 1.15.4) lib/enum.ex:2510: Enum."-reduce/3-lists^foldl/2-0-"/3 (ash_postgres 1.3.41) lib/aggregate.ex:121: anonymous fn/6 in AshPostgres.Aggregate.add_aggregates/6 (elixir 1.15.4) lib/enum.ex:4830: Enumerable.List.reduce/3 (elixir 1.15.4) lib/enum.ex:2564: Enum.reduce_while/3 (ash_postgres 1.3.41) lib/aggregate.ex:53: AshPostgres.Aggregate.add_aggregates/6 (ash 2.13.3) lib/ash/query/aggregate.ex:570: anonymous fn/6 in Ash.Query.Aggregate.value_request/9 (ash 2.13.3) lib/ash/engine/engine.ex:537: anonymous fn/2 in Ash.Engine.run_iteration/1 (ash 2.13.3) lib/ash/engine/engine.ex:558: anonymous fn/4 in Ash.Engine.async/2 (elixir 1.15.4) lib/task/supervised.ex:101: Task.Supervised.invoke_mfa/2 (elixir 1.15.4) lib/task/supervised.ex:36: Task.Supervised.reply/4 (ash 2.13.3) lib/ash/engine/engine.ex:552: Ash.Engine.async/2 (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2 (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2 (ash 2.13.3) lib/ash/engine/engine.ex:702: Ash.Engine.start_pending_tasks/1 (ash 2.13.3) lib/ash/engine/engine.ex:323: Ash.Engine.run_to_completion/1 (ash 2.13.3) lib/ash/engine/engine.ex:252: Ash.Engine.do_run/2 (ash 2.13.3) lib/ash/engine/engine.ex:148: Ash.Engine.run/2 (ash 2.13.3) lib/ash/actions/read.ex:173: Ash.Actions.Read.do_run/3 (ash 2.13.3) lib/ash/actions/read.ex:96: Ash.Actions.Read.run/3 (ash 2.13.3) lib/ash/api/api.ex:1733: Ash.Api.load/4 (ash 2.13.3) lib/ash/api/api.ex:1707: Ash.Api.load!/4 test/complex_calculations_test.exs:55: (test)
2023-08-17 12:49:18 +12:00
end