Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: expand ftltest fakes to other verb types #1415

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion go-runtime/ftl/ftltest/ftltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func WithSecret[T ftl.SecretType](secret ftl.SecretValue[T], value T) Option {
// ... other options
//
// )
func WhenVerb[Req any, Resp any](verb ftl.Verb[Req, Resp], fake func(ctx context.Context, req Req) (resp Resp, err error)) Option {
func WhenVerb[Req any, Resp any](verb ftl.Verb[Req, Resp], fake ftl.Verb[Req, Resp]) Option {
return func(ctx context.Context, state *OptionsState) error {
ref := ftl.FuncRef(verb)
state.mockVerbs[schema.RefKey(ref)] = func(ctx context.Context, req any) (resp any, err error) {
Expand All @@ -165,6 +165,73 @@ func WhenVerb[Req any, Resp any](verb ftl.Verb[Req, Resp], fake func(ctx context
}
}

// 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.Source, func(ctx context.Context) (Example.Resp, error) {
// ...
// }),
// ... other options
//
// )
func WhenSource[Resp any](source ftl.Source[Resp], fake func(ctx context.Context) (resp Resp, err error)) Option {
return func(ctx context.Context, state *OptionsState) error {
ref := ftl.FuncRef(source)
state.mockVerbs[schema.RefKey(ref)] = func(ctx context.Context, req any) (resp any, err error) {
return fake(ctx)
}
return nil
}
}

// 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.Sink, func(ctx context.Context, req Example.Req) error {
// ...
// }),
// ... other options
//
// )
func WhenSink[Req any](sink ftl.Sink[Req], fake func(ctx context.Context, req Req) error) Option {
return func(ctx context.Context, state *OptionsState) error {
ref := ftl.FuncRef(sink)
state.mockVerbs[schema.RefKey(ref)] = func(ctx context.Context, req any) (resp any, err error) {
request, ok := req.(Req)
if !ok {
return nil, fmt.Errorf("invalid request type %T for %v, expected %v", req, ref, reflect.TypeFor[Req]())
}
return ftl.Unit{}, fake(ctx, request)
}
return nil
}
}

// 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.Empty, func(ctx context.Context) error {
// ...
// }),
// ... other options
//
// )
func WhenEmpty(empty ftl.Empty, fake func(ctx context.Context) (err error)) Option {
return func(ctx context.Context, state *OptionsState) error {
ref := ftl.FuncRef(empty)
state.mockVerbs[schema.RefKey(ref)] = func(ctx context.Context, req any) (resp any, err error) {
return ftl.Unit{}, fake(ctx)
}
return nil
}
}

// WithCallsAllowedWithinModule allows tests to enable calls to all verbs within the current module
//
// Any overrides provided by calling WhenVerb(...) will take precedence
Expand Down
4 changes: 3 additions & 1 deletion integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ func TestModuleUnitTests(t *testing.T) {
run(t,
copyModule("time"),
copyModule("wrapped"),
build("time", "wrapped"),
copyModule("verbtypes"),
build("time", "wrapped", "verbtypes"),
testModule("wrapped"),
testModule("verbtypes"),
)
}

Expand Down
2 changes: 2 additions & 0 deletions integration/testdata/go/verbtypes/ftl.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module = "verbtypes"
language = "go"
51 changes: 51 additions & 0 deletions integration/testdata/go/verbtypes/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module ftl/verbtypes

go 1.22.2

require (
github.com/TBD54566975/ftl v0.201.0
github.com/alecthomas/assert/v2 v2.9.0
)

replace github.com/TBD54566975/ftl => ../../../..

require (
connectrpc.com/connect v1.16.1 // indirect
connectrpc.com/grpcreflect v1.2.0 // indirect
connectrpc.com/otelconnect v0.7.0 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/TBD54566975/scaffolder v0.8.0 // indirect
github.com/alecthomas/concurrency v0.0.2 // indirect
github.com/alecthomas/kong v0.9.0 // indirect
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/repr v0.4.0 // indirect
github.com/alecthomas/types v0.14.0 // indirect
github.com/alessio/shellescape v1.4.2 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/swaggest/jsonschema-go v0.3.70 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.4 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.34.0 // indirect
)
142 changes: 142 additions & 0 deletions integration/testdata/go/verbtypes/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions integration/testdata/go/verbtypes/verbtypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package verbtypes

import (
"context"
// Import the FTL SDK.
)

// verbtypes is a simple module that has each type of verb (verb, source, sink and empty)

type Request struct {
Input string `json:"input"`
}

type Response struct {
Output string `json:"output"`
}

//ftl:verb
func Verb(ctx context.Context, req Request) (Response, error) {
return Response{Output: req.Input}, nil
}

//ftl:verb
func Source(ctx context.Context) (Response, error) {
return Response{Output: "source"}, nil
}

//ftl:verb
func Sink(ctx context.Context, req Request) error {
return nil
}

//ftl:verb
func Empty(ctx context.Context) error {
return nil
}
Loading