ash_graphql/test/create_test.exs
Zach Daniel 9151b712f1 improvement: start on error messaging groundwork
fix: properly handle relationship changes on updates
2021-03-15 15:51:17 -04:00

117 lines
2.5 KiB
Elixir

defmodule AshGraphql.CreateTest do
use ExUnit.Case, async: false
setup do
on_exit(fn ->
try do
ETS.Set.delete(ETS.Set.wrap_existing!(AshGraphql.Test.Post))
ETS.Set.delete(ETS.Set.wrap_existing!(AshGraphql.Test.Comment))
rescue
_ ->
:ok
end
end)
end
test "a create with arguments works" do
resp =
"""
mutation CreatePost($input: CreatePostInput) {
createPost(input: $input) {
result{
text
}
errors{
message
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{
"input" => %{
"text" => "foobar",
"confirmation" => "foobar"
}
}
)
assert {:ok, result} = resp
refute Map.has_key?(result, :errors)
assert %{data: %{"createPost" => %{"result" => %{"text" => "foobar"}}}} = result
end
test "arguments are threaded properly" do
resp =
"""
mutation CreatePost($input: CreatePostInput) {
createPost(input: $input) {
result{
text
}
errors{
message
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{
"input" => %{
"text" => "foobar",
"confirmation" => "foobar2"
}
}
)
assert {:ok, result} = resp
assert %{data: %{"createPost" => %{"result" => nil, "errors" => [%{"message" => message}]}}} =
result
assert message =~ "Confirmation did not match value"
end
test "relationships are applied properly" do
comment = AshGraphql.Test.Api.create!(Ash.Changeset.new(AshGraphql.Test.Comment))
resp =
"""
mutation CreatePost($input: CreatePostInput) {
createPost(input: $input) {
result{
text
comments{
id
}
}
errors{
message
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{
"input" => %{
"text" => "foobar",
"confirmation" => "foobar",
"comments" => [comment.id]
}
}
)
assert {:ok, result} = resp
comment_id = comment.id
assert %{
data: %{
"createPost" => %{
"result" => %{"comments" => [%{"id" => ^comment_id}]},
"errors" => []
}
}
} = result
end
end