-
Notifications
You must be signed in to change notification settings - Fork 2
/
trace_test.go
65 lines (49 loc) · 2.04 KB
/
trace_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
64
65
package pipeline_test
import (
"bytes"
"context"
"errors"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/saantiaguilera/go-pipeline"
)
func TestTrace_GivenAStepToTrace_WhenRun_ThenOutputsInnerStepErr(t *testing.T) {
mockStep := new(mockStep[any, any])
mockStep.On("Run", mock.Anything, mock.Anything).Return(nil, errors.New("some error"))
step := pipeline.NewTracedStep[any, any]("test name", mockStep)
_, err := step.Run(context.Background(), 1)
assert.NotNil(t, err)
assert.Equal(t, "some error", err.Error())
}
func TestTrace_GivenAStepToTrace_WhenRunFailing_ThenSpecificFormatIsUsed(t *testing.T) {
mockStep := new(mockStep[any, any])
mockStep.On("Run", mock.Anything, 1).Return(nil, errors.New("some error"))
writer := bytes.NewBufferString("")
step := pipeline.NewTracedStepWithWriter[any, any]("test name", mockStep, writer)
validator := regexp.MustCompile(`^\[STAGE] \d{4}-\d{2}-\d{2} - \d{2}:\d{2}:\d{2} \| test name \| [.\d]+[µnm]s \| Failure: some error\n$`)
_, _ = step.Run(context.Background(), 1)
output := writer.Bytes()
assert.True(t, validator.Match(output))
}
func TestTrace_GivenAStepToTrace_WhenRunSuccessfully_ThenSpecificFormatIsUsed(t *testing.T) {
mockStep := new(mockStep[any, any])
mockStep.On("Run", mock.Anything, 1).Return("test", nil)
writer := bytes.NewBufferString("")
step := pipeline.NewTracedStepWithWriter[any, any]("test name", mockStep, writer)
validator := regexp.MustCompile(`^\[STAGE] \d{4}-\d{2}-\d{2} - \d{2}:\d{2}:\d{2} \| test name \| [.\d]+[µnm]s \| Success\n$`)
v, _ := step.Run(context.Background(), 1)
output := writer.Bytes()
assert.True(t, validator.Match(output))
assert.Equal(t, "test", v)
}
func TestTrace_GivenAStepToDraw_WhenDrawn_ThenDelegatesToInnerStep(t *testing.T) {
mockGraph := new(mockGraph)
mockStep := new(mockStep[any, any])
mockStep.On("Draw", mockGraph).Once()
step := pipeline.NewTracedStep[any, any]("test name", mockStep)
step.Draw(mockGraph)
mockGraph.AssertExpectations(t)
mockStep.AssertExpectations(t)
}