-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstage_test.go
63 lines (57 loc) · 1.59 KB
/
stage_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package migration
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type mockExecutor struct {
mock.Mock
}
func (m *mockExecutor) execute(ctx context.Context, task *Task) error {
time.Sleep(50 * time.Millisecond)
args := m.Called(ctx, task)
if args.Get(0) == nil {
return nil
}
return args.Error(0)
}
func TestStage_Execute_Success(t *testing.T) {
shouldBe := assert.New(t)
mockedExecutor := &mockExecutor{}
mockedExecutor.On("execute", mock.Anything, mock.Anything).Return(nil).Once()
s := &stage{
name: "test",
success: Step(0),
failure: Step(1),
execute: mockedExecutor.execute,
}
task := &Task{Request: Request{ID: "test-123"}}
result := s.Execute(context.TODO(), task)
mockedExecutor.AssertExpectations(t)
shouldBe.True(result)
shouldBe.Equal(Step(0), task.LastStep)
shouldBe.GreaterOrEqual(task.TotalDuration, 50*time.Millisecond)
}
func TestStage_Execute_Failure(t *testing.T) {
shouldBe := assert.New(t)
expected := "test error"
mockedExecutor := &mockExecutor{}
mockedExecutor.On("execute", mock.Anything, mock.Anything).Return(errors.New(expected)).Once()
s := &stage{
name: "test",
success: Step(0),
failure: Step(1),
execute: mockedExecutor.execute,
}
task := &Task{Request: Request{ID: "test-123"}}
result := s.Execute(context.TODO(), task)
mockedExecutor.AssertExpectations(t)
shouldBe.False(result)
shouldBe.Equal(Step(1), task.LastStep)
shouldBe.Equal(time.Duration(0), task.TotalDuration)
shouldBe.Equal(&expected, task.ErrorDetails)
shouldBe.Equal(StatusFailed, task.Status)
}