test: add test for manage relationship error paths

This commit is contained in:
Zach Daniel 2024-09-11 15:24:33 -04:00
parent 2e2ab0e778
commit 8f0621378a
2 changed files with 67 additions and 1 deletions

View file

@ -18,7 +18,7 @@
"git_cli": {:hex, :git_cli, "0.3.0", "a5422f9b95c99483385b976f5d43f7e8233283a47cda13533d7c16131cb14df5", [:mix], [], "hexpm", "78cb952f4c86a41f4d3511f1d3ecb28edb268e3a7df278de2faa1bd4672eaf9b"},
"git_ops": {:hex, :git_ops, "2.6.1", "cc7799a68c26cf814d6d1a5121415b4f5bf813de200908f930b27a2f1fe9dad5", [:mix], [{:git_cli, "~> 0.2", [hex: :git_cli, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "ce62d07e41fe993ec22c35d5edb11cf333a21ddaead6f5d9868fcb607d42039e"},
"glob_ex": {:hex, :glob_ex, "0.1.8", "f7ef872877ca2ae7a792ab1f9ff73d9c16bf46ecb028603a8a3c5283016adc07", [:mix], [], "hexpm", "9e39d01729419a60a937c9260a43981440c43aa4cadd1fa6672fecd58241c464"},
"igniter": {:hex, :igniter, "0.3.34", "fee93422583884b4a6985de45797097d36f36283d4e61c6154d0e8ec02e19e2b", [:mix], [{:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:rewrite, "~> 0.9", [hex: :rewrite, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.4", [hex: :sourceror, repo: "hexpm", optional: false]}, {:spitfire, ">= 0.1.3 and < 1.0.0-0", [hex: :spitfire, repo: "hexpm", optional: false]}], "hexpm", "b8e0bd0cdc8354b44f292a3eab4eaac155e4a9c9784b066ec29a2587595bcae8"},
"igniter": {:hex, :igniter, "0.3.35", "edd8db6234db7639eb2f954b45ab23db98b1fd0a940850f58db8900912a908bf", [:mix], [{:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:rewrite, "~> 0.9", [hex: :rewrite, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.4", [hex: :sourceror, repo: "hexpm", optional: false]}, {:spitfire, ">= 0.1.3 and < 1.0.0-0", [hex: :spitfire, repo: "hexpm", optional: false]}], "hexpm", "d2826d94d8c851e2bc8b920766815b3df4b184cab05eae8423e965363a2e02b1"},
"iterex": {:hex, :iterex, "0.1.2", "58f9b9b9a22a55cbfc7b5234a9c9c63eaac26d276b3db80936c0e1c60355a5a6", [:mix], [], "hexpm", "2e103b8bcc81757a9af121f6dc0df312c9a17220f302b1193ef720460d03029d"},
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
"libgraph": {:hex, :libgraph, "0.16.0", "3936f3eca6ef826e08880230f806bfea13193e49bf153f93edcf0239d4fd1d07", [:mix], [], "hexpm", "41ca92240e8a4138c30a7e06466acc709b0cbb795c643e9e17174a178982d6bf"},

View file

@ -0,0 +1,66 @@
defmodule Ash.Test.ManageRelationshipTest do
@moduledoc false
use ExUnit.Case, async: true
defmodule ParentResource do
use Ash.Resource,
domain: Ash.Test.Domain
actions do
defaults [:read, :update, :destroy]
create :create do
accept [:name]
argument :related_resource, :map, allow_nil?: false
change manage_relationship(:related_resource, :related_resource, type: :create)
end
end
attributes do
uuid_primary_key :id
attribute :name, :string
end
relationships do
has_one :related_resource, Ash.Test.ManageRelationshipTest.RelatedResource,
destination_attribute: :parent_resource_id
end
end
defmodule RelatedResource do
use Ash.Resource,
domain: Ash.Test.Domain
actions do
defaults [:read, :destroy, create: :*, update: :*]
end
attributes do
uuid_primary_key :id
attribute :required_attribute, :string, allow_nil?: false
end
relationships do
belongs_to :parent_resource, ParentResource
end
end
test "errors have the proper path set on them" do
assert {:error,
%Ash.Error.Invalid{
errors: [
%{path: [:related_resource, 0]}
]
}} =
ParentResource
|> Ash.Changeset.for_create(:create, %{
name: "Test Parent Resource",
related_resource: %{
# This is the missing required field
required_attribute: nil
}
})
|> Ash.create()
end
end