Skip to content

Commit 97119f2

Browse files
committed
test: add failing test for expr(has_one.function_calc) nil poisoning in batch
1 parent d3c8ca2 commit 97119f2

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

test/calculation_test.exs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,4 +1603,48 @@ defmodule AshPostgres.CalculationTest do
16031603
assert loaded3.active_item_name == nil
16041604
assert loaded3.all_item_name == nil
16051605
end
1606+
1607+
test "expr(has_one.function_based_calculation) in batch with nil relationships" do
1608+
alias AshPostgres.Test.{Chat, Message}
1609+
1610+
chat1 = Ash.Seed.seed!(Chat, %{name: "Chat 1"})
1611+
1612+
chat2 = Ash.Seed.seed!(Chat, %{name: "Chat 2"})
1613+
1614+
Ash.Seed.seed!(Message, %{
1615+
chat_id: chat2.id,
1616+
content: "Unread message",
1617+
read_at: nil
1618+
})
1619+
1620+
chat3 = Ash.Seed.seed!(Chat, %{name: "Chat 3"})
1621+
1622+
Ash.Seed.seed!(Message, %{
1623+
chat_id: chat3.id,
1624+
content: "Read message",
1625+
read_at: DateTime.utc_now()
1626+
})
1627+
1628+
single =
1629+
Chat
1630+
|> Ash.Query.filter(id == ^chat2.id)
1631+
|> Ash.Query.load([:last_unread_message_formatted_fn])
1632+
|> Ash.read!()
1633+
|> hd()
1634+
1635+
assert single.last_unread_message_formatted_fn == "FnMessage: Unread message"
1636+
1637+
chats =
1638+
Chat
1639+
|> Ash.Query.filter(id in [^chat1.id, ^chat2.id, ^chat3.id])
1640+
|> Ash.Query.sort(:name)
1641+
|> Ash.Query.load([:last_unread_message_formatted_fn])
1642+
|> Ash.read!()
1643+
1644+
[loaded1, loaded2, loaded3] = chats
1645+
1646+
assert loaded1.last_unread_message_formatted_fn == nil
1647+
assert loaded2.last_unread_message_formatted_fn == "FnMessage: Unread message"
1648+
assert loaded3.last_unread_message_formatted_fn == nil
1649+
end
16061650
end

test/support/resources/chat.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ defmodule AshPostgres.Test.Chat do
2323
attribute(:name, :string, public?: true)
2424
end
2525

26+
calculations do
27+
calculate(
28+
:last_unread_message_formatted_fn,
29+
:string,
30+
expr(last_unread_message.formatted_content_fn)
31+
)
32+
end
33+
2634
relationships do
2735
belongs_to :last_read_message, AshPostgres.Test.Message do
2836
allow_nil?(true)

test/support/resources/message.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ defmodule AshPostgres.Test.Message do
2525
attribute(:read_at, :utc_datetime, public?: true)
2626
end
2727

28+
calculations do
29+
calculate :formatted_content_fn, :string do
30+
load([:content])
31+
32+
calculation(fn records, _context ->
33+
Enum.map(records, &"FnMessage: #{&1.content}")
34+
end)
35+
end
36+
end
37+
2838
relationships do
2939
belongs_to :chat, AshPostgres.Test.Chat do
3040
public?(true)

0 commit comments

Comments
 (0)