ash_postgres/test/multi_domain_calculations_test.exs

61 lines
1.9 KiB
Elixir
Raw Normal View History

2024-07-14 08:06:52 +12:00
defmodule AshPostgres.Test.MultiDomainCalculationsTest do
use AshPostgres.RepoCase, async: false
require Ash.Query
test "total is returned correctly" do
item =
AshPostgres.Test.MultiDomainCalculations.DomainOne.Item
|> Ash.Changeset.for_create(:create, %{})
|> Ash.create!()
other_item =
AshPostgres.Test.MultiDomainCalculations.DomainTwo.OtherItem
|> Ash.Changeset.for_create(:create, %{item_id: item.id})
|> Ash.create!()
for i <- 0..2 do
AshPostgres.Test.MultiDomainCalculations.DomainTwo.SubItem
|> Ash.Changeset.for_create(:create, %{other_item_id: other_item.id, amount: i})
|> Ash.create!()
end
assert [%{total_amount: 3}] =
2024-07-14 08:06:52 +12:00
Ash.read!(AshPostgres.Test.MultiDomainCalculations.DomainOne.Item,
load: [:total_amount]
)
end
test "total using relationship is returned correctly" do
item =
AshPostgres.Test.MultiDomainCalculations.DomainOne.Item
|> Ash.Changeset.for_create(:create, %{key: "key"})
|> Ash.create!()
Ash.read!(AshPostgres.Test.MultiDomainCalculations.DomainOne.Item,
load: [:total_amount_relationship]
)
2024-07-18 03:40:17 +12:00
_relationship_item =
AshPostgres.Test.MultiDomainCalculations.DomainThree.RelationshipItem
|> Ash.Changeset.for_create(:create, %{key: "key", value: 1})
|> Ash.create!()
other_item =
AshPostgres.Test.MultiDomainCalculations.DomainTwo.OtherItem
|> Ash.Changeset.for_create(:create, %{item_id: item.id})
|> Ash.create!()
for i <- 0..2 do
AshPostgres.Test.MultiDomainCalculations.DomainTwo.SubItem
|> Ash.Changeset.for_create(:create, %{other_item_id: other_item.id, amount: i})
|> Ash.create!()
end
2024-07-18 03:40:17 +12:00
assert [%{total_amount_relationship: 3}] =
Ash.read!(AshPostgres.Test.MultiDomainCalculations.DomainOne.Item,
load: [:total_amount_relationship]
)
end
2024-07-14 08:06:52 +12:00
end