-
Notifications
You must be signed in to change notification settings - Fork 2
/
default_test.go
121 lines (94 loc) · 3.24 KB
/
default_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package gotoprom
import (
"errors"
"reflect"
"testing"
"github.com/cabify/gotoprom/prometheusvanilla"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestMustAddBuilder(t *testing.T) {
initializerMock, tearDown := mockDefaultInitializer()
defer tearDown()
defer initializerMock.AssertExpectations(t)
expectedErr := errors.New("my err")
typ := prometheusvanilla.HistogramType
builder := func(
name, help, namespace string,
labelNames []string,
tag reflect.StructTag,
) (func(prometheus.Labels) interface{}, prometheus.Collector, error) {
return nil, nil, expectedErr
}
initializerMock.On("MustAddBuilder", typ, mock.Anything).Run(func(args mock.Arguments) {
// we can't assert that two functions are the same, so we invoke it and see if it's ours
_, _, err := args[1].(Builder)("", "", "", nil, reflect.StructTag(""))
assert.Equal(t, expectedErr, err)
}).Once()
MustAddBuilder(typ, builder)
}
func TestAddBuilder(t *testing.T) {
initializerMock, tearDown := mockDefaultInitializer()
defer tearDown()
defer initializerMock.AssertExpectations(t)
expectedErr := errors.New("my err")
typ := prometheusvanilla.HistogramType
builder := func(
name, help, namespace string,
labelNames []string,
tag reflect.StructTag,
) (func(prometheus.Labels) interface{}, prometheus.Collector, error) {
return nil, nil, expectedErr
}
initializerMock.On("AddBuilder", typ, mock.Anything).Run(func(args mock.Arguments) {
// we can't assert that two functions are the same, so we invoke it and see if it's ours
_, _, err := args[1].(Builder)("", "", "", nil, reflect.StructTag(""))
assert.Equal(t, expectedErr, err)
}).Return(expectedErr).Once()
err := AddBuilder(typ, builder)
assert.Equal(t, expectedErr, err)
}
func TestInit(t *testing.T) {
initializerMock, tearDown := mockDefaultInitializer()
defer tearDown()
defer initializerMock.AssertExpectations(t)
expectedErr := errors.New("my err")
metrics := struct{ whatever int }{}
namespace := "some namespace"
initializerMock.On("Init", metrics, namespace).Return(expectedErr).Once()
err := Init(metrics, namespace)
assert.Equal(t, expectedErr, err)
}
func TestMustInit(t *testing.T) {
initializerMock, tearDown := mockDefaultInitializer()
defer tearDown()
defer initializerMock.AssertExpectations(t)
metrics := struct{ whatever int }{}
namespace := "some namespace"
initializerMock.On("MustInit", metrics, namespace).Once()
MustInit(metrics, namespace)
}
func mockDefaultInitializer() (mock *InitializerMock, tearDown func()) {
original := DefaultInitializer
mock = &InitializerMock{}
DefaultInitializer = mock
return mock, func() { DefaultInitializer = original }
}
type InitializerMock struct {
mock.Mock
}
func (m *InitializerMock) MustAddBuilder(typ reflect.Type, registerer Builder) {
m.Called(typ, registerer)
}
func (m *InitializerMock) AddBuilder(typ reflect.Type, registerer Builder) error {
ret := m.Called(typ, registerer)
return ret[0].(error)
}
func (m *InitializerMock) MustInit(metrics interface{}, namespace string) {
m.Called(metrics, namespace)
}
func (m *InitializerMock) Init(metrics interface{}, namespace string) error {
ret := m.Called(metrics, namespace)
return ret[0].(error)
}