2020-07-15 17:38:42 +12:00
|
|
|
defmodule Ash.Test.Actions.ValidationTest do
|
|
|
|
@moduledoc false
|
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
|
|
|
defmodule Profile do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
|
|
|
data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private? true
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2022-04-29 10:07:06 +12:00
|
|
|
defaults [:create, :read, :update, :destroy]
|
2020-07-15 17:38:42 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
validations do
|
|
|
|
validate absent([:bio, :date]), on: :create
|
|
|
|
validate present(:bio), on: :update
|
|
|
|
validate absent(:date), on: :destroy
|
2021-10-02 16:27:35 +13:00
|
|
|
|
2020-08-27 16:30:09 +12:00
|
|
|
validate one_of(:status, ["foo", "bar"])
|
2021-10-02 16:27:35 +13:00
|
|
|
|
|
|
|
validate attribute_equals(:status, "foo") do
|
|
|
|
where([attribute_equals(:foo, true)])
|
|
|
|
end
|
2022-07-08 06:12:32 +12:00
|
|
|
|
2022-10-08 04:24:04 +13:00
|
|
|
validate attribute_does_not_equal(:status, "foo"),
|
|
|
|
where: attribute_equals(:foo, false)
|
2020-07-15 17:38:42 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2020-07-15 17:38:42 +12:00
|
|
|
attribute :bio, :string
|
|
|
|
attribute :date, :date
|
2020-08-27 16:30:09 +12:00
|
|
|
attribute :status, :string
|
2021-10-02 16:27:35 +13:00
|
|
|
attribute :foo, :boolean
|
2020-07-15 17:38:42 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-07 19:41:02 +13:00
|
|
|
defmodule Registry do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Registry
|
|
|
|
|
|
|
|
entries do
|
|
|
|
entry Profile
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-15 17:38:42 +12:00
|
|
|
defmodule Api do
|
|
|
|
use Ash.Api
|
|
|
|
|
|
|
|
resources do
|
2021-10-07 19:41:02 +13:00
|
|
|
registry Registry
|
2020-07-15 17:38:42 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "validations run on create" do
|
|
|
|
assert_raise(Ash.Error.Invalid, ~r/bio, date: must be absent/, fn ->
|
|
|
|
Profile
|
|
|
|
|> Ash.Changeset.new(%{bio: "foobar"})
|
|
|
|
|> Api.create!()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-10-02 16:27:35 +13:00
|
|
|
test "validations only run when their when conditions validate properly" do
|
|
|
|
Profile
|
2022-07-08 06:12:32 +12:00
|
|
|
|> Ash.Changeset.new(%{foo: false, status: "bar"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Profile
|
|
|
|
|> Ash.Changeset.new(%{foo: true, status: "foo"})
|
2021-10-02 16:27:35 +13:00
|
|
|
|> Api.create!()
|
|
|
|
|
2022-07-08 06:12:32 +12:00
|
|
|
assert_raise(Ash.Error.Invalid, ~r/status: must not equal foo/, fn ->
|
|
|
|
Profile
|
|
|
|
|> Ash.Changeset.new(%{foo: false, status: "foo"})
|
|
|
|
|> Api.create!()
|
|
|
|
end)
|
|
|
|
|
2021-10-02 16:27:35 +13:00
|
|
|
assert_raise(Ash.Error.Invalid, ~r/status: must equal foo/, fn ->
|
|
|
|
Profile
|
|
|
|
|> Ash.Changeset.new(%{foo: true, status: "bar"})
|
|
|
|
|> Api.create!()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-07-15 17:38:42 +12:00
|
|
|
test "validations run on update" do
|
|
|
|
assert_raise(Ash.Error.Invalid, ~r/bio: must be present/, fn ->
|
|
|
|
Profile
|
|
|
|
|> Ash.Changeset.new(%{})
|
|
|
|
|> Api.create!()
|
|
|
|
|> Ash.Changeset.new(%{})
|
|
|
|
|> Api.update!()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "validations run on destroy" do
|
|
|
|
assert_raise(Ash.Error.Invalid, ~r/date: must be absent/, fn ->
|
|
|
|
Profile
|
|
|
|
|> Ash.Changeset.new(%{})
|
|
|
|
|> Api.create!()
|
|
|
|
|> Ash.Changeset.new(%{bio: "foo", date: Date.utc_today()})
|
|
|
|
|> Api.update!()
|
|
|
|
|> Api.destroy!()
|
|
|
|
end)
|
|
|
|
end
|
2020-08-27 16:30:09 +12:00
|
|
|
|
|
|
|
describe "one_of" do
|
|
|
|
test "it succeeds if the value is in the list" do
|
|
|
|
Profile
|
|
|
|
|> Ash.Changeset.new(%{status: "foo"})
|
|
|
|
|> Api.create!()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it fails if the value is not in the list" do
|
2020-10-21 06:11:21 +13:00
|
|
|
assert_raise(Ash.Error.Invalid, ~r/expected one of foo, bar/, fn ->
|
2020-08-27 16:30:09 +12:00
|
|
|
Profile
|
|
|
|
|> Ash.Changeset.new(%{status: "blart"})
|
|
|
|
|> Api.create!()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
2020-07-15 17:38:42 +12:00
|
|
|
end
|