aestubs

package
v0.0.0-...-e0f9f6e Latest Latest
Warning

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

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

Documentation

Overview

Package aestubs provides a set of App Engine service stubs and a configurable appengine.Context implementation.

The aestubs package is a companion for appengine/aetest. It provides some faster in-memory implementations of most common appengine APIs, avoiding the need to spin up a full implementation of the appengine.Context, provided by the appengine/aetest.NewContext method.

WARNING: This package is a work in progress and is subject to changes

How to use

You can use the package by initializing either an service-aware context:

func TestCurrentAppId(t *testing.T) {
	c := aestubs.NewContext(nil, t)
	fmt.Printf(appengine.AppID(c))
}

It is also possible to use an in-memory stub provided by the package. The context returned by NewContext method contains all services binded by default:

type Test struct {
	Name string
}

func TestDatastorePut(t *testing.T) {
	c := aestubs.NewContext(nil, t)
	// The in-memory implementation requires no setup or tear down, and
	// is safe for concurrent use
	k := datastore.NewKey(c, "Test", "", 0, nil)
	e := &Test{Name: "Test entity"}
	k, err := datastore.Put(c, k, e)
	if err != nil {
		t.Errorf("Error in datastore.Put: %v", err)
	}
	// Check state of k and e...
}

Stability

This package rely on the AppEngine internal implementation package, appengine_internal, and may break in upcomming SDK releases. However, test speed up with the current implementation can be up to 100x, depending on the actual resource usage in your test cases, or test machines.

Implemented services and methods

Currently implemented services and methods are:

datastore_v3.Get           Work as expected.
datastore_v3.Put           Work as expected.
datastore_v3.AllocateIds   Work as expected, simulates legacy id policy.
urlfetch.Fetch             Basic funcionality, no headers, limit validation, performs real HTTP requests.
taskqueue.Add              Allow basic task enqueue, no queue name validation, no tasks run, count enqueued tasks per queue.

Not all services are implemented yet, but they can be added in your test code. The caveat is that your test code will also rely directly in the appengine_internal package.

Index

Constants

View Source
const (
	Datastore = "datastore_v3"
	Taskqueue = "taskqueue"
	Urlfetch  = "urlfetch"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context interface {
	appengine.Context
	Stub(service string) ServiceStub
	AddStub(service string, stub ServiceStub) Context
	Clean()
}

func NewContext

func NewContext(opts *Opts, t *testing.T) Context

func NewEmptyContext

func NewEmptyContext(opts *Opts, t *testing.T) Context

type DatastoreStub

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

DatastoreStub is an in-memory datastore backed by a map.

func NewDatastoreStub

func NewDatastoreStub() *DatastoreStub

NewDatastoreStub initializes a DatastoreStub map properly.

func (*DatastoreStub) Call

Call makes DatastoreStub implement the ServiceStub interface.

func (*DatastoreStub) Clean

func (d *DatastoreStub) Clean()

Clean clean up the datastore data in memory

func (*DatastoreStub) Length

func (d *DatastoreStub) Length() int

type Opts

type Opts struct {
	AppID string
}

type ServiceStub

type ServiceStub interface {
	Call(method string, in, out appengine_internal.ProtoMessage, opts *appengine_internal.CallOptions) error
	Clean()
}

type TaskqueueStub

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

func NewTaskqueueStub

func NewTaskqueueStub() *TaskqueueStub

func (*TaskqueueStub) AddedTasks

func (t *TaskqueueStub) AddedTasks(queue string) int

func (*TaskqueueStub) Call

func (*TaskqueueStub) Clean

func (t *TaskqueueStub) Clean()

type UrlfetchStub

type UrlfetchStub struct {
	*http.Client
}

func NewUrlfetchStub

func NewUrlfetchStub() *UrlfetchStub

func (*UrlfetchStub) Call

func (*UrlfetchStub) Clean

func (u *UrlfetchStub) Clean()

Jump to

Keyboard shortcuts

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