Skip to content

Commit

Permalink
Fix and add example for iwftest
Browse files Browse the repository at this point in the history
  • Loading branch information
longquanzheng committed May 22, 2023
1 parent c10019c commit 60054ad
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
45 changes: 45 additions & 0 deletions iwftest/communication.go

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

24 changes: 24 additions & 0 deletions iwftest/example/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package example

import "github.com/indeedeng/iwf-golang-sdk/iwf"

func NewInitState() iwf.WorkflowState {
return initState{}
}

type initState struct {
iwf.WorkflowStateDefaults
}

const keyCustomer = "customer"

func (b initState) WaitUntil(ctx iwf.WorkflowContext, input iwf.Object, persistence iwf.Persistence, communication iwf.Communication) (*iwf.CommandRequest, error) {
var customer string
input.Get(&customer)
persistence.SetDataAttribute(keyCustomer, customer)
return iwf.EmptyCommandRequest(), nil
}

func (b initState) Execute(ctx iwf.WorkflowContext, input iwf.Object, commandResults iwf.CommandResults, persistence iwf.Persistence, communication iwf.Communication) (*iwf.StateDecision, error) {
return iwf.GracefulCompletingWorkflow, nil
}
46 changes: 46 additions & 0 deletions iwftest/example/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package example

import (
"github.com/golang/mock/gomock"
"github.com/indeedeng/iwf-golang-sdk/iwf"
"github.com/indeedeng/iwf-golang-sdk/iwftest"
"github.com/stretchr/testify/assert"
"testing"
)

var mockWfCtx *iwftest.MockWorkflowContext
var mockPersistence *iwftest.MockPersistence
var mockCommunication *iwftest.MockCommunication
var emptyCmdResults = iwf.CommandResults{}
var testCustomer = "customer1"
var emptyObj = iwftest.NewTestObject(testCustomer)

func beforeEach(t *testing.T) {
ctrl := gomock.NewController(t)

mockWfCtx = iwftest.NewMockWorkflowContext(ctrl)
mockPersistence = iwftest.NewMockPersistence(ctrl)
mockCommunication = iwftest.NewMockCommunication(ctrl)
}

func TestInitState_WaitUntil(t *testing.T) {
beforeEach(t)

state := NewInitState()

mockPersistence.EXPECT().SetDataAttribute(keyCustomer, testCustomer)
cmdReq, err := state.WaitUntil(mockWfCtx, emptyObj, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.EmptyCommandRequest(), cmdReq)
}

func TestInitState_Execute(t *testing.T) {
beforeEach(t)

state := NewInitState()
input := iwftest.NewTestObject(testCustomer)

decision, err := state.Execute(mockWfCtx, input, emptyCmdResults, mockPersistence, mockCommunication)
assert.Nil(t, err)
assert.Equal(t, iwf.GracefulCompletingWorkflow, decision)
}

0 comments on commit 60054ad

Please sign in to comment.