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

feat: module tests can declare config/secrets/DSNs and add verb mocks #1317

Merged
merged 7 commits into from
Apr 30, 2024

Conversation

matt2e
Copy link
Collaborator

@matt2e matt2e commented Apr 22, 2024

Part of: https://hackmd.io/zNiBPIhVQN2hkeNHg2r8YA

This PR makes it easy to set up the following:

  • config
  • secrets
  • DSNs
  • mocks for verbs
"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

@alecthomas alecthomas mentioned this pull request Apr 22, 2024
@matt2e matt2e force-pushed the matt2e/tests-declare-module-context branch 3 times, most recently from 8f6de76 to c10e481 Compare April 22, 2024 06:01
matt2e added a commit that referenced this pull request Apr 22, 2024
Doc: https://hackmd.io/@ftl/SyynEK2eC
Other PRs
- [x] Pre-Req: protobuf changes
#1312
- [ ] Possible future change for tests moved here:
#1317

Changes:
- Module server requests module context, instead of calculating them
independently from envars
- controller alculates values for module context (config, secrets,
DSNs), returns it to module via gRPC
- Within a module, `ftl.PostgresDatabase(name string)` now returns a
handler, with `Get()` needing to be called to get `*sql.DB`
- Module context builder is used to compose a module context. It can
take in:
    - a GetModuleContext response, to read in values received over gRPC
    - manual entry (useful for testing) (see other PR above)
- Added in-memory implementations of `configuration.MutableProvider` and
`configuration.Resolver`. This allows us to have a backing store for
configs and secrets for `ModuleContext` which shares the same behaviour
for (un)marshalling json.
- `configuration.Manager` now has raw data versions of `Get()` and
`Set()` called `GetData()` and `SetData()` which expose the features
without needing unnecessary conversion steps. This helps when moving
values between configuration managers directly or over gRPC.
@matt2e matt2e force-pushed the matt2e/tests-declare-module-context branch 2 times, most recently from 33cb463 to 2c1513b Compare April 22, 2024 06:48
@matt2e matt2e changed the title feat: module tests can declare config/secrets/DSNs explicitly [Draft] feat: module tests can declare config/secrets/DSNs explicitly Apr 22, 2024
@matt2e matt2e changed the title [Draft] feat: module tests can declare config/secrets/DSNs explicitly [WIP] feat: module tests can declare config/secrets/DSNs explicitly Apr 26, 2024
@matt2e matt2e force-pushed the matt2e/tests-declare-module-context branch 2 times, most recently from a1dbff1 to 7bd914f Compare April 28, 2024 23:38
@github-actions github-actions bot changed the title [WIP] feat: module tests can declare config/secrets/DSNs explicitly feat: [WIP] module tests can declare config/secrets/DSNs explicitly Apr 28, 2024
@matt2e matt2e force-pushed the matt2e/tests-declare-module-context branch 5 times, most recently from 41f30a3 to 30f7b0c Compare April 29, 2024 06:00
"context"
)

type CallOverrider interface {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want this to be public to module code, but wasn't sure of a better place to put it...

@matt2e matt2e force-pushed the matt2e/tests-declare-module-context branch from 30f7b0c to b338886 Compare April 29, 2024 06:04
@matt2e matt2e changed the title feat: [WIP] module tests can declare config/secrets/DSNs explicitly feat: module tests can declare config/secrets/DSNs explicitly Apr 29, 2024
@matt2e matt2e marked this pull request as ready for review April 29, 2024 06:07
@matt2e matt2e requested a review from a team as a code owner April 29, 2024 06:07
@matt2e matt2e requested review from deniseli and removed request for a team April 29, 2024 06:07
@@ -43,8 +43,8 @@ func (v *Ref) UnmarshalText(text []byte) error {
return nil
}

func (v *Ref) String() string { return v.Module + "." + v.Name }
func (v *Ref) ToProto() *schemapb.Ref {
func (v Ref) String() string { return v.Module + "." + v.Name }
Copy link
Collaborator Author

@matt2e matt2e Apr 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is because Ref not *Ref is used everywhere but printf wasn't formatting it nicely

@matt2e matt2e changed the title feat: module tests can declare config/secrets/DSNs explicitly feat: module tests can declare config/secrets/DSNs and add verb mocks Apr 29, 2024
// mockProvider keeps a mapping of verb references to mock functions.
//
// It implements the CallOverrider interface to intercept calls with the mock functions.
type mockProvider struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth renaming this to mockVerbProvider in case we want to support mocking of entities other than verbs down the road?

DBTypePostgres = DBType(modulecontext.DBTypePostgres)
)

// Context suitable for use in testing FTL verbs with provided options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nitty nit: would you mind adding some example code to this comment so that folks don't need to grep around to learn how to use this? Basically exactly what you put in your PR description:

ctx := ftltest.Context (
        ftltest.WithConfig("default", "anonymous"),
        ftltest.WithSecret("example", "test123"),
        ftltest.WithDSN("db", ftltest.DBTypePostgres, "..."),
        ftltest.WithFakeVerb(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
        },
    )

Definitely don't need to include every single option in the example if you want to keep the comment shorter, either. Thanks :)

Copy link
Contributor

@deniseli deniseli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super useful addition! thanks :D

@matt2e matt2e force-pushed the matt2e/tests-declare-module-context branch from 96accdb to e31dd78 Compare April 30, 2024 01:47
@matt2e matt2e force-pushed the matt2e/tests-declare-module-context branch from e31dd78 to 9e05028 Compare April 30, 2024 01:49
@matt2e matt2e force-pushed the matt2e/tests-declare-module-context branch from c1bfd0c to e7a85b9 Compare April 30, 2024 03:00
@matt2e matt2e merged commit 03ec498 into main Apr 30, 2024
11 checks passed
@matt2e matt2e deleted the matt2e/tests-declare-module-context branch April 30, 2024 03:19
@matt2e matt2e mentioned this pull request May 3, 2024
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants