Skip to content

Commit d6c3520

Browse files
committed
test: add tests for calculation typing
1 parent 919b9df commit d6c3520

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

test/calculation_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,4 +1034,15 @@ defmodule AshPostgres.CalculationTest do
10341034

10351035
assert full_name == "name"
10361036
end
1037+
1038+
test "calculation with fragment and cond returning integer doesn't cause Postgrex encoding error" do
1039+
Post
1040+
|> Ash.Changeset.for_create(:create, %{title: "hello ash lovers"})
1041+
|> Ash.create!()
1042+
1043+
assert [%Post{}] =
1044+
Post
1045+
|> Ash.Query.sort("posts_with_matching_title.relevance_score")
1046+
|> Ash.read!()
1047+
end
10371048
end

test/support/resources/post.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,24 @@ defmodule AshPostgres.Test.Post do
788788
end
789789

790790
calculations do
791+
calculate :relevance_score,
792+
:integer,
793+
expr(
794+
cond do
795+
fragment(
796+
"ts_rank_cd(to_tsvector(?), ?, 32)::float",
797+
^ref(:title),
798+
fragment("to_tsquery(?)", ^arg(:query))
799+
) > 0.6 ->
800+
1
801+
802+
true ->
803+
2
804+
end
805+
) do
806+
argument(:query, :string)
807+
end
808+
791809
calculate(:upper_thing, :string, expr(fragment("UPPER(?)", uniq_on_upper)))
792810

793811
calculate(:upper_title, :string, expr(fragment("UPPER(?)", title)))

0 commit comments

Comments
 (0)