AbsintheErrorPayload.TestHelper (AbsintheErrorPayload v1.2.0) View Source
This module defines assertions and helpers to use when testing graphql.
Link to this section Summary
Functions
Compares a expected list, map or struct to a graphql response.
Compares an expected list of validation errors to a successful mutation response, as generated by AbsintheErrorPayload.Payload.build_payload/2
Compares an expected map to a successful mutation response, as generated by AbsintheErrorPayload.Payload.build_payload/2
compares ISO Extended formatted strings
Returns a map with all keys and values set to strings, which is close to how the raw graphql result is returned.
Mapping of AbsintheErrorPayload.ValidationMessage
fields used by assert_mutation_failure
Subset of Mapping of AbsintheErrorPayload.ValidationMessage
fields used by assert_mutation_failure
Link to this section Functions
Compares a expected list, map or struct to a graphql response.
This function uses a field definition that specifies the expected field type of the data. This performs some simple transforms to properly compare values, including:
- camelCasing field names
- converting all non-int values to strings, including converting
false
to"false"
- uppercasing enum field responses
- parsing date types as ISO Extended format strings
Supported field types are: :string, :float, :integer, :enum, :date, :list, :boolean
Example
query = "{ user(id: \"1\")
{
firstName
lastName
age
}
}
"
{:ok, %{data: data}} = evaluate_graphql(query)
user_fields = %{first_name: :string, last_name: string, age: integer}
expected = %User{first_name: "Lilo", last_name: "Pelekai", age: 6}
%{"user" => result} = data
assert_equivalent_graphql(expected, result, user_fields)
Compares an expected list of validation errors to a successful mutation response, as generated by AbsintheErrorPayload.Payload.build_payload/2
Optionally pass a subset of fields to compare - by default all AbsintheErrorPayload.ValidationMessage
fields are included,
so you will need to use this parameter if you are returning fewer fields.
Example
user = insert(:user, %{first_name: "Stitch"})
query =
"
mutation{
updateUser(
id: #{user.id}
user: {
age: 0
}
){
successful
messages {
field
message
code
}
result {
id
firstName
age
updatedAt
}
}
}
"
assert {:ok, %{data: data}} = evaluate_graphql(query)
assert %{"updateUser" => payload} = data
expected = %ValidationMessage{
code: :greater_than, field: :age, message: "must be greater than 0"
}
assert_mutation_failure([expected], payload, [:field, :message, :code])
Compares an expected map to a successful mutation response, as generated by AbsintheErrorPayload.Payload.build_payload/2
Example
query =
"
mutation{
createUser(
user: {
age: 6
firstName: "Lilo"
lastName: "Pelekai"
}
){
successful
messages {
field
message
code
}
result {
id
age
firstName
lastName
}
}
}
"
assert {:ok, %{data: data}} = evaluate_graphql(query)
assert %{"createUser" => payload} = data
user_fields = %{first_name: :string, last_name: string, age: integer}
expected = %User{first_name: "Lilo", last_name: "Pelekai", age: 6}
assert_mutation_success(expected, payload, user_fields)
assert %User{} = Repo.get(User, payload["result"]["id"])
compares ISO Extended formatted strings
Returns a map with all keys and values set to strings, which is close to how the raw graphql result is returned.
Numbers are always stringified as this function has no insight into the graphql schema.
This is compensated for in assert_equivalent_graphql/3
, which has context about
expected data types.
Associations are returned as "association" if not preloaded.
Mapping of AbsintheErrorPayload.ValidationMessage
fields used by assert_mutation_failure
Subset of Mapping of AbsintheErrorPayload.ValidationMessage
fields used by assert_mutation_failure