AbsintheErrorPayload.TestHelper (AbsintheErrorPayload v1.1.4) 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

Link to this function

assert_equivalent_graphql(expected, response, fields)

View Source

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
  • coverting 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)
Link to this function

assert_mutation_failure(expected, payload, only \\ nil)

View Source

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])
Link to this function

assert_mutation_success(expected, payload, fields)

View Source

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"])
Link to this function

assert_similar_times(first, second)

View Source

compares ISO Extended formatted strings

Link to this function

to_stringified_map(fixture)

View Source

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.

Link to this function

validation_message_fields()

View Source

Mapping of AbsintheErrorPayload.ValidationMessage fields used by assert_mutation_failure

Link to this function

validation_message_fields(only)

View Source

Subset of Mapping of AbsintheErrorPayload.ValidationMessage fields used by assert_mutation_failure