A Small library to help test your Absinthe Schema. This provides a centralized way to save queries and helper functions for running those queries and also for verifying the results of those queries.
If available in Hex, the package can be installed
by adding absinthe_test
to your list of dependencies in mix.exs
:
def deps do
[
{:absinthe_test, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/absinthe_test.
You will need to tell AbsintheTest
what module your Schema lives in. To do this simply set the schema
configuration to the name of your module:
# test.exs
config :absinthe_test, schema: My.Schema.Module
You will also need to start AbsintheTest
before you use it (there is a small genserver which manages query storage for you)
# test_helper.exs
AbsintheTest.start([], [])
To test your schema you will need to do three things:
- register queries
- execute queries
- verify results
The examples below will use the following Schema:
query do
field :test, :test do
arg :value
resolve fn args, _ ->
{:ok, %{value: Map.get(args, :value)}}
end
end
end
query = """
query testQuery($id: ID!, $value: Int) {
testQuery(id: $id, value: $value) {
id
value
}
}
"""
AbsintheTest.Context.register_query(:test_query, query)
result = AbsintheTest.Context.run_query(:test_query, variables: %{"value" => 4})
true = AbsintheTest.expected_result?(result, "test", %{"value" => 4})
The expected_result?
function has two forms /3
and /2
. The /3
form will validate assuming an {:ok, Absinthe.run_result}
form. You can check for a specific key/value pair in the response payload with that form. The /2
form will validate assuming an {:error, String.t}
form.
iex> {:ok, %{data: %{"test" => %{"value" => 5}}}}
iex> |> AbsintheTest.Context.expected_result?("test", %{"value" => 5})
true
iex> {:ok, %{data: %{"test" => %{"id" => 84}}}}
iex> |> AbsintheTest.Context.expected_result?("test", %{"value" => 22})
false
iex> {:error, "Soylent Green is People"}
iex> |> AbsintheTest.Context.expected_result?("hungerLevel", %{"value" => 0})
false
iex> {:error, "Soylent Green is People"}
iex> |> AbsintheTest.Context.expected_result?("Soylent Green is People")
true