test: pass in arguments to validation (#518)

This commit is contained in:
Barnabas Jovanovics 2023-02-28 16:03:35 +01:00 committed by GitHub
parent eec9de56ce
commit f312d53eab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -133,6 +133,13 @@ defmodule Ash.Test.CalculationTest do
actions do
defaults [:create, :read, :update, :destroy]
read :paginated do
pagination do
keyset? true
default_limit 10
end
end
end
attributes do
@ -177,6 +184,13 @@ defmodule Ash.Test.CalculationTest do
calculate :names_of_best_friends_of_me, :string, NamesOfBestFriendsOfMe
calculate :name_with_users_name, :string, NameWithUsersName
calculate :full_name_with_salutation,
:string,
expr(^arg(:salutation) <> " " <> conditional_full_name) do
argument :salutation, :string, allow_nil?: false
load [:conditional_full_name]
end
calculate :conditional_full_name,
:string,
expr(
@ -428,6 +442,19 @@ defmodule Ash.Test.CalculationTest do
assert full_names == ["zach daniel", "brian cranston"]
end
test "can sort on calculation in paginated read" do
full_names =
User
|> Ash.Query.for_read(:paginated, %{salutation: "Mr"})
|> Ash.Query.load(full_name_with_salutation: [salutation: "Mr"])
|> Ash.Query.sort(full_name_with_salutation: {:asc, %{salutation: "Mr"}})
|> Api.read!()
|> Map.get(:results)
|> Enum.map(& &1.full_name_with_salutation)
assert full_names == []
end
test "the `if` calculation resolves the first expr when true, and the second when false" do
User
|> Ash.Changeset.new(%{first_name: "bob"})