Skip to content

Commit 7e0b5d5

Browse files
committed
failed to filter array repro. fix assert_valid_resource_objects
1 parent d351726 commit 7e0b5d5

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

lib/ash_json_api/test/test.ex

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,16 @@ defmodule AshJsonApi.Test do
362362
"data" => results
363363
} = conn.resp_body
364364

365-
assert Enum.all?(results, fn
366-
%{"type" => ^expected_type, "id" => maybe_known_id} ->
367-
Enum.member?(expected_ids, maybe_known_id)
368-
369-
_ ->
370-
false
371-
end)
365+
# Extract actual IDs from results
366+
actual_ids =
367+
Enum.map(results, fn
368+
%{"type" => ^expected_type, "id" => id} -> id
369+
other -> flunk("Unexpected result: #{inspect(other)}")
370+
end)
371+
372+
# Check that we have exactly the expected IDs (order doesn't matter)
373+
assert Enum.sort(actual_ids) == Enum.sort(expected_ids),
374+
"Expected IDs #{inspect(expected_ids)}, but got #{inspect(actual_ids)}"
372375

373376
conn
374377
end

test/spec_compliance/fetching_data/filtering_test.exs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ defmodule AshJsonApiTest.FetchingData.Filtering do
44

55
# credo:disable-for-this-file Credo.Check.Readability.MaxLineLength
66

7+
defmodule Narrative do
8+
use Ash.Type.Enum,
9+
values: [
10+
:novel,
11+
:legend,
12+
:myth,
13+
:fable,
14+
:tale,
15+
:action,
16+
:plot
17+
]
18+
end
19+
720
defmodule Author do
821
use Ash.Resource,
922
domain: AshJsonApiTest.FetchingData.Filtering.Domain,
@@ -72,13 +85,18 @@ defmodule AshJsonApiTest.FetchingData.Filtering do
7285
defaults([:read, :update, :destroy])
7386

7487
create :create do
75-
accept([:name, :author_id])
88+
accept([:name, :author_id, :narratives])
7689
end
7790
end
7891

7992
attributes do
8093
uuid_primary_key(:id)
8194
attribute(:name, :string, public?: true)
95+
96+
attribute(:narratives, {:array, Narrative},
97+
default: [],
98+
public?: true
99+
)
82100
end
83101

84102
relationships do
@@ -162,6 +180,33 @@ defmodule AshJsonApiTest.FetchingData.Filtering do
162180
|> assert_invalid_resource_objects("post", [post.id])
163181
end
164182

183+
test "in/not_in filter" do
184+
post =
185+
Post
186+
|> Ash.Changeset.for_create(:create, %{name: "foo", narratives: [:novel, :legend]})
187+
|> Ash.create!()
188+
189+
post2 =
190+
Post
191+
|> Ash.Changeset.for_create(:create, %{name: "bar", narratives: [:legend]})
192+
|> Ash.create!()
193+
194+
_conn =
195+
Domain
196+
|> get("/posts?filter[narratives][in]=novel,legend", status: 200)
197+
|> assert_valid_resource_objects("post", [post.id, post2.id])
198+
199+
# _conn =
200+
# Domain
201+
# |> get("/posts?filter[narratives][in][][]=novel", status: 200)
202+
# |> assert_valid_resource_objects("post", [post.id, post2.id])
203+
204+
# _conn =
205+
# Domain
206+
# |> get("/posts?filter[narratives][not_in]=novel,legend", status: 200)
207+
# |> assert_valid_resource_objects("post", [])
208+
end
209+
165210
test "is_nil filter" do
166211
post =
167212
Post

0 commit comments

Comments
 (0)