contracts

package
v0.38.1 Latest Latest
Warning

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

Go to latest
Published: May 25, 2021 License: Apache-2.0 Imports: 15 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 DeleteAllEntity

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

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
	FixtureFactory
}

func (Creator) Benchmark

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

func (Creator) Test

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

type CreatorPublisher

type CreatorPublisher struct {
	T
	Subject        func(testing.TB) CreatorPublisherSubject
	FixtureFactory FixtureFactory
}

func (CreatorPublisher) Benchmark

func (spec CreatorPublisher) Benchmark(b *testing.B)

func (CreatorPublisher) Spec

func (spec CreatorPublisher) Spec(tb testing.TB)

func (CreatorPublisher) Test

func (spec CreatorPublisher) Test(t *testing.T)

type CreatorPublisherSubject

type CreatorPublisherSubject interface {
	CRD
	frameless.CreatorPublisher
}

type Deleter

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

func (Deleter) Benchmark

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

func (Deleter) Test

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

type DeleterPublisher

type DeleterPublisher struct {
	T
	Subject        func(testing.TB) DeleterPublisherSubject
	FixtureFactory FixtureFactory
}

func (DeleterPublisher) Benchmark

func (spec DeleterPublisher) Benchmark(b *testing.B)

func (DeleterPublisher) Test

func (spec DeleterPublisher) Test(t *testing.T)

type DeleterPublisherSubject

type DeleterPublisherSubject interface {
	CRD
	frameless.DeleterPublisher
}

type FindOne

type FindOne struct {
	T
	Subject func(testing.TB) CRD
	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 (spec FindOne) Benchmark(b *testing.B)

func (FindOne) Spec

func (spec FindOne) Spec(tb testing.TB)

func (FindOne) Test

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

type Finder

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

func (Finder) Benchmark

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

func (Finder) Spec

func (spec Finder) Spec(tb testing.TB)

func (Finder) Test

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

type FixtureFactory

type FixtureFactory interface {
	// Create create a newEntity struct instance based on the received input struct type.
	// Create also populate the struct field with dummy values.
	// It is expected that the newly created fixture will have no content for extID field.
	//Create(testing.TB, context.Context, any) any
	Create(T interface{}) (ptr interface{})
	// Context able to provide the specs with a Context object for a certain entity Type.
	Context() (ctx context.Context)
}

type FixtureFactorySpec

type FixtureFactorySpec struct {
	Type interface{}
	FixtureFactory
}

func (FixtureFactorySpec) Benchmark

func (spec FixtureFactorySpec) Benchmark(b *testing.B)

func (FixtureFactorySpec) Test

func (spec FixtureFactorySpec) Test(t *testing.T)

type Interface

type Interface = testcase.Contract

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 OnePhaseCommitProtocol

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

func (OnePhaseCommitProtocol) Benchmark

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

func (OnePhaseCommitProtocol) Context

func (spec OnePhaseCommitProtocol) Context() context.Context

func (OnePhaseCommitProtocol) Spec

func (spec OnePhaseCommitProtocol) Spec(tb testing.TB)

func (OnePhaseCommitProtocol) Test

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

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
	Subject func(testing.TB) UpdaterSubject
	FixtureFactory
}

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

func (Updater) Benchmark

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

func (Updater) Test

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

type UpdaterPublisher

type UpdaterPublisher struct {
	T
	Subject func(testing.TB) UpdaterPublisherSubject
	FixtureFactory
}

func (UpdaterPublisher) Benchmark

func (spec UpdaterPublisher) Benchmark(b *testing.B)

func (UpdaterPublisher) Spec

func (spec UpdaterPublisher) Spec(tb testing.TB)

func (UpdaterPublisher) Test

func (spec UpdaterPublisher) Test(t *testing.T)

type UpdaterPublisherSubject

type UpdaterPublisherSubject interface {
	CRD
	frameless.Updater
	frameless.UpdaterPublisher
}

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