nutest

package module
v0.0.0-...-43d857d Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

nuclio function wrapper

Test nuclio functions locally of as part of Go testing

Usage, Golang unit testing framework:

package main

import (
	"testing"
	"github.com/nuclio/nuclio-test-go"
)

func TestName(t *testing.T) {
	// data binding for V3IO data containers, optional (use nil instead of &data)
	data := nutest.DataBind{Name:"db0", Url:"<v3io address>", Container:"x"}

	// Create TestContext and specify the function name, verbose, data 
	tc, err := nutest.NewTestContext(MyHandler, true, &data )
	if err != nil {
		t.Fail()
	}

	// Optional, initialize context must have a function in the form:
	//    InitContext(context *nuclio.Context) error
	err = tc.InitContext(InitContext)
	if err != nil {
		t.Fail()
	}

	// Create a new test event 
	testEvent := nutest.TestEvent{
		Path: "/some/path",
		Body: []byte("1234"),
		Headers:map[string]interface{}{"first": "string"},
		}
	
	// invoke the tested function with the new event and log it's output 
	resp, err := tc.Invoke(&testEvent)
	tc.Logger.InfoWith("Run complete", "resp", resp, "err", err)
}

Usage, called from another program:

package main

import (
	"github.com/nuclio/nuclio-test-go"
)

func main() {
	// data binding for V3IO data containers, optional (use nil instead of &data)
	data := nutest.DataBind{Name:"db0", Url:""<v3io address>", Container:"x"}

	// Create TestContext and specify the function name, verbose, data 
	tc, err := nutest.NewTestContext(MyHandler, true, &data )
	if err != nil {
		panic(err)
	}

	// Create a new test event 
	testEvent := nutest.TestEvent{
		Path: "/some/path",
		Body: []byte("1234"),
		Headers:map[string]interface{}{"first": "something"},
	}
	
	// invoke the tested function with the new event and log it's output 
	resp, err := tc.Invoke(&testEvent)
	tc.Logger.InfoWith("Run complete", "resp", resp, "err", err)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupported = errors.New("Event does not support this interface")

Functions

This section is empty.

Types

type DataBind

type DataBind struct {
	Name      string
	Url       string
	Container string
	User      string
	Password  string
}

type TestContext

type TestContext struct {
	LogLevel nucliozap.Level
	Logger   logger.Logger
	Data     *DataBind
	// contains filtered or unexported fields
}

func NewTestContext

func NewTestContext(function func(context *nuclio.Context, event nuclio.Event) (interface{}, error),
	verbose bool, data *DataBind) (*TestContext, error)

func (*TestContext) GetContext

func (tc *TestContext) GetContext() nuclio.Context

func (*TestContext) InitContext

func (tc *TestContext) InitContext(function func(context *nuclio.Context) error) error

func (*TestContext) Invoke

func (tc *TestContext) Invoke(event nuclio.Event) (interface{}, error)

type TestEvent

type TestEvent struct {
	nuclio.AbstractEvent

	Body        []byte
	ContentType string

	Headers map[string]interface{}
	Path    string
	URL     string
	Method  string
	Time    time.Time
	// contains filtered or unexported fields
}

func (*TestEvent) GetBody

func (te *TestEvent) GetBody() []byte

func (*TestEvent) GetContentType

func (te *TestEvent) GetContentType() string

func (*TestEvent) GetHeader

func (te *TestEvent) GetHeader(key string) interface{}

func (*TestEvent) GetHeaderByteSlice

func (te *TestEvent) GetHeaderByteSlice(key string) []byte

func (*TestEvent) GetHeaderString

func (te *TestEvent) GetHeaderString(key string) string

func (*TestEvent) GetHeaders

func (te *TestEvent) GetHeaders() map[string]interface{}

func (*TestEvent) GetMethod

func (te *TestEvent) GetMethod() string

func (*TestEvent) GetPath

func (te *TestEvent) GetPath() string

func (*TestEvent) GetTimestamp

func (te *TestEvent) GetTimestamp() time.Time

func (*TestEvent) GetURL

func (te *TestEvent) GetURL() string

Jump to

Keyboard shortcuts

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