ftltest

package
v0.418.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2025 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Overview

Package ftltest contains test utilities for the ftl package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call

func Call[VerbClient, Req, Resp any](ctx context.Context, req Req) (Resp, error)

Call a Verb inline, applying resources and test behavior.

func CallEmpty

func CallEmpty[VerbClient any](ctx context.Context) error

CallEmpty calls an Empty inline, applying resources and test behavior.

func CallSink

func CallSink[VerbClient, Req any](ctx context.Context, req Req) error

CallSink calls a Sink inline, applying resources and test behavior.

func CallSource

func CallSource[VerbClient, Resp any](ctx context.Context) (Resp, error)

CallSource calls a Source inline, applying resources and test behavior.

func Context

func Context(options ...Option) context.Context

Context suitable for use in testing FTL verbs with provided options

func GetDatabaseHandle

func GetDatabaseHandle[T ftl.DatabaseConfig]() (ftl.DatabaseHandle[T], error)

GetDatabaseHandle returns a database handle using the given database config.

func SubContext

func SubContext(ctx context.Context, options ...Option) context.Context

SubContext applies the given options to the given context, creating a new context extending the previous one.

Does not modify the existing context

Types

type Option

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

func WhenEmpty

func WhenEmpty[EmptyClient any](fake ftl.Empty) Option

WhenEmpty replaces an implementation for a verb with no request or response

To be used when setting up a context for a test:

ctx := ftltest.Context(
	ftltest.WhenEmpty[example.EmptyClient](func(ctx context.Context) error {
    	...
	}),
)

func WhenMap

func WhenMap[T, U any](mapper *ftl.MapHandle[T, U], fake func(context.Context) (U, error)) Option

WhenMap injects a fake implementation of a Mapping function

To be used when setting up a context for a test:

ctx := ftltest.Context(
	ftltest.WhenMap(Example.MapHandle, func(ctx context.Context) (U, error) {
    	// ...
	}),
	// ... other options
)

func WhenSink

func WhenSink[SinkClient, Req any](fake ftl.Sink[Req]) Option

WhenSink replaces an implementation for a verb with no response

To be used when setting up a context for a test:

ctx := ftltest.Context(
	ftltest.WhenSink[example.SinkClient](func(ctx context.Context, req example.Req) error {
    	...
	}),
	// ... other options
)

func WhenSource

func WhenSource[SourceClient, Resp any](fake ftl.Source[Resp]) Option

WhenSource replaces an implementation for a verb with no request

To be used when setting up a context for a test:

ctx := ftltest.Context(
	ftltest.WhenSource[example.SourceClient](func(ctx context.Context) (example.Resp, error) {
    	// ...
	}),
	// ... other options
)

func WhenVerb

func WhenVerb[VerbClient, Req, Resp any](fake ftl.Verb[Req, Resp]) Option

WhenVerb replaces an implementation for a verb

To be used when setting up a context for a test:

ctx := ftltest.Context(
	ftltest.WhenVerb[example.VerbClient](func(ctx context.Context, req example.Req) (example.Resp, error) {
    	// ...
	}),
	// ... other options
)

func WithCallsAllowedWithinModule

func WithCallsAllowedWithinModule() Option

WithCallsAllowedWithinModule allows tests to enable calls to all verbs within the current module

Any overrides provided by calling WhenVerb(...) will take precedence

func WithConfig

func WithConfig[T ftl.ConfigType](config ftl.Config[T], value T) Option

WithConfig sets a configuration for the current module

To be used when setting up a context for a test:

ctx := ftltest.Context(
	ftltest.WithConfig(exampleEndpoint, "https://example.com"),
	// ... other options
)

func WithDatabase

func WithDatabase[T ftl.DatabaseConfig]() Option

WithDatabase sets up a database for testing by appending "_test" to the DSN and emptying all tables

func WithDefaultProjectFile

func WithDefaultProjectFile() Option

WithDefaultProjectFile loads config and secrets from the default project file, which is either the FTL_CONFIG environment variable or the ftl-project.toml file in the git root.

func WithMapsAllowed

func WithMapsAllowed() Option

WithMapsAllowed allows all `ftl.Map` calls to pass through to their original implementation.

Any overrides provided by calling WhenMap(...) will take precedence.

func WithProjectFile

func WithProjectFile(path string) Option

WithProjectFile loads config and secrets from a project file

Takes a path to an FTL project file. If an empty path is provided, the path is inferred from the FTL_CONFIG environment variable. If that is not found, the ftl-project.toml file in the git root is used. If a project file is not found, an error is returned.

To be used when setting up a context for a test:

ctx := ftltest.Context(
	ftltest.WithProjectFile("path/to/ftl-project.yaml"),
	// ... other options
)

func WithSecret

func WithSecret[T ftl.SecretType](secret ftl.Secret[T], value T) Option

WithSecret sets a secret for the current module

To be used when setting up a context for a test:

ctx := ftltest.Context(
	ftltest.WithSecret(privateKey, "abc123"),
	// ... other options
)

type OptionsState

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

Jump to

Keyboard shortcuts

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