b
...

Package gtt

import "github.com/ohler55/graphql-test-tool/gtt"
Overview
Index
Subdirectories

Overview ▾

Package gtt is a GraphQL Test Tool for running tests cases against a GraphQL server. All tests are driven by use case JSON files. The format is described in [file_format.md](file_format.md). Some example files are in the `examples` directory and a simple test server can be set up using the files in the `test` directory.

Each use case file should contain only on JSON object. That object can have a "comment" and a "steps" element.

- comment is an optional description of the use case.
- steps is an array of steps that define the use case.

Note: String fields such as comments and content can also be an array of strings. The array of strings is joined with newlines to for a string. The intent is to make it easier to enter multi-line comments more easily.

An example is:

{
  "comment": [
    "This example demonstrates the use of multi-line comments (this comment), a GET",
    "query, sortBy, use of exact matching (artists.0.name), and the use of a regular",
    "expression match (artists.1.name)."
  ],
  "steps": [
    {
      "label": "List the artists",
      "path": "?query={artists{name}}",
      "sortBy": {
	"artists": "name"
      },
      "expect": {
	"data": {
	  "artists": [
	    {
              "name": "Fazerdaze"
	    },
	    {
              "name": "/Boys/"
	    }
	  ]
	}
      }
    }
  ]
}

The "steps" array contains Step objects that can include the following fields:

- label is an optional label that will be displayed during a run
  if comments are being dislayed.

- comment is an optional description of the step.

- path is the path part of the URL that will be used in the
  request to the GraphQL server. The path is assumed to be relative
  to the base URL unless it starts with a '/' character.

- content is the content when using a POST request. If the
  Content is not empty then a POST is used. If it is empty then a GET
  request is made to the server. The string should be either GraphQL
  or the HTTP GET format.

- json if true and a POST request then the JSON format will be
  used with a COntent-Type of "applicaiton/json" otherwise the
  Content will be sent as GraphQL with a Content-Type of
  "application/graphql".

- remember in a series of steps it is often helpful to remember
  values from earlier steps. The Remember map describes what to
  remember and what key to store that value in. In the map, the keys
  are the keys for the memory cache while the Remember map values are
  the path to the value to remember. The path is a simple dot
  delimited path. It is not a full JSON path (maybe in the future).

- op is the operation to include in either the URL query or as a
  value for the 'operationName' if using JSON in the Content.

- vars are the variables to be passed along with a GraphQL
  request. They are appended to the URL or added to the JSON
  'variables' element if the POST contents is JSON. The values in the
  Vars map can be either a value or a string that begines with a '$'
  which indicates a rmembered value should be used instead.

- sortBy are the sort keys for the result. Depending on the
  implementation of the GraphQL server, the order of returned objects
  may not be consistent. To make it easier to use in testing the
  SortBy keys are the paths to arrays while the value for the keys
  are the attributes to sort on.

- expect is the expected contents. Like the Content it can be a
  nil, string, array, or map. A nil value indicates no checking of
  the response is needed. Strings are used mostly for fetched HTML
  while the normal JSON responses are converted to a genereric
  map[string]interface{} and compared to the Expect value.

  The rules for comparison are:

   1) Any element in the Expect value must be present in the response.

   2) Elements in the response not in the Expect value are ignored.

   3) If the Expect value is a string that starts and ends with a '/'
      character the string is assumed to be a regular expression is
      matched against the string in the reponse or the response is
      converted to a strings using fmt.Sprintf "%v" and then compared.

   4) Maps and arrays are followed recursively.

Constants

const (

    // Debug for debug logging.
    Debug = "\x1b[35m" // dark cyan
)

type Runner

Runner runs UseCases. It provide focal point for an assembly of use case tests. Test output is feature based. A flag is available for each category of output.

type Runner struct {

    // Server is the host and port of a URL. Example: http://localhost:8080
    Server string

    // Base of the URL path. By convention this is usually "/graphql".
    Base string

    // ShowComments if true will cause comments to be printed during a run.
    ShowComments bool

    // ShowRequests if true will cause request URL and content to be printed
    // during a run.
    ShowRequests bool

    // ShowResponses if true will cause response JSON to be printed during a run.
    ShowResponses bool

    // NoColor if true turns off colorized output.
    NoColor bool

    // Indent is the number of spaces to indent JSON resonses. If 0 no
    // modification to responses are made otherwise the JSON is unmarshalled
    // and re-marshalled with an indentation.
    Indent int

    // UseCases to run.
    UseCases []*UseCase

    // Writer is an alternate io.Writer that will be used in place of writing
    // to Stdout when logging if not nil.
    Writer io.Writer
}

func (*Runner) JSON

func (r *Runner) JSON(indents ...int) []byte

func (*Runner) Log

func (r *Runner) Log(color string, format string, args ...interface{})

Log output for one of the categories.

func (*Runner) Native

func (r *Runner) Native() interface{}

Native version of the Runner. Used for JSON() which is mostly for debugging.

func (*Runner) Run

func (r *Runner) Run() (err error)

Run the usecases.

func (*Runner) String

func (r *Runner) String() string

type Step

Step is a step in a use case. Each describes how an HTTP request will be sent to a GraphQL server and what to do with the response.

type Step struct {

    // Label of the step as a way to refer to the step.
    Label string

    // Comment describes the step.
    Comment string

    // Path is the path part of the URL that will be used in the request to
    // the GraphQL server. The path is assumed to be relative to the base URL
    // unless it starts with a '/' character.
    Path string

    // Content is the content when using a POST request. If the Content is not
    // empty then a POST is used. If it is empty then a GET request is made to
    // the server. The string should be either GraphQL or the HTTP GET format.
    Content string

    // UseJSON if true and a POST request then the JSON format will be used
    // with a COntent-Type of "applicaiton/json" otherwise the Content will be
    // sent as GraphQL with a Content-Type of "application/graphql".
    UseJSON bool

    // Remember in a series of steps it is often helpful to remember values
    // from earlier steps. The Remember map describes what to remember and
    // what key to store that value in. In the map, the keys are the keys for
    // the memory cache while the Remember map values are the path to the
    // value to remember. The path is a simple dot delimited path. It is not a
    // full JSON path (maybe in the future).
    Remember map[string]string

    // Op is the operation to include in either the URL query or as a value
    // for the 'operationName' if using JSON in the Content.
    Op string

    // Vars are the variables to be passed along with a GraphQL request. They
    // are appended to the URL or added to the JSON 'variables' element if the
    // POST contents is JSON. The values in the Vars map can be either a value
    // or a string that begines with a '$' which indicates a rmembered value
    // should be used instead.
    Vars map[string]interface{}

    // SortBy are the sort keys for the result. Depending on the
    // implementation of the GraphQL server, the order of returned objects may
    // not be consistent. To make it easier to use in testing the SortBy keys
    // are the paths to arrays while the value for the keys are the attributes
    // to sort on.
    SortBy map[string]string

    // Expect is the expected contents. Like the Content it can be a nil, string,
    // array, or map. A nil value indicates no checking of the response is
    // needed. Strings are used mostly for fetched HTML while the normal JSON
    // responses are converted to a genereric map[string]interface{} and
    // compared to the Expect value.
    //
    // The rules for comparison are:
    //   1) Any element in the Expect value must be present in the response.
    //   2) Elements in the response not in the Expect value are ignored.
    //   3) If the Expect value is a string that starts and ends with a '/'
    //      character the string is assumed to be a regular expression is
    //      matched against the string in the reponse or the response is
    //      converted to a strings using fmt.Sprintf "%v" and then compared.
    //   4) Maps and arrays are followed recursively.
    Expect interface{}
}

func (*Step) Execute

func (s *Step) Execute(uc *UseCase) error

Execute the step using the information in the provided use case such as remembered values and base URL.

func (*Step) Native

func (s *Step) Native() interface{}

func (*Step) Set

func (s *Step) Set(data interface{}) (err error)

type UseCase

UseCase encapsulates a use case composed of multiple steps. The use case can be read from a JSON file in which case the Filepath member will be set.

type UseCase struct {

    // Comment is the description of the use case.
    Comment string

    // Filepath is the path to the file that the use case was read from or
    // will be written to.
    Filepath string

    // Steps are the steps to be taken in the use case.
    Steps []*Step
    // contains filtered or unexported fields
}

func NewUseCase

func NewUseCase(filepath string) (uc *UseCase, err error)

NewUseCase creates a new UseCase from a file.

func (*UseCase) JSON

func (uc *UseCase) JSON(indents ...int) []byte

func (*UseCase) Native

func (uc *UseCase) Native() interface{}

func (*UseCase) Run

func (uc *UseCase) Run(r *Runner) (err error)

func (*UseCase) String

func (uc *UseCase) String() string

Subdirectories

Name Synopsis
..
gtt