graphqlfixture

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2021 License: MIT Imports: 12 Imported by: 0

README

graphqlfixture

A test utility for fixture creation and teardown using GraphQL.

When creating fixtures for tests, we often need to know the exact DB ID that's generated in order to:

  • use that DB ID in subsequent fixture creations (e.g., subsequent fixture uses that DB ID as foreign key).
  • use that DB ID in tests (e.g., query by ID)
  • assert that DB ID as part of object comparison in tests (optional)

This utility tries to address this need, especially in the context of hasura or GraphQL tests. One particular limitation is the fixtures created are actually persisted into (cannot take advantage of the DB rollback), thus requires explicit "teardown" step.

Example

See example code and steps to run it.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fixture

type Fixture struct {
	Setup    string            // the graphql to seed the fixture (expect mutation.. could be query too? to just get some existing data, e.g., max of something)
	Captors  map[string]string // directives for capturing data from the setup response: key = captor name, the "logical name" of the captured value, value = the jsonpath ino the response to extract the value
	Teardown *string           // the graphql to remove the seeded fixture (expect delete mutation). optional, if no new fixture is created during setup.
	// contains filtered or unexported fields
}

Fixture contains the setup, teardown logic for a piece of fixtures, and the data needs to be extracted (captured) from the fixture, e.g., IDs.

type Fixtures

type Fixtures struct {
	Fixtures []Fixture // a list of fixtures, to be setup in this sequence, and torn down in the reverse sequence
	// contains filtered or unexported fields
}

func (*Fixtures) Get

func (fs *Fixtures) Get(captorName string) (interface{}, error)

Get returns the raw data given the captorName

func (*Fixtures) GetAndParse

func (fs *Fixtures) GetAndParse(captorName string, value interface{}) error

Get parses the data retrieved by given captorName into the desired value type

func (*Fixtures) Logs

func (fs *Fixtures) Logs() []string

Logs return logs

func (*Fixtures) Parse

func (fs *Fixtures) Parse()

Parse does a few validations and parsing in the list of fixtures: - setup and teardown graphql can pass the graphql parser (at least syntactically correct),

  • and extract the graphql variables (whose values will be filled from captures) at the same time

- no duplicates in captor names across all fixtures - captor name used in a fixture's setup must already be "captured" in previous fixture's captors - captor name used in a fixture's teardown must already be "captured" in previous + current fixture's captors The result of parsing is in fs.parsed and fs.parseErr

func (*Fixtures) Setup

func (fs *Fixtures) Setup(ctx context.Context, graphqlClient *graphqlclient.Client) error

Setup calls each fixture's Setup (graphql call) in sequence, and captures the values from the responses.

func (*Fixtures) SetupUntil

func (fs *Fixtures) SetupUntil() *int

SetupUntil returns setupUntilIdx (can be nil)

func (*Fixtures) Teardown

func (fs *Fixtures) Teardown(ctx context.Context, graphqlClient *graphqlclient.Client) error

Teardown calls each fixture's Teardown (graphql call) in reverse sequence.

func (*Fixtures) TeardownUntil

func (fs *Fixtures) TeardownUntil() *int

TeardownUntil returns teardownUntilIdx (can be nil)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL