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

Provide ability to mock ftl.Map #1586

Closed
Tracked by #1612
mistermoe opened this issue May 28, 2024 · 0 comments · Fixed by #1616
Closed
Tracked by #1612

Provide ability to mock ftl.Map #1586

mistermoe opened this issue May 28, 2024 · 0 comments · Fixed by #1616
Assignees
Labels
good first issue Good for newcomers

Comments

@mistermoe
Copy link
Collaborator

mistermoe commented May 28, 2024

I'm trying to test a verb that makes a request to a third party API whose client is returned using ftl.Map e.g.

package somemodule

var sec = ftl.Secret[SomeAPICredentials]("some_api")
var someAPI = ftl.Map(sec, func(ctx context.Context, creds SomeAPICredentials) (libsomeapi.Client, error) {
	return libsomeapi.NewClient(creds), nil
})

//ftl:verb export
func SomeVerb(ctx context.Context, req ftl.Builtin) (ftl.Builtin, error) {
	// ...stuff
	
	resp, err := someAPI.Get(ctx).Foo()
	
	// ... other stuff
}

it would be cool if i could override what ftl.Map returns when writing tests so that no network calls are required and so non-happy paths can be tested.

For now, i think i'll have to set/unset the global variable in each test.

Some design notes

We have a new system for differentiation between "real" code and "test" code should take place, the FTL interface. We should implement this in there somehow, and add options for ftltest.Context() to set it up and tear it down at the end of the test.

It should probably follow the pattern of how the fakes for verbs are done:

  1. By default all Map calls under testing will panic
  2. An option to allow all Map calls to pass through
  3. A final option to mock individual calls.

These three will give full control.

We'll need to do something like:

  1. Add a FTL.CallMap(ctx context.Context, mapper any) (any, error) method to the FTL interface
  2. Add mockMaps to ftltest.OptionsState
  3. Extend newFakeFTL() to take in the mockMaps
  4. Add options for ftltest.Context() to achieve the above
  5. Replace the ftl.Map.Get implementation with one that uses the FTL interface

I think the API would be something like:

// WhenMap injects a fake implementation of a Mapping function
func WhenMap[T, U any](mapper *ftl.MapHandle[T], fake func(context.Context, T) (U, error)) Option

// WithMapsAllowed allows all `ftl.Map` calls to pass through to their original implementation, rather than
func WithMapsAllowed() Option
@github-actions github-actions bot added the triage Issue needs triaging label May 28, 2024
@ftl-robot ftl-robot mentioned this issue May 28, 2024
@wesbillman wesbillman added good first issue Good for newcomers and removed triage Issue needs triaging labels May 28, 2024
@deniseli deniseli self-assigned this May 31, 2024
deniseli added a commit that referenced this issue Jun 5, 2024
Fixes: #1586
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants