Skip to content

Commit b2ed2e6

Browse files
Fix iwftest.MockCommunication and add example (#63)
1 parent c10019c commit b2ed2e6

File tree

4 files changed

+116
-58
lines changed

4 files changed

+116
-58
lines changed

iwftest/README.md

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,5 @@ The APIs are generated by the below commands:
99

1010
## Usage
1111

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

14-
```go
15-
package subscription
16-
17-
import (
18-
"github.com/golang/mock/gomock"
19-
"github.com/indeedeng/iwf-golang-sdk/iwf"
20-
"github.com/indeedeng/iwf-golang-sdk/iwftest"
21-
"github.com/stretchr/testify/assert"
22-
"testing"
23-
"time"
24-
)
25-
26-
var mockWfCtx *iwftest.MockWorkflowContext
27-
var mockPersistence *iwftest.MockPersistence
28-
var mockCommunication *iwftest.MockCommunication
29-
var emptyCmdResults = iwf.CommandResults{}
30-
var emptyObj = iwftest.NewTestObject(nil)
31-
var mockSvc *MockMyService
32-
33-
func beforeEach(t *testing.T) {
34-
ctrl := gomock.NewController(t)
35-
36-
mockSvc = NewMockMyService(ctrl)
37-
mockWfCtx = iwftest.NewMockWorkflowContext(ctrl)
38-
mockPersistence = iwftest.NewMockPersistence(ctrl)
39-
mockCommunication = iwftest.NewMockCommunication(ctrl)
40-
}
41-
42-
43-
func TestInitState_WaitUntil(t *testing.T) {
44-
beforeEach(t)
45-
46-
state := NewInitState()
47-
48-
mockPersistence.EXPECT().SetDataObject(keyCustomer, testCustomer)
49-
cmdReq, err := state.WaitUntil(mockWfCtx, testCustomerObj, mockPersistence, mockCommunication)
50-
assert.Nil(t, err)
51-
assert.Equal(t, iwf.EmptyCommandRequest(), cmdReq)
52-
}
53-
54-
55-
func TestTrialState_WaitUntil(t *testing.T) {
56-
beforeEach(t)
57-
58-
state := NewTrialState(mockSvc)
59-
60-
mockSvc.EXPECT().sendEmail(testCustomer.Email, gomock.Any(), gomock.Any())
61-
mockPersistence.EXPECT().GetDataObject(keyCustomer, gomock.Any()).SetArg(1, testCustomer)
62-
cmdReq, err := state.WaitUntil(mockWfCtx, emptyObj, mockPersistence, mockCommunication)
63-
assert.Nil(t, err)
64-
firingTime := cmdReq.Commands[0].TimerCommand.FiringUnixTimestampSeconds
65-
assert.Equal(t, iwf.AllCommandsCompletedRequest(
66-
iwf.NewTimerCommand("", time.Unix(firingTime, 0)),
67-
), cmdReq)
68-
}
69-
70-
```

iwftest/communication.go

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

iwftest/example/example.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package example
2+
3+
import "github.com/indeedeng/iwf-golang-sdk/iwf"
4+
5+
func NewInitState() iwf.WorkflowState {
6+
return initState{}
7+
}
8+
9+
type initState struct {
10+
iwf.WorkflowStateDefaults
11+
}
12+
13+
const keyCustomer = "customer"
14+
15+
func (b initState) WaitUntil(ctx iwf.WorkflowContext, input iwf.Object, persistence iwf.Persistence, communication iwf.Communication) (*iwf.CommandRequest, error) {
16+
var customer string
17+
input.Get(&customer)
18+
persistence.SetDataAttribute(keyCustomer, customer)
19+
return iwf.EmptyCommandRequest(), nil
20+
}
21+
22+
func (b initState) Execute(ctx iwf.WorkflowContext, input iwf.Object, commandResults iwf.CommandResults, persistence iwf.Persistence, communication iwf.Communication) (*iwf.StateDecision, error) {
23+
return iwf.GracefulCompletingWorkflow, nil
24+
}

iwftest/example/example_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package example
2+
3+
import (
4+
"github.com/golang/mock/gomock"
5+
"github.com/indeedeng/iwf-golang-sdk/iwf"
6+
"github.com/indeedeng/iwf-golang-sdk/iwftest"
7+
"github.com/stretchr/testify/assert"
8+
"testing"
9+
)
10+
11+
var mockWfCtx *iwftest.MockWorkflowContext
12+
var mockPersistence *iwftest.MockPersistence
13+
var mockCommunication *iwftest.MockCommunication
14+
var emptyCmdResults = iwf.CommandResults{}
15+
var testCustomer = "customer1"
16+
var emptyObj = iwftest.NewTestObject(testCustomer)
17+
18+
func beforeEach(t *testing.T) {
19+
ctrl := gomock.NewController(t)
20+
21+
mockWfCtx = iwftest.NewMockWorkflowContext(ctrl)
22+
mockPersistence = iwftest.NewMockPersistence(ctrl)
23+
mockCommunication = iwftest.NewMockCommunication(ctrl)
24+
}
25+
26+
func TestInitState_WaitUntil(t *testing.T) {
27+
beforeEach(t)
28+
29+
state := NewInitState()
30+
31+
mockPersistence.EXPECT().SetDataAttribute(keyCustomer, testCustomer)
32+
cmdReq, err := state.WaitUntil(mockWfCtx, emptyObj, mockPersistence, mockCommunication)
33+
assert.Nil(t, err)
34+
assert.Equal(t, iwf.EmptyCommandRequest(), cmdReq)
35+
}
36+
37+
func TestInitState_Execute(t *testing.T) {
38+
beforeEach(t)
39+
40+
state := NewInitState()
41+
input := iwftest.NewTestObject(testCustomer)
42+
43+
decision, err := state.Execute(mockWfCtx, input, emptyCmdResults, mockPersistence, mockCommunication)
44+
assert.Nil(t, err)
45+
assert.Equal(t, iwf.GracefulCompletingWorkflow, decision)
46+
}

0 commit comments

Comments
 (0)