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
25 changes: 25 additions & 0 deletions test/calculation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,31 @@
assert full_name == "name"
end

test "cond/if calculation can be used in query filters" do

Check failure on line 1103 in test/calculation_test.exs

View workflow job for this annotation

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

test cond/if calculation can be used in query filters (AshPostgres.CalculationTest)

Check failure on line 1103 in test/calculation_test.exs

View workflow job for this annotation

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

test cond/if calculation can be used in query filters (AshPostgres.CalculationTest)

Check failure on line 1103 in test/calculation_test.exs

View workflow job for this annotation

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

test cond/if calculation can be used in query filters (AshPostgres.CalculationTest)

Check failure on line 1103 in test/calculation_test.exs

View workflow job for this annotation

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

test cond/if calculation can be used in query filters (AshPostgres.CalculationTest)
post =
Post
|> Ash.Changeset.for_create(:create, %{title: "match", score: 75})
|> Ash.create!()

# Loading the cond-based calculation works fine
post = Ash.load!(post, :score_category)
assert post.score_category == 2

# Filtering by a cond-based calculation should also work.
# This currently fails with DBConnection.EncodeError because the THEN
# clause literals in the generated CASE WHEN are not type-cast when the
# expression appears in a WHERE clause (they ARE cast in SELECT).
assert [_] =
Post
|> Ash.Query.filter(score_category == 2)
|> Ash.read!()

assert [] =
Post
|> Ash.Query.filter(score_category == 3)
|> Ash.read!()
end

test "calculation with fragment and cond returning integer doesn't cause Postgrex encoding error" do
Post
|> Ash.Changeset.for_create(:create, %{title: "hello ash lovers"})
Expand Down
14 changes: 14 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,20 @@ defmodule AshPostgres.Test.Post do

calculate(:c_times_p, :integer, expr(count_of_comments * count_of_linked_posts))


calculate(
:score_category,
:integer,
expr(
cond do
score > 100 -> 3
score > 50 -> 2
score > 0 -> 1
true -> 0
end
)
)

calculate(
:literal_map_in_expr,
:map,
Expand Down
Loading