contracts

package
v0.51.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package specs

Summary

This package implements generic CRUD operation related testing specifications that commonly appears upon interacting with external resources.

Reason for separate pkg from resources pkg

This package originally was part of the resources pkg, but package "testing" has side effects on import. In order to avoid force the import on the implementations, the specs had to be extracted into a separate package. Since testing#T.Run is heavily used, abstracting testing.T away is difficult.

Minimum Requirement from Resource point of view

In order to make this package work, you have to implement the TestMinimumRequirementsWithExampleEntities specification. Most of the other Resource specs specification depends on the Resource specs mentioned in the min requirement specification. Keep in mind, that you have no guarantee on your Resource content during test execution, because some specification may alter the content of the Resource (db), or delete from it. If you need specific data in the Resource you want to test with, you must ensure in the test execution that such Context is correctly provisioned, and after test execution, cleaned up. If you use such data-set in a external Resource that needs to be kept intact, I advise you to use separate environments for test execution and manual testing.

Requirement from Business Entities

This package depends on a fact that there is a string field ID in a business entity struct, or at least a tag `ext:"ID"`. This allows the package to create specifications that assumes, that the ID field links the EntityTypeName structure to an external Resource object. The Resource specs package doesn't care about the content of the ID string field, and don't have assumptions other than the existence of the field ID on a struct

Index

Constants

View Source
const ErrIDRequired frameless.Error = `` /* 174-byte string literal not displayed */

Variables

View Source
var AsyncTester = testcase.Retry{Strategy: Waiter}
View Source
var Waiter = testcase.Waiter{
	WaitDuration: time.Millisecond,
	WaitTimeout:  5 * time.Second,
}

Functions

func CreateEntity

func CreateEntity(tb testing.TB, subject CRD, ctx context.Context, ptr interface{})

func CreatePTR added in v0.43.1

func CreatePTR(ff frameless.FixtureFactory, T T) interface{}

func DeleteAllEntity

func DeleteAllEntity(tb testing.TB, subject CRD, ctx context.Context)

func DeleteEntity

func DeleteEntity(tb testing.TB, subject CRD, ctx context.Context, ent interface{})

func HasEntity

func HasEntity(tb testing.TB, subject frameless.Finder, ctx context.Context, ent interface{})

func HasID

func HasID(tb testing.TB, ent interface{}) (id interface{})

func IsAbsent

func IsAbsent(tb testing.TB, T T, subject frameless.Finder, ctx context.Context, id interface{})

func IsFindable

func IsFindable(tb testing.TB, T T, subject frameless.Finder, ctx context.Context, id interface{}) interface{}

func UpdateEntity

func UpdateEntity(tb testing.TB, subject interface {
	frameless.Finder
	frameless.Updater
	frameless.Deleter
}, ctx context.Context, ptr interface{})

Types

type CRD

CRD is the minimum requirements to write easily behavioral specification for a resource.

type Creator

type Creator struct {
	T
	Subject        func(testing.TB) CRD
	Context        func(testing.TB) context.Context
	FixtureFactory func(testing.TB) frameless.FixtureFactory
}

func (Creator) Benchmark

func (c Creator) Benchmark(b *testing.B)

func (Creator) Spec added in v0.43.1

func (c Creator) Spec(s *testcase.Spec)

func (Creator) Test

func (c Creator) Test(t *testing.T)

type Deleter

type Deleter struct {
	T
	Subject        func(testing.TB) CRD
	Context        func(testing.TB) context.Context
	FixtureFactory func(testing.TB) frameless.FixtureFactory
}

func (Deleter) Benchmark

func (c Deleter) Benchmark(b *testing.B)

func (Deleter) Spec added in v0.43.1

func (c Deleter) Spec(s *testcase.Spec)

func (Deleter) Test

func (c Deleter) Test(t *testing.T)

type FindOne

type FindOne struct {
	T
	Subject        func(testing.TB) CRD
	Context        func(testing.TB) context.Context
	FixtureFactory func(testing.TB) frameless.FixtureFactory
	// MethodName is the name of the test subject QueryOne method of this contract specification.
	MethodName string
	// ToQuery takes an entity ptr and returns with a closure that has the knowledge about how to query on the Subject resource to find the entity.
	//
	// By convention, any preparation action that affect the Storage must take place prior to returning the closure.
	// The QueryOne closure should only have the Method call with the already mapped values.
	// ToQuery will be evaluated in the beginning of the testing,
	// and executed after all the test Context preparation is done.
	ToQuery func(tb testing.TB, resource interface{}, ent T) QueryOne
	// Specify allow further specification describing for a given FindOne query function.
	// If none specified, this field will be ignored
	Specify func(testing.TB)
}

func (FindOne) Benchmark

func (c FindOne) Benchmark(b *testing.B)

func (FindOne) Spec

func (c FindOne) Spec(s *testcase.Spec)

func (FindOne) String added in v0.46.0

func (c FindOne) String() string

func (FindOne) Test

func (c FindOne) Test(t *testing.T)

type Finder

type Finder struct {
	T
	Subject        func(testing.TB) CRD
	Context        func(testing.TB) context.Context
	FixtureFactory func(testing.TB) frameless.FixtureFactory
}

func (Finder) Benchmark

func (c Finder) Benchmark(b *testing.B)

func (Finder) Spec

func (c Finder) Spec(s *testcase.Spec)

func (Finder) Test

func (c Finder) Test(t *testing.T)

type FixtureFactory

type FixtureFactory struct {
	T       interface{}
	Subject func(testing.TB) frameless.FixtureFactory
	Context func(testing.TB) context.Context
}

func (FixtureFactory) Benchmark added in v0.49.0

func (c FixtureFactory) Benchmark(b *testing.B)

func (FixtureFactory) Spec added in v0.49.0

func (c FixtureFactory) Spec(s *testcase.Spec)

func (FixtureFactory) String added in v0.49.0

func (c FixtureFactory) String() string

func (FixtureFactory) Test added in v0.49.0

func (c FixtureFactory) Test(t *testing.T)

type Interface

type Interface interface {
	testcase.Contract
	testcase.OpenContract
}

Interface represent a resource specification also known as "contract".

The main goal of a resource Spec is to introduce dependency injection pattern at logical level between consumers and suppliers. In other words any expectations from a consumer/interactor/use-case towards a used dependency should be defined in a contract. This allows architecture flexibility since the expectations not bound to a certain technology, but purely high level and as such can be implemented in various ways.

Using resource Spec also force the writer of the Spec to keep things at high level and only focus on the expected behavior, instead of going into implementation details.

type MetaAccessor added in v0.43.1

type MetaAccessor struct {
	T, V           T
	Subject        func(testing.TB) MetaAccessorSubject
	Context        func(testing.TB) context.Context
	FixtureFactory func(testing.TB) frameless.FixtureFactory
}

func (MetaAccessor) Benchmark added in v0.43.1

func (c MetaAccessor) Benchmark(b *testing.B)

func (MetaAccessor) Spec added in v0.43.1

func (c MetaAccessor) Spec(s *testcase.Spec)

func (MetaAccessor) Test added in v0.43.1

func (c MetaAccessor) Test(t *testing.T)

type MetaAccessorBasic added in v0.43.1

type MetaAccessorBasic struct {
	// V is the value T type that can be set and looked up with frameless.MetaAccessor.
	V              T
	Subject        func(testing.TB) frameless.MetaAccessor
	FixtureFactory func(testing.TB) frameless.FixtureFactory
}

func (MetaAccessorBasic) Benchmark added in v0.43.1

func (c MetaAccessorBasic) Benchmark(b *testing.B)

func (MetaAccessorBasic) Spec added in v0.43.1

func (c MetaAccessorBasic) Spec(s *testcase.Spec)

func (MetaAccessorBasic) Test added in v0.43.1

func (c MetaAccessorBasic) Test(t *testing.T)

type MetaAccessorPublisher added in v0.43.1

type MetaAccessorPublisher struct {
	T, V           T
	Subject        func(testing.TB) MetaAccessorSubject
	Context        func(testing.TB) context.Context
	FixtureFactory func(testing.TB) frameless.FixtureFactory
}

func (MetaAccessorPublisher) Benchmark added in v0.43.1

func (c MetaAccessorPublisher) Benchmark(b *testing.B)

func (MetaAccessorPublisher) Spec added in v0.43.1

func (c MetaAccessorPublisher) Spec(s *testcase.Spec)

func (MetaAccessorPublisher) Test added in v0.43.1

func (c MetaAccessorPublisher) Test(t *testing.T)

type MetaAccessorSubject added in v0.43.1

type MetaAccessorSubject struct {
	frameless.MetaAccessor
	CRD
	frameless.Publisher
}

type OnePhaseCommitProtocol

type OnePhaseCommitProtocol struct {
	T
	Subject        func(testing.TB) (frameless.OnePhaseCommitProtocol, CRD)
	Context        func(testing.TB) context.Context
	FixtureFactory func(testing.TB) frameless.FixtureFactory
}

func (OnePhaseCommitProtocol) Benchmark

func (c OnePhaseCommitProtocol) Benchmark(b *testing.B)

func (OnePhaseCommitProtocol) Spec

func (OnePhaseCommitProtocol) Test

func (c OnePhaseCommitProtocol) Test(t *testing.T)

type Publisher added in v0.41.0

type Publisher struct {
	T
	Subject        func(testing.TB) PublisherSubject
	Context        func(testing.TB) context.Context
	FixtureFactory func(testing.TB) frameless.FixtureFactory
}

func (Publisher) Benchmark added in v0.41.0

func (c Publisher) Benchmark(b *testing.B)

func (Publisher) Spec added in v0.41.0

func (c Publisher) Spec(s *testcase.Spec)

func (Publisher) String added in v0.41.0

func (c Publisher) String() string

func (Publisher) Test added in v0.41.0

func (c Publisher) Test(t *testing.T)

type PublisherSubject added in v0.41.0

type PublisherSubject interface {
	CRD
	frameless.Publisher
}

type QueryOne

type QueryOne func(tb testing.TB, ctx context.Context, ptr T) (found bool, err error)

QueryOne is the generic representation of a query that meant to find one result. It is really similar to resources.Finder#FindByID, with the exception that the closure meant to know the query method name on the subject and the inputs it requires.

QueryOne is generated through ToQuery factory function in FindOne resource contract specification.

type T

type T = frameless.T

type Updater

type Updater struct {
	T              T
	Subject        func(testing.TB) UpdaterSubject
	Context        func(testing.TB) context.Context
	FixtureFactory func(testing.TB) frameless.FixtureFactory
}

Updater will request an update for a wrapped entity object in the Resource

func (Updater) Benchmark

func (c Updater) Benchmark(b *testing.B)

func (Updater) Spec added in v0.43.1

func (c Updater) Spec(s *testcase.Spec)

func (Updater) Test

func (c Updater) Test(t *testing.T)

type UpdaterSubject

type UpdaterSubject interface {
	CRD
	frameless.Updater
}

Jump to

Keyboard shortcuts

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