test

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: MIT Imports: 9 Imported by: 0

README

Testing

This subpackage provides a test suite for resources.

Single Tests

The Handler provides a way to execute single test cases. This may be useful if you need the full control over the test flow.

package main

import (
	"testing"
	
	"pkg.loki.codes/concourse-resource/test"

	
	"your/resource/implementation"
)

func TestResource_Check(t *testing.T) {
	h := test.NewHandler[implementation.Config](t, implementation.New)
	testCase := test.CheckCase[implementation.Config]{
		Case: test.Case{
			ShouldFail:  false,
			ErrorString: "" /* of ShouldFail is true, compare against this error string */,
			Validation:  nil /* optional validation function */,
		},
		Input: test.CheckInput[implementation.Config]{
			Source:  implementation.Config{/* config */},
			Version: /* your version definition */,
		},
		Output: /* the expected output */,
	}
	h.RunCheck(testCase)
}

Multiple Tests

For convenience, a suite.Suit is provided that tests all resource functions.

package main

import (
	"testing"

	"github.com/stretchr/testify/suite"
	"pkg.loki.codes/concourse-resource/test"

	"your/resource/implementation"
)

func TestSuite(t *testing.T) {
	s := NewSuite[implementation.Config](implementation.New)

	s.SetInCases(map[string]InCase[implementation.Config]{
		"default": test.CheckCase[implementation.Config]{
			Case: test.Case{
				ShouldFail:  false,
				ErrorString: "" /* of ShouldFail is true, compare against this error string */,
				Validation:  nil /* optional validation function */,
			},
			Input: test.CheckInput[implementation.Config]{
				Source:  implementation.Config{/* config */},
				Version: /* your version definition */,
			},
			Output: /* the expected output */,
		},
	})
	suite.Run(t, s)
}

Examples

Have a look at the *_test.go files for more details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Case

type Case struct {
	ShouldFail  bool                                               // defines if the testcase should fail
	ErrorString string                                             // an optional error message to with
	Validation  func(assert *assert.Assertions, resource any) bool // an optional validation function
}

Case defines the input and expected result of a single test

type CheckCase

type CheckCase[cfgType any] struct {
	Case
	Input  CheckInput[cfgType]
	Output types.CheckOutput
}

CheckCase defines a testcase for the `check` action

type CheckInput

type CheckInput[T any] struct {
	Source  T
	Version any
}

CheckInput is a dummy input for a `check` test

type ConfigCase

type ConfigCase[cfgType any] struct {
	Case
	Input cfgType
}

ConfigCase defines a testcase for the construction of a new resource

type Handler

type Handler[T any] struct {
	// contains filtered or unexported fields
}

A Handler for your tests.

func NewHandler

func NewHandler[T any](t *testing.T, constructor resource.NewResource[T]) *Handler[T]

NewHandler creates a new testing Handler

func (*Handler[T]) RunCheck

func (h *Handler[T]) RunCheck(c CheckCase[T]) bool

RunCheck executes a single CheckCase

func (*Handler[T]) RunConfig

func (h *Handler[T]) RunConfig(c ConfigCase[T]) bool

RunConfig executes a single ConfigCase

func (*Handler[T]) RunIn

func (h *Handler[T]) RunIn(c InCase[T]) bool

RunIn executes a single InCase

func (*Handler[T]) RunOut

func (h *Handler[T]) RunOut(c OutCase[T]) bool

RunOut executes a single OutCase

type InCase

type InCase[cfgType any] struct {
	Case
	Input  InInput[cfgType]
	Output types.InOutput
}

InCase defines a testcase for the `in` action

type InInput

type InInput[cfgType any] struct {
	Source  cfgType
	Version any
	Params  any
}

InInput is a dummy input for a `in` test

type MockCheckResource

type MockCheckResource interface {
	types.InResource
	ValidateCheck(assert *assert.Assertions, expectedVersion any) bool
}

type MockInResource

type MockInResource interface {
	types.InResource
	ValidateIn(assert *assert.Assertions, expectedVersion, expectedParams any) bool
}

type MockOutResource

type MockOutResource interface {
	types.InResource
	ValidateOut(assert *assert.Assertions, expectedParams any) bool
}

type MockResource

type MockResource[cfgType any] interface {
	Validate(assert *assert.Assertions, expected cfgType) bool
}

type OutCase

type OutCase[cfgType any] struct {
	Case
	Input  OutInput[cfgType]
	Output types.OutOutput
}

OutCase defines a testcase for the `out` action

type OutInput

type OutInput[cfgType any] struct {
	Source cfgType
	Params any
}

OutInput is a dummy input for a `out` test

type ResourceTestSuite

type ResourceTestSuite[cfgType any] struct {
	suite.Suite
	// contains filtered or unexported fields
}

ResourceTestSuite provides the ability to run multiple tests, simply by providing maps of test cases.

If no case is given for a test type, the subtest will return a SUCCESS status

func NewSuite

func NewSuite[cfgType any](constructor resource.NewResource[cfgType]) *ResourceTestSuite[cfgType]

NewSuite creates a new suite.Suite

func (*ResourceTestSuite[cfgType]) SetCheckCases

func (s *ResourceTestSuite[cfgType]) SetCheckCases(checkCases map[string]CheckCase[cfgType])

SetCheckCases sets the list of CheckCase tests

func (*ResourceTestSuite[cfgType]) SetConfigCases

func (s *ResourceTestSuite[cfgType]) SetConfigCases(configCases map[string]ConfigCase[cfgType])

SetConfigCases sets the list of ConfigCase tests

func (*ResourceTestSuite[cfgType]) SetInCases

func (s *ResourceTestSuite[cfgType]) SetInCases(inCases map[string]InCase[cfgType])

SetInCases sets the list of InCase tests

func (*ResourceTestSuite[cfgType]) SetOutCases

func (s *ResourceTestSuite[cfgType]) SetOutCases(outCases map[string]OutCase[cfgType])

SetOutCases sets the list of OutCase tests

func (*ResourceTestSuite[cfgType]) SetupSuite

func (s *ResourceTestSuite[cfgType]) SetupSuite()

func (*ResourceTestSuite[cfgType]) TestCheck

func (s *ResourceTestSuite[cfgType]) TestCheck()

func (*ResourceTestSuite[cfgType]) TestConfig

func (s *ResourceTestSuite[cfgType]) TestConfig()

func (*ResourceTestSuite[cfgType]) TestIn

func (s *ResourceTestSuite[cfgType]) TestIn()

func (*ResourceTestSuite[cfgType]) TestOut

func (s *ResourceTestSuite[cfgType]) TestOut()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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