2021-03-22 07:19:45 +13:00
|
|
|
defmodule Ash.Test.QueryTest do
|
|
|
|
@moduledoc false
|
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2022-07-26 10:47:30 +12:00
|
|
|
require Ash.Query
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
alias Ash.Test.Domain, as: Domain
|
|
|
|
|
2021-03-22 07:19:45 +13:00
|
|
|
defmodule User do
|
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
|
2021-03-22 07:19:45 +13:00
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
|
2021-03-22 07:19:45 +13:00
|
|
|
read :read do
|
|
|
|
primary? true
|
|
|
|
end
|
|
|
|
|
|
|
|
read :by_id do
|
|
|
|
argument :id, :uuid, allow_nil?: false
|
|
|
|
|
|
|
|
filter expr(id == ^arg(:id))
|
|
|
|
end
|
|
|
|
|
|
|
|
create :create
|
|
|
|
update :update
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
attribute :name, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
2021-10-07 19:41:02 +13:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
attribute :email, :string, sensitive?: true, public?: true
|
2021-10-07 19:41:02 +13:00
|
|
|
end
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
relationships do
|
|
|
|
belongs_to :best_friend, __MODULE__ do
|
|
|
|
public?(true)
|
|
|
|
end
|
2021-03-22 07:19:45 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "read argument validation" do
|
|
|
|
test "it returns an appropriate error when an argument is invalid" do
|
|
|
|
query = Ash.Query.for_read(User, :by_id, %{id: "foobar"})
|
|
|
|
assert [%Ash.Error.Query.InvalidArgument{field: :id}] = query.errors
|
|
|
|
end
|
|
|
|
end
|
2022-07-26 10:47:30 +12:00
|
|
|
|
2024-05-04 21:56:46 +12:00
|
|
|
describe "page validation" do
|
|
|
|
test "it returns an appropriate error when a page is invalid" do
|
|
|
|
query = Ash.Query.page(User, limit: :foo)
|
|
|
|
assert [%Ash.Error.Query.InvalidPage{page: [limit: :foo]}] = query.errors
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-26 10:47:30 +12:00
|
|
|
describe "sensitive attributes" do
|
|
|
|
test "it hides the value in filters" do
|
2022-09-27 11:08:18 +13:00
|
|
|
equals = Ash.Query.filter(User, email == "fred") |> inspect()
|
2022-07-26 10:47:30 +12:00
|
|
|
refute equals =~ "fred"
|
|
|
|
assert equals =~ "**redacted**"
|
2024-05-22 00:47:51 +12:00
|
|
|
|
2022-09-27 11:08:18 +13:00
|
|
|
contains = Ash.Query.filter(User, contains(email, "fred")) |> inspect()
|
2022-07-26 10:47:30 +12:00
|
|
|
refute contains =~ "fred"
|
|
|
|
assert contains =~ "**redacted**"
|
|
|
|
|
2022-09-27 11:08:18 +13:00
|
|
|
concat = Ash.Query.filter(User, email <> "-fred" == "a@b.com-fred") |> inspect()
|
2022-07-26 10:47:30 +12:00
|
|
|
refute concat =~ "fred"
|
|
|
|
assert concat =~ "**redacted**"
|
|
|
|
end
|
|
|
|
end
|
2022-09-20 02:14:08 +12:00
|
|
|
|
|
|
|
describe "loading?" do
|
|
|
|
test "it detects a simple load" do
|
|
|
|
assert User
|
|
|
|
|> Ash.Query.load(:best_friend)
|
|
|
|
|> Ash.Query.loading?(:best_friend)
|
|
|
|
end
|
|
|
|
end
|
2021-03-22 07:19:45 +13:00
|
|
|
end
|