-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper_internal_test.go
191 lines (171 loc) · 4.25 KB
/
helper_internal_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package grpcsteps
import (
"testing"
"github.com/stretchr/testify/assert"
"go.nhat.io/grpcmock/service"
"google.golang.org/grpc/codes"
"github.com/godogx/grpcsteps/internal/grpctest"
)
func TestToPayload(t *testing.T) {
t.Parallel()
strPtr := func(s string) *string {
return &s
}
testCases := []struct {
scenario string
methodType service.Type
data *string
expectedResult interface{}
expectedError string
}{
{
scenario: "invalid data for unary",
methodType: service.TypeUnary,
data: strPtr("42"),
expectedError: "json: cannot unmarshal number into Go value of type grpctest.Item",
},
{
scenario: "invalid data for stream",
methodType: service.TypeClientStream,
data: strPtr(`{"id": 42}`),
expectedError: "json: cannot unmarshal object into Go value of type []*grpctest.Item",
},
{
scenario: "unary payload",
methodType: service.TypeUnary,
data: strPtr(`{"id": 42}`),
expectedResult: &grpctest.Item{Id: 42},
},
{
scenario: "unary payload (nil)",
methodType: service.TypeUnary,
expectedResult: (*grpctest.Item)(nil),
},
{
scenario: "server stream payload",
methodType: service.TypeServerStream,
data: strPtr(`{"id": 42}`),
expectedResult: &grpctest.Item{Id: 42},
},
{
scenario: "server stream payload (nil)",
methodType: service.TypeServerStream,
expectedResult: (*grpctest.Item)(nil),
},
{
scenario: "client stream payload",
methodType: service.TypeClientStream,
data: strPtr(`[{"id": 42}]`),
expectedResult: []*grpctest.Item{{Id: 42}},
},
{
scenario: "client stream payload (nil)",
methodType: service.TypeClientStream,
expectedResult: ([]*grpctest.Item)(nil),
},
{
scenario: "bidirectional stream payload",
methodType: service.TypeBidirectionalStream,
data: strPtr(`[{"id": 42}]`),
expectedResult: []*grpctest.Item{{Id: 42}},
},
{
scenario: "bidirectional stream payload (nil)",
methodType: service.TypeBidirectionalStream,
expectedResult: ([]*grpctest.Item)(nil),
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.scenario, func(t *testing.T) {
t.Parallel()
result, err := toPayload(tc.methodType, &grpctest.Item{}, tc.data)
assert.Equal(t, tc.expectedResult, result)
if tc.expectedError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tc.expectedError)
}
})
}
}
func TestToStatusCode(t *testing.T) {
t.Parallel()
testCases := []struct {
scenario string
code string
expectedResult codes.Code
expectedError string
}{
{
scenario: "invalid code",
code: "not a code",
expectedResult: codes.Unknown,
expectedError: `invalid code: "\"NOT A CODE\""`,
},
{
scenario: "screaming snake case",
code: "DEADLINE_EXCEEDED",
expectedResult: codes.DeadlineExceeded,
},
{
scenario: "camel case",
code: "DeadlineExceeded",
expectedResult: codes.DeadlineExceeded,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.scenario, func(t *testing.T) {
t.Parallel()
result, err := toStatusCode(tc.code)
assert.Equal(t, tc.expectedResult, result)
if tc.expectedError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tc.expectedError)
}
})
}
}
func TestToUpperSnakeCase(t *testing.T) {
t.Parallel()
testCases := []struct {
scenario string
value string
expected string
}{
{
scenario: "PascalCase",
value: "NotImplemented",
expected: "NOT_IMPLEMENTED",
},
{
scenario: "camelCase",
value: "notImplemented",
expected: "NOT_IMPLEMENTED",
},
{
scenario: "snake_case",
value: "not_implemented",
expected: "NOT_IMPLEMENTED",
},
{
scenario: "UPPER_SNAKE_CASE",
value: "NOT_IMPLEMENTED",
expected: "NOT_IMPLEMENTED",
},
{
scenario: "kebab-case is not supported",
value: "not-implemented",
expected: "NOT-IMPLEMENTED",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.scenario, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.expected, toUpperSnakeCase(tc.value))
})
}
}