testfixture

package
v0.0.0-...-bf90f01 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomizeEntityFunc

type CustomizeEntityFunc func(fxt *TestFixture, idx int) error

type CustomizeNamespaceFunc

type CustomizeNamespaceFunc CustomizeEntityFunc

CustomizeNamespaceFunc is directly compatible with CustomizeEntityFunc but it can only be used for the Namespaces() recipe-function.

type CustomizeTenantFunc

type CustomizeTenantFunc CustomizeEntityFunc

CustomizeTenantFunc is directly compatible with CustomizeEntityFunc but it can only be used for the Tenants() recipe-function.

type NamespacesModifier

type NamespacesModifier func(*tenant.Namespace)

type NamespacesSetter

type NamespacesSetter struct {
	// contains filtered or unexported fields
}

func AddDefaultNamespaces

func AddDefaultNamespaces() *NamespacesSetter

func AddNamespaces

func AddNamespaces(envTypes ...environment.Type) *NamespacesSetter

func (*NamespacesSetter) MasterURL

func (m *NamespacesSetter) MasterURL(masterURL string) *NamespacesSetter

func (*NamespacesSetter) Outdated

func (m *NamespacesSetter) Outdated() *NamespacesSetter

func (*NamespacesSetter) State

type RecipeFunction

type RecipeFunction func(fxt *TestFixture) error

A RecipeFunction tells the test fixture to create n objects of a given kind. You can pass in customize-entity-functions in order to manipulate the objects before they get created.

func Namespaces

func Namespaces(n int, fns ...CustomizeNamespaceFunc) RecipeFunction

Namespaces tells the test fixture to create at least n namespace objects. See also the Tenants() function for more general information on n and fns.

When called in NewFixture() this function will call also call

Tenants(1)

but with NewFixtureIsolated(), no other objects will be created.

func Tenants

func Tenants(n int, fns ...CustomizeTenantFunc) RecipeFunction

Tenants tells the test fixture to create at least n tenant objects.

If called multiple times with differently n's, the biggest n wins. All customize-entitiy-functions fns from all calls will be respected when creating the test fixture.

Here's an example how you can create 42 tenants and give them a numbered user name like "John Doe 0", "John Doe 1", and so forth:

Tenants(42, func(fxt *TestFixture, idx int) error{
    fxt.Tenants[idx].Email = "JaneDoe" + strconv.FormatInt(idx, 10) + "@foo.com"
    return nil
})

Notice that the index idx goes from 0 to n-1 and that you have to manually lookup the object from the test fixture. The Tenant object referenced by

fxt.Tenants[idx]

is guaranteed to be ready to be used for creation. That means, you don't necessarily have to touch it to avoid unique key violation for example. This is totally optional.

type TenantModifier

type TenantModifier func(tnnt *tenant.Tenant)

func AddSpecificTenants

func AddSpecificTenants(modifiers ...TenantModifier) []TenantModifier

func AddTenants

func AddTenants(numberOfTenants int) []TenantModifier

func SingleWithName

func SingleWithName(name string) TenantModifier

func SingleWithNames

func SingleWithNames(name, baseName string) TenantModifier

type TestFixture

type TestFixture struct {
	Tenants    []*tenant.Tenant    // Tenants (if any) that were created for this test fixture.
	Namespaces []*tenant.Namespace // Namespaces (if any) that were created for this test fixture.
	// contains filtered or unexported fields
}

A TestFixture object is the result of a call to

NewFixture()

or

NewFixtureIsolated()

Don't create one on your own!

func FillDB

func FillDB(t *testing.T, db *gorm.DB, tenantModifiers []TenantModifier, mandatoryNssSetter *NamespacesSetter, otherNssSetters ...*NamespacesSetter) *TestFixture

func NewFixture

func NewFixture(db *gorm.DB, recipeFuncs ...RecipeFunction) (*TestFixture, error)

NewFixture will create a test fixture by executing the recipies from the given recipe functions. If recipeFuncs is empty, nothing will happen.

For example

NewFixture(db, Comments(100))

will create a work item (and everything required in order to create it) and author 100 comments for it. They will all be created by the same user if you don't tell the system to do it differently. For example, to create 100 comments from 100 different users we can do the following:

NewFixture(db, Tenants(100), Namespaces(100, func(fxt *TestFixture, idx int) error{
    fxt.Namespaces[idx].TenantID = fxt.Tenants[idx].ID
    return nil
}))

That will create 100 tenants and 100 namspaces and for each namespace we're using the ID of one of the tenants that have been created earlier. There's one important observation to make with this example: there's an order to how entities get created in the test fixture. That order is basically defined by the number of dependencies that each entity has. For example a tenant has no dependency, so it will be created first and then can be accessed safely by any of the other entity creation functions. A namespace for example depends on a tenant. The NewFixture function does take care of recursively resolving those dependencies first.

func NewFixtureIsolated

func NewFixtureIsolated(db *gorm.DB, setupFuncs ...RecipeFunction) (*TestFixture, error)

NewFixtureIsolated will create a test fixture by executing the recipies from the given recipe functions. If recipeFuncs is empty, nothing will happen.

The difference to the normal NewFixture function is that we will only create those object that where specified in the recipeFuncs. We will not create any object that is normally demanded by an object. For example, if you call

NewFixture(t, db, Namespaces(1))

you would (apart from other objects) get at least one namespace AND a tenant because that is needed to create a namespace. With

NewFixtureIsolated(t, db, Namespaces(2), Tenants(1))

on the other hand, we will only create a tenant, two namespaces for it, and nothing more.

func NewTestFixture

func NewTestFixture(t testing.TB, db *gorm.DB, recipeFuncs ...RecipeFunction) *TestFixture

NewTestFixture does the same as NewFixture except that it automatically fails the given test if the fixture could not be created correctly.

func (*TestFixture) Check

func (fxt *TestFixture) Check() error

Check runs all check functions that each recipe-function has registered to check that the amount of objects has been created that were demanded in the recipe function.

In this example

fxt, _:= NewFixture(db, Namespaces(2))
err = fxt.Check()

err will only be nil if at least namespaces have been created and all of the dependencies that a namespace requires. Look into the documentation of each recipe-function to find out what dependencies each entity has.

Notice, that check is called at the end of NewFixture() and its derivatives, so if you don't mess with the fixture after it was created, there's no need to call Check() again.

Jump to

Keyboard shortcuts

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