Skip to content

Commit

Permalink
Aligning OnWorkflow with OnActivity and adding unit tests to confirm …
Browse files Browse the repository at this point in the history
…behavior is coherent (#1128)

Aligning OnWorkflow with OnActivity and adding unit tests to confirm behavior is coherent
  • Loading branch information
edmondop authored Jun 7, 2023
1 parent eb68bb8 commit 24f2c86
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/workflow_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,19 +388,19 @@ func (e *TestWorkflowEnvironment) OnWorkflow(workflow interface{}, args ...inter
var call *mock.Call
switch fType.Kind() {
case reflect.Func:
fnType := reflect.TypeOf(workflow)
if err := validateFnFormat(fnType, true); err != nil {
if err := validateFnFormat(fType, true); err != nil {
panic(err)
}
fnName, _ := getWorkflowFunctionName(e.impl.registry, workflow)
if alias, ok := e.impl.registry.getWorkflowAlias(fnName); ok {
fnName = alias
}
e.impl.registry.RegisterWorkflowWithOptions(workflow, RegisterWorkflowOptions{DisableAlreadyRegisteredCheck: true})
call = e.mock.On(fnName, args...)
case reflect.String:
call = e.mock.On(workflow.(string), args...)
default:
panic("activity must be function or string")
panic("workflow must be function or string")
}

return e.wrapCall(call)
Expand Down
26 changes: 26 additions & 0 deletions internal/workflow_testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package internal
import (
"context"
"errors"
"github.com/stretchr/testify/assert"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -291,3 +292,28 @@ func TestActivityAssertNumberOfCalls(t *testing.T) {
env.AssertNumberOfCalls(t, "namedActivity", 3)
env.AssertNumberOfCalls(t, "otherActivity", 0)
}

func HelloWorkflow(_ Context, name string) (string, error) {
return "", errors.New("unimplemented")
}

func TestWorkflowMockingWithoutRegistration(t *testing.T) {
testSuite := &WorkflowTestSuite{}
env := testSuite.NewTestWorkflowEnvironment()
env.OnWorkflow(HelloWorkflow, mock.Anything, mock.Anything).Return(
func(ctx Context, person string) (string, error) {
return "Hello " + person + "!", nil
})
env.ExecuteWorkflow("HelloWorkflow", "Temporal")
require.NoError(t, env.GetWorkflowError())
var result string
err := env.GetWorkflowResult(&result)
require.NoError(t, err)
require.Equal(t, "Hello Temporal!", result)
}

func TestActivityMockingByNameWithoutRegistrationFails(t *testing.T) {
testSuite := &WorkflowTestSuite{}
env := testSuite.NewTestWorkflowEnvironment()
assert.Panics(t, func() { env.OnActivity("SayHello", mock.Anything, mock.Anything) }, "The code did not panic")
}

0 comments on commit 24f2c86

Please sign in to comment.