Skip to content

repro(filter): failed to filter array #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 10 additions & 7 deletions lib/ash_json_api/test/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,16 @@ defmodule AshJsonApi.Test do
"data" => results
} = conn.resp_body

assert Enum.all?(results, fn
%{"type" => ^expected_type, "id" => maybe_known_id} ->
Enum.member?(expected_ids, maybe_known_id)

_ ->
false
end)
# Extract actual IDs from results
actual_ids =
Enum.map(results, fn
%{"type" => ^expected_type, "id" => id} -> id
other -> flunk("Unexpected result: #{inspect(other)}")
end)

# Check that we have exactly the expected IDs (order doesn't matter)
assert Enum.sort(actual_ids) == Enum.sort(expected_ids),
"Expected IDs #{inspect(expected_ids)}, but got #{inspect(actual_ids)}"

conn
end
Expand Down
135 changes: 134 additions & 1 deletion test/spec_compliance/fetching_data/filtering_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ defmodule AshJsonApiTest.FetchingData.Filtering do

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

defmodule Narrative do
use Ash.Type.Enum,
values: [
:novel,
:legend,
:myth,
:fable,
:tale,
:action,
:plot
]
end

defmodule Author do
use Ash.Resource,
domain: AshJsonApiTest.FetchingData.Filtering.Domain,
Expand Down Expand Up @@ -72,13 +85,18 @@ defmodule AshJsonApiTest.FetchingData.Filtering do
defaults([:read, :update, :destroy])

create :create do
accept([:name, :author_id])
accept([:name, :author_id, :narratives])
end
end

attributes do
uuid_primary_key(:id)
attribute(:name, :string, public?: true)

attribute(:narratives, {:array, Narrative},
default: [],
public?: true
)
end

relationships do
Expand Down Expand Up @@ -162,6 +180,121 @@ defmodule AshJsonApiTest.FetchingData.Filtering do
|> assert_invalid_resource_objects("post", [post.id])
end

test "equals array filter" do
post =
Post
|> Ash.Changeset.for_create(:create, %{name: "foo", narratives: [:novel, :legend]})
|> Ash.create!()

post2 =
Post
|> Ash.Changeset.for_create(:create, %{name: "bar", narratives: [:legend]})
|> Ash.create!()

_conn =
Domain
|> get("/posts?filter[narratives][]=novel&filter[narratives][]=legend",
status: 200
)
|> assert_valid_resource_objects("post", [post.id])

# order of values matters
_conn =
Domain
|> get("/posts?filter[narratives][]=legend&filter[narratives][]=novel",
status: 200
)
|> assert_valid_resource_objects("post", [])

_conn =
Domain
|> get("/posts?filter[narratives][]=legend",
status: 200
)
|> assert_valid_resource_objects("post", [post2.id])

# it's not inclusion check
_conn =
Domain
|> get("/posts?filter[narratives][]=novel",
status: 200
)
|> assert_valid_resource_objects("post", [])
end

test "in/not_in array filter" do
post =
Post
|> Ash.Changeset.for_create(:create, %{name: "foo", narratives: [:novel, :legend]})
|> Ash.create!()

post2 =
Post
|> Ash.Changeset.for_create(:create, %{name: "bar", narratives: [:legend]})
|> Ash.create!()

# single value
# _conn =
# Domain
# |> get("posts/filter[narratives][in]=novel",
# status: 200
# )
# |> assert_valid_resource_objects("post", [post.id, post2.id])

# comma-separated
# _conn =
# Domain
# |> get("/posts?filter[narratives][in]=novel,legend",
# status: 200
# )
# |> assert_valid_resource_objects("post", [post.id, post2.id])

# deep object
# _conn =
# Domain
# |> get(
# URI.encode(
# "/posts?filter=[{name:\"narratives\", op: \"in\", val: [\"novel\",\"legend\"]}]"
# ),
# status: 200
# )
# |> assert_valid_resource_objects("post", [post.id, post2.id])

# PHP-style array
# _conn =
# Domain
# |> get("/posts?filter[narratives][in][]=novel&filter[narratives][in][]=legend",
# status: 200
# )
# |> assert_valid_resource_objects("post", [post.id, post2.id])

# nested array
# _conn =
# Domain
# |> get("/posts?filter[narratives][in][][]=novel ",
# status: 200
# )
# |> assert_valid_resource_objects("post", [post.id, post2.id])

# bracket notation
# _conn =
# Domain
# |> get("/posts?filter[narratives][in]=[novel,legend]", status: 200)
# |> assert_valid_resource_objects("post", [post.id, post2.id])

_conn =
Domain
|> get("/posts?filter[narratives][in][0]=novel&filter[narratives][in][1]=legend",
status: 200
)
|> assert_valid_resource_objects("post", [post.id, post2.id])

# _conn =
# Domain
# |> get("/posts?filter[narratives][not_in]=novel,legend", status: 200)
# |> assert_valid_resource_objects("post", [])
end

test "is_nil filter" do
post =
Post
Expand Down