test: add failing test to demonstrate error using ref (#282)

Filtering on ref in this manner worked up until Ash 3.0. Now, it gives
the following error:

1) test filter with ref (AshPostgres.FilterTest)
     test/filter_test.exs:1076
     ** (FunctionClauseError) no function clause matching in Ash.Filter.check_filterable/2

     The following arguments were given to Ash.Filter.check_filterable/2:

         # 1
         AshPostgres.Test.Organization

         # 2
         :id

     Attempted function clauses (showing 2 out of 2):

         defp check_filterable(_resource, [])
         defp check_filterable(resource, [relationship | rest])

     code: |> Ash.Query.filter(^ref(:id, [:posts, :comments]) == ^comment.id)
     stacktrace:
       (ash 3.0.1) lib/ash/filter/filter.ex:2944: Ash.Filter.check_filterable/2
       (ash 3.0.1) lib/ash/filter/filter.ex:2933: anonymous fn/2 in Ash.Filter.validate_filterable_relationship_paths/2
       (elixir 1.16.2) lib/enum.ex:4316: Enum.find_value_list/3
       (ash 3.0.1) lib/ash/filter/filter.ex:2923: Ash.Filter.validate_refs/3
       (ash 3.0.1) lib/ash/filter/filter.ex:3015: Ash.Filter.resolve_call/2
       (ash 3.0.1) lib/ash/filter/filter.ex:2489: Ash.Filter.add_expression_part/3
       (ash 3.0.1) lib/ash/filter/filter.ex:2427: anonymous fn/3 in Ash.Filter.parse_expression/2
       (elixir 1.16.2) lib/enum.ex:4839: Enumerable.List.reduce/3
       (elixir 1.16.2) lib/enum.ex:2582: Enum.reduce_while/3
       (ash 3.0.1) lib/ash/filter/filter.ex:334: Ash.Filter.parse/3
       (ash 3.0.1) lib/ash/query/query.ex:2574: Ash.Query.do_filter/3
       test/filter_test.exs:1095: (test)
This commit is contained in:
Alan Heywood 2024-05-16 01:19:29 +10:00 committed by GitHub
parent 6d825792b3
commit 3f00dcbe8e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,10 +1,11 @@
defmodule AshPostgres.FilterTest do
alias AshPostgres.Test.Organization
use AshPostgres.RepoCase, async: false
alias AshPostgres.Test.{Author, Comment, Post}
alias AshPostgres.Test.{Author, Comment, Post, Organization}
alias AshPostgres.Test.ComplexCalculations.{Channel, ChannelMember}
require Ash.Query
import Ash.Expr
describe "with no filter applied" do
test "with no data" do
@ -1070,4 +1071,29 @@ defmodule AshPostgres.FilterTest do
)
|> Ash.read!()
end
test "filter with ref" do
organization =
Organization
|> Ash.Changeset.for_create(:create, %{name: "foo"})
|> Ash.create!()
post =
Post
|> Ash.Changeset.for_create(:create, %{organization_id: organization.id})
|> Ash.create!()
comment =
Comment
|> Ash.Changeset.for_create(:create, %{title: "not match"})
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|> Ash.create!()
fetched_org =
Organization
|> Ash.Query.filter(^ref(:id, [:posts, :comments]) == ^comment.id)
|> Ash.read_one!()
assert fetched_org.id == organization.id
end
end