generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: module tests can declare config/secrets/DSNs and add verb mocks (…
…#1317) Part of: https://hackmd.io/zNiBPIhVQN2hkeNHg2r8YA This PR makes it easy to set up the following: - config - secrets - DSNs - mocks for verbs ```go "github.com/TBD54566975/ftl/go-runtime/ftltest" func TestEcho(t *testing.T) { ctx := ftltest.Context ( ftltest.WithConfig("default", "anonymous"), ftltest.WithSecret("example", "test123"), ftltest.WithDSN("db", ftltest.DBTypePostgres, "..."), ftltest.WhenVerb(time.Time, func(ctx context. Context, req time. TimeRequest) (time.TimeResponse, error) { return time.TimeResponse{Time: stdtime.Date(2021, 9, 1, 0, 0, 0, 0, stdtime.UTC)}, nil }, ) ... } ``` Another improvement is when doing unit tests, a simple error message will be returned when attempting to make a `ftl.Call` when there isn't any rpc client set up. This error will be returned: > time.time: no mock found
- Loading branch information
Showing
7 changed files
with
187 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ftl | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type CallOverrider interface { | ||
OverrideCall(ctx context.Context, callee Ref, req any) (override bool, resp any, err error) | ||
} | ||
type contextCallOverriderKey struct{} | ||
|
||
func ApplyCallOverriderToContext(ctx context.Context, overrider CallOverrider) context.Context { | ||
return context.WithValue(ctx, contextCallOverriderKey{}, overrider) | ||
} | ||
|
||
func CallOverriderFromContext(ctx context.Context) (CallOverrider, bool) { | ||
if overrider, ok := ctx.Value(contextCallOverriderKey{}).(CallOverrider); ok { | ||
return overrider, true | ||
} | ||
return nil, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package ftltest | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/TBD54566975/ftl/go-runtime/ftl" | ||
"github.com/TBD54566975/ftl/internal/rpc" | ||
|
||
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect" | ||
) | ||
|
||
type mockFunc func(ctx context.Context, req any) (resp any, err error) | ||
|
||
// mockVerbProvider keeps a mapping of verb references to mock functions. | ||
// | ||
// It implements the CallOverrider interface to intercept calls with the mock functions. | ||
type mockVerbProvider struct { | ||
mocks map[ftl.Ref]mockFunc | ||
} | ||
|
||
var _ = (ftl.CallOverrider)(&mockVerbProvider{}) | ||
|
||
func newMockVerbProvider() *mockVerbProvider { | ||
provider := &mockVerbProvider{ | ||
mocks: map[ftl.Ref]mockFunc{}, | ||
} | ||
return provider | ||
} | ||
|
||
func (m *mockVerbProvider) OverrideCall(ctx context.Context, ref ftl.Ref, req any) (override bool, resp any, err error) { | ||
mock, ok := m.mocks[ref] | ||
if ok { | ||
resp, err = mock(ctx, req) | ||
return true, resp, err | ||
} | ||
if rpc.IsClientAvailableInContext[ftlv1connect.VerbServiceClient](ctx) { | ||
return false, nil, nil | ||
} | ||
// Return a clean error for testing because we know the client is not available to make real calls | ||
return false, nil, fmt.Errorf("no mock found") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters