Skip to content

Commit

Permalink
Fix iwftest.MockCommunication and add example (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
longquanzheng committed May 22, 2023
1 parent c10019c commit b2ed2e6
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 58 deletions.
59 changes: 1 addition & 58 deletions iwftest/README.md
Expand Up @@ -9,62 +9,5 @@ The APIs are generated by the below commands:

## Usage

See the [samples](https://github.com/indeedeng/iwf-golang-samples) for more details:
See the [example](./example) for more details.

```go
package subscription

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"
"time"
)

var mockWfCtx *iwftest.MockWorkflowContext
var mockPersistence *iwftest.MockPersistence
var mockCommunication *iwftest.MockCommunication
var emptyCmdResults = iwf.CommandResults{}
var emptyObj = iwftest.NewTestObject(nil)
var mockSvc *MockMyService

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

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


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

state := NewInitState()

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


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

state := NewTrialState(mockSvc)

mockSvc.EXPECT().sendEmail(testCustomer.Email, gomock.Any(), gomock.Any())
mockPersistence.EXPECT().GetDataObject(keyCustomer, gomock.Any()).SetArg(1, testCustomer)
cmdReq, err := state.WaitUntil(mockWfCtx, emptyObj, mockPersistence, mockCommunication)
assert.Nil(t, err)
firingTime := cmdReq.Commands[0].TimerCommand.FiringUnixTimestampSeconds
assert.Equal(t, iwf.AllCommandsCompletedRequest(
iwf.NewTimerCommand("", time.Unix(firingTime, 0)),
), cmdReq)
}

```
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
@@ -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
@@ -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 b2ed2e6

Please sign in to comment.