Skip to content
Merged
Show file tree
Hide file tree
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
44 changes: 44 additions & 0 deletions test/calculation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,4 +1603,48 @@
assert loaded3.active_item_name == nil
assert loaded3.all_item_name == nil
end

test "expr(has_one.function_based_calculation) in batch with nil relationships" do

Check failure on line 1607 in test/calculation_test.exs

View workflow job for this annotation

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

test expr(has_one.function_based_calculation) in batch with nil relationships (AshPostgres.CalculationTest)

Check failure on line 1607 in test/calculation_test.exs

View workflow job for this annotation

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

test expr(has_one.function_based_calculation) in batch with nil relationships (AshPostgres.CalculationTest)

Check failure on line 1607 in test/calculation_test.exs

View workflow job for this annotation

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

test expr(has_one.function_based_calculation) in batch with nil relationships (AshPostgres.CalculationTest)
alias AshPostgres.Test.{Chat, Message}

chat1 = Ash.Seed.seed!(Chat, %{name: "Chat 1"})

chat2 = Ash.Seed.seed!(Chat, %{name: "Chat 2"})

Ash.Seed.seed!(Message, %{
chat_id: chat2.id,
content: "Unread message",
read_at: nil
})

chat3 = Ash.Seed.seed!(Chat, %{name: "Chat 3"})

Ash.Seed.seed!(Message, %{
chat_id: chat3.id,
content: "Read message",
read_at: DateTime.utc_now()
})

single =
Chat
|> Ash.Query.filter(id == ^chat2.id)
|> Ash.Query.load([:last_unread_message_formatted_fn])
|> Ash.read!()
|> hd()

assert single.last_unread_message_formatted_fn == "FnMessage: Unread message"

chats =
Chat
|> Ash.Query.filter(id in [^chat1.id, ^chat2.id, ^chat3.id])
|> Ash.Query.sort(:name)
|> Ash.Query.load([:last_unread_message_formatted_fn])
|> Ash.read!()

[loaded1, loaded2, loaded3] = chats

assert loaded1.last_unread_message_formatted_fn == nil
assert loaded2.last_unread_message_formatted_fn == "FnMessage: Unread message"
assert loaded3.last_unread_message_formatted_fn == nil
end
end
8 changes: 8 additions & 0 deletions test/support/resources/chat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ defmodule AshPostgres.Test.Chat do
attribute(:name, :string, public?: true)
end

calculations do
calculate(
:last_unread_message_formatted_fn,
:string,
expr(last_unread_message.formatted_content_fn)
)
end

relationships do
belongs_to :last_read_message, AshPostgres.Test.Message do
allow_nil?(true)
Expand Down
10 changes: 10 additions & 0 deletions test/support/resources/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ defmodule AshPostgres.Test.Message do
attribute(:read_at, :utc_datetime, public?: true)
end

calculations do
calculate :formatted_content_fn, :string do
load([:content])

calculation(fn records, _context ->
Enum.map(records, &"FnMessage: #{&1.content}")
end)
end
end

relationships do
belongs_to :chat, AshPostgres.Test.Chat do
public?(true)
Expand Down
Loading