fabric8-wit: github.com/fabric8-services/fabric8-wit/test/testfixture Index | Files

package testfixture

import "github.com/fabric8-services/fabric8-wit/test/testfixture"

Package testfixture helps you bootstrap test scenarios very effectively.

Take a look at the

NewFixture()

function to learn how to use this package.

Index

Package Files

customize_entity_funcs.go doc.go environments.go make_functions.go query_functions.go recipe_funcs.go testfixture.go

type CustomizeAreaFunc Uses

type CustomizeAreaFunc CustomizeEntityFunc

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

func PlaceAreaUnderRootArea Uses

func PlaceAreaUnderRootArea() CustomizeAreaFunc

PlaceAreaUnderRootArea when asking for more than one area, all but the first one will be placed under the first area (aka root area).

type CustomizeCodebaseFunc Uses

type CustomizeCodebaseFunc CustomizeEntityFunc

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

type CustomizeCommentFunc Uses

type CustomizeCommentFunc CustomizeEntityFunc

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

type CustomizeEntityFunc Uses

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

A CustomizeEntityFunc acts as a generic function to the various recipe-functions (e.g. Identities(), Spaces(), etc.). The current test fixture is given with the fxt argument and the position of the object that will be created next is indicated by the index idx. That index can be used to look up e.g. a space with

s := fxt.Spaces[idx]

That space s will be a ready-to-create space object on that you can modify to your liking.

Notice that when you lookup objects in the test fixture, you can only safely access those object on which the entity depends, because those are guaranteed to be already created. For example when you try to access a work item type from a customize-entity-function, it will not be very useful:

NewFixture(db, WorkItemTypes(1), Spaces(1, func(fxt *TestFixture, idx int) error{
    fmt.Println(fxt.WorkItemType[0].ID) // WARNING: not yet set
    return nil
}))

On the other hand, you can safely lookup the space ID when you're in the customize-entity-function for a work item:

NewFixture(db, WorkItem(1, func(fxt *TestFixture, idx int) error{
    fmt.Println(fxt.Space[0].ID) // safe to access
    return nil
}))

Notice that you can do all kinds of distribution related functions in a customize-entitiy-function. For example, you can control which identity owns a space or define what work item type each work item shall have. If not otherwise specified (e.g. as for WorkItemLinks()) we use a straight forward approach. So for example if you write

NewFixture(t, db, Identities(10), Spaces(100))

then we will create 10 identites and 100 spaces and the owner of all spaces will be identified with the ID of the first identity:

fxt.Identities[0].ID

If you want a different distribution, you can create your own customize- entitiy-function (see Identities() for an example).

If you for some error reason you want your test fixture creation to fail you can use the fxt.T test instance:

NewFixture(db, Identities(100, func(fxt *TestFixture, idx int) error{
    return errors.New("some test failure reason")
}))

type CustomizeIdentityFunc Uses

type CustomizeIdentityFunc CustomizeEntityFunc

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

func SetIdentityUsernames Uses

func SetIdentityUsernames(usernames ...string) CustomizeIdentityFunc

SetIdentityUsernames takes the given usernames and uses them during creation of identities. The length of requested work item types and the number of usernames must match or the NewFixture call will return an error.

type CustomizeIterationFunc Uses

type CustomizeIterationFunc CustomizeEntityFunc

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

func PlaceIterationUnderRootIteration Uses

func PlaceIterationUnderRootIteration() CustomizeIterationFunc

PlaceIterationUnderRootIteration when asking for more than one iteration, all but the first one will be placed under the first iteration (aka root iteration).

func SetIterationNames Uses

func SetIterationNames(names ...string) CustomizeIterationFunc

SetIterationNames takes the given names and uses them during creation of iterations. The length of requested iterations and the number of names must match or the NewFixture call will return an error.

func UserActive Uses

func UserActive(active bool) CustomizeIterationFunc

UserActive ensures that all created iterations have the given user activation state

type CustomizeLabelFunc Uses

type CustomizeLabelFunc CustomizeEntityFunc

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

func SetLabelNames Uses

func SetLabelNames(names ...string) CustomizeLabelFunc

SetLabelNames takes the given names and uses them during creation of labels. The length of requested labels and the number of names must match or the NewFixture call will return an error.

type CustomizeSpaceFunc Uses

type CustomizeSpaceFunc CustomizeEntityFunc

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

type CustomizeTrackerFunc Uses

type CustomizeTrackerFunc CustomizeEntityFunc

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

type CustomizeWorkItemFunc Uses

type CustomizeWorkItemFunc CustomizeEntityFunc

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

func SetWorkItemField Uses

func SetWorkItemField(fieldName string, values ...interface{}) CustomizeWorkItemFunc

SetWorkItemField takes the given values and uses them during creation of work items to set field values. The length of requested work items and the number of values must match or the NewFixture call will return an error.

func SetWorkItemTitles Uses

func SetWorkItemTitles(titleStrings ...interface{}) CustomizeWorkItemFunc

SetWorkItemTitles takes the given titles and uses them during creation of work items. The length of requested work items and the number of titles must match or the NewFixture call will return an error.

type CustomizeWorkItemLinkCategoryFunc Uses

type CustomizeWorkItemLinkCategoryFunc CustomizeEntityFunc

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

type CustomizeWorkItemLinkFunc Uses

type CustomizeWorkItemLinkFunc CustomizeEntityFunc

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

type CustomizeWorkItemLinkTypeFunc Uses

type CustomizeWorkItemLinkTypeFunc CustomizeEntityFunc

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

func SetTopologies Uses

func SetTopologies(topologies ...link.Topology) CustomizeWorkItemLinkTypeFunc

SetTopologies takes the given topologies and uses them during creation of work item link types. The length of requested work item link types and the number of topologies must match or the NewFixture call will return an error.

func SetWorkItemLinkTypeNames Uses

func SetWorkItemLinkTypeNames(names ...string) CustomizeWorkItemLinkTypeFunc

SetWorkItemLinkTypeNames takes the given names and uses them during creation of work item link types. The length of requested work item link types and the number of names must match or the NewFixture call will return an error.

type CustomizeWorkItemTypeFunc Uses

type CustomizeWorkItemTypeFunc CustomizeEntityFunc

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

func SetWorkItemTypeNames Uses

func SetWorkItemTypeNames(names ...string) CustomizeWorkItemTypeFunc

SetWorkItemTypeNames takes the given names and uses them during creation of work item types. The length of requested work item types and the number of names must match or the NewFixture call will return an error.

type RecipeFunction Uses

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 Areas Uses

func Areas(n int, fns ...CustomizeAreaFunc) RecipeFunction

Areas tells the test fixture to create at least n area objects. See also the Identities() function for more general information on n and fns.

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

Spaces(1)

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

func Codebases Uses

func Codebases(n int, fns ...CustomizeCodebaseFunc) RecipeFunction

Codebases tells the test fixture to create at least n codebase objects. See also the Identities() function for more general information on n and fns.

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

Spaces(1)

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

func Comments Uses

func Comments(n int, fns ...CustomizeWorkItemFunc) RecipeFunction

Comments tells the test fixture to create at least n comment objects. See also the Identities() function for more general information on n and fns.

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

Identities(1)
WorkItems(1)

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

func CreateWorkItemEnvironment Uses

func CreateWorkItemEnvironment() RecipeFunction

CreateWorkItemEnvironment returns a higher level recipe function that contains a little bit more business logic compared to the plain recipe functions that only set up dependencies according to database needs. In this particular case, we create a root iteration and root area and make additional areas be child of the root instances.

func Identities Uses

func Identities(n int, fns ...CustomizeIdentityFunc) RecipeFunction

Identities tells the test fixture to create at least n identity 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 identites and give them a numbered user name like "John Doe 0", "John Doe 1", and so forth:

Identities(42, func(fxt *TestFixture, idx int) error{
    fxt.Identities[idx].Username = "Jane Doe " + strconv.FormatInt(idx, 10)
    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 identity object referenced by

fxt.Identities[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.

func Iterations Uses

func Iterations(n int, fns ...CustomizeIterationFunc) RecipeFunction

Iterations tells the test fixture to create at least n iteration objects. See also the Identities() function for more general information on n and fns.

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

Spaces(1)

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

func Labels Uses

func Labels(n int, fns ...CustomizeLabelFunc) RecipeFunction

Labels tells the test fixture to create at least n label objects. See also the Identities() function for more general information on n and fns.

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

Spaces(1)

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

func Spaces Uses

func Spaces(n int, fns ...CustomizeSpaceFunc) RecipeFunction

Spaces tells the test fixture to create at least n space objects. See also the Identities() function for more general information on n and fns.

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

Identities(1)

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

func Trackers Uses

func Trackers(n int, fns ...CustomizeTrackerFunc) RecipeFunction

Trackers tells the test fixture to create at least n tracker objects. See also the Identities() function for more general information on n and fns.

func WorkItemLinkCategories Uses

func WorkItemLinkCategories(n int, fns ...CustomizeWorkItemLinkCategoryFunc) RecipeFunction

WorkItemLinkCategories tells the test fixture to create at least n work item link category objects. See also the Identities() function for more general information on n and fns.

No other objects will be created.

func WorkItemLinkTypes Uses

func WorkItemLinkTypes(n int, fns ...CustomizeWorkItemLinkTypeFunc) RecipeFunction

WorkItemLinkTypes tells the test fixture to create at least n work item link type objects. See also the Identities() function for more general information on n and fns.

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

Spaces(1)
WorkItemLinkCategories(1)

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

We've created these helper functions that you should have a look at if you want to implement your own re-usable customize-entity-functions:

SetTopologies(topology ...link.Topology)

The topology functions above are neat because you don't have to write a full function function yourself.

By default a call to

WorkItemLinkTypes(1)

equals

WorkItemLinkTypes(1, SetTopologies(link.TopologyTree))

because we automatically set the topology for each link type to be "tree".

func WorkItemLinks(n int, fns ...CustomizeWorkItemLinkFunc) RecipeFunction

WorkItemLinks tells the test fixture to create at least n work item link objects. See also the Identities() function for more general information on n and fns.

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

WorkItemLinkTypes(1)
WorkItems(2*n)

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

Notice, that we will create two times the number of work items of your requested links. The way those links will be created can for sure be influenced using a customize-entity-function; but by default we create each link between two distinct work items. That means, no link will include the same work item.

func WorkItemTypes Uses

func WorkItemTypes(n int, fns ...CustomizeWorkItemTypeFunc) RecipeFunction

WorkItemTypes tells the test fixture to create at least n work item type objects. See also the Identities() function for more general information on n and fns.

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

Spaces(1)

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

The work item type that we create for each of the n instances is always the same and it tries to be compatible with the planner item work item type by specifying the same fields.

func WorkItems Uses

func WorkItems(n int, fns ...CustomizeWorkItemFunc) RecipeFunction

WorkItems tells the test fixture to create at least n work item objects. See also the Identities() function for more general information on n and fns.

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

Spaces(1)
WorkItemTypes(1)
Identities(1)

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

Notice that the Number field of a work item is only set after the work item has been created, so any changes you make to

fxt.WorkItems[idx].Number

will have no effect.

type TestFixture Uses

type TestFixture struct {
    Identities             []*account.Identity          // Itentities (if any) that were created for this test fixture.
    Iterations             []*iteration.Iteration       // Iterations (if any) that were created for this test fixture.
    Areas                  []*area.Area                 // Areas (if any) that were created for this test fixture.
    Spaces                 []*space.Space               // Spaces (if any) that were created for this test fixture.
    Codebases              []*codebase.Codebase         // Codebases (if any) that were created for this test fixture.
    WorkItems              []*workitem.WorkItem         // Work items (if any) that were created for this test fixture.
    Comments               []*comment.Comment           // Comments (if any) that were created for this test fixture.
    WorkItemTypes          []*workitem.WorkItemType     // Work item types (if any) that were created for this test fixture.
    WorkItemLinkTypes      []*link.WorkItemLinkType     // Work item link types (if any) that were created for this test fixture.
    WorkItemLinkCategories []*link.WorkItemLinkCategory // Work item link categories (if any) that were created for this test fixture.
    WorkItemLinks          []*link.WorkItemLink         // Work item links (if any) that were created for this test fixture.
    Labels                 []*label.Label
    Trackers               []*remoteworkitem.Tracker // Remote work item tracker (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 NewFixture Uses

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, Identities(100), Comments(100, func(fxt *TestFixture, idx int) error{
    fxt.Comments[idx].Creator = fxt.Identities[idx].ID
    return nil
}))

That will create 100 identities and 100 comments and for each comment we're using the ID of one of the identities 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 an identity has no dependency, so it will be created first and then can be accessed safely by any of the other entity creation functions. A comment for example depends on a work item which itself depends on a work item type and a space. The NewFixture function does take care of recursively resolving those dependcies first.

If you just want to create 100 identities and 100 work items but don't care about resolving the dependencies automatically you can create the entities in isolation:

NewFixtureIsolated(db, Identities(100), Comments(100, func(fxt *TestFixture, idx int) error{
    fxt.Comments[idx].Creator = fxt.Identities[idx].ID
    fxt.Comments[idx].ParentID = someExistingWorkItemID
    return nil
}))

Notice that I manually have to specify the ParentID of the work comment then because we cannot automatically resolve to which work item we will attach the comment.

func NewFixtureIsolated Uses

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, WorkItems(1))

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

NewFixtureIsolated(t, db, Comments(2), WorkItems(1))

on the other hand, we will only create a work item, two comments for it, and nothing more. And for sure your test will fail if you do that because you need to specify a space ID and a work item type ID for the created work item:

NewFixtureIsolated(db, Comments(2), WorkItems(1, func(fxt *TestFixture, idx int) error{
  fxt.WorkItems[idx].SpaceID = someExistingSpaceID
  fxt.WorkItems[idx].WorkItemType = someExistingWorkItemTypeID
  return nil
}))

func NewTestFixture Uses

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 Uses

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, WorkItems(2))
err = fxt.Check()

err will only be nil if at least two work items have been created and all of the dependencies that a work item 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.

func (*TestFixture) IdentityByUsername Uses

func (fxt *TestFixture) IdentityByUsername(username string) *account.Identity

IdentityByUsername returns the first identity that has the given username (if any).

func (*TestFixture) IterationByName Uses

func (fxt *TestFixture) IterationByName(name string, spaceID ...uuid.UUID) *iteration.Iteration

IterationByName returns the first iteration that has the given name (if any). If you have iterations with the same name in different spaces you can also pass in one space ID to filter by space as well.

func (*TestFixture) LabelByName Uses

func (fxt *TestFixture) LabelByName(name string, spaceID ...uuid.UUID) *label.Label

LabelByName returns the first label that has the given name (if any). If you have labels with the same name in different spaces you can also pass in one space ID to filter by space as well.

func (*TestFixture) WorkItemByTitle Uses

func (fxt *TestFixture) WorkItemByTitle(title string, spaceID ...uuid.UUID) *workitem.WorkItem

WorkItemByTitle returns the first work item that has the given title (if any). If you have work items with the same title in different spaces you can also pass in one space ID to filter by space as well.

func (*TestFixture) WorkItemLinkTypeByName Uses

func (fxt *TestFixture) WorkItemLinkTypeByName(name string, spaceID ...uuid.UUID) *link.WorkItemLinkType

WorkItemLinkTypeByName returns the first work item link type that has the given name (if any). If you have work item link types with the same name in different spaces you can also pass in one space ID to filter by space as well.

func (*TestFixture) WorkItemTypeByID Uses

func (fxt *TestFixture) WorkItemTypeByID(id uuid.UUID) *workitem.WorkItemType

WorkItemTypeByID returns the work item type that has the given ID (if any).

func (*TestFixture) WorkItemTypeByName Uses

func (fxt *TestFixture) WorkItemTypeByName(name string, spaceID ...uuid.UUID) *workitem.WorkItemType

WorkItemTypeByName returns the first work item type that has the given name (if any). If you have work item types with the same name in different spaces you can also pass in one space ID to filter by space as well.

Package testfixture imports 22 packages (graph). Updated 2017-11-19. Refresh now. Tools for package owners.