-
Notifications
You must be signed in to change notification settings - Fork 2
/
rpc_test.go
382 lines (336 loc) · 12 KB
/
rpc_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package raiden_test
import (
"fmt"
"testing"
"time"
"github.com/sev-2/raiden"
"github.com/sev-2/raiden/pkg/mock"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
)
type Scouter struct{}
type Candidate struct{}
type Submission struct{}
type GetSubmissionsParams struct {
ScouterName string `json:"scouter_name" column:"name:scouter_name;type:varchar"`
CandidateName string `json:"candidate_name" column:"name:candidate_name;type:text"`
}
type GetSubmissionsItem struct {
Id int64 `json:"id" column:"name:id;type:integer"`
CreatedAt time.Time `json:"created_at" column:"name:created_at;type:timestamp"`
ScName string `json:"sc_name" column:"name:sc_name;type:varchar"`
CName string `json:"c_name" column:"name:c_name;type:varchar"`
}
type GetSubmissionsResult []GetSubmissionsItem
type GetSubmissions struct {
raiden.RpcBase
Params *GetSubmissionsParams `json:"-"`
Return GetSubmissionsResult `json:"-"`
}
func (r *GetSubmissions) GetName() string {
return "get_submissions"
}
func (r *GetSubmissions) UseParamPrefix() bool {
return false
}
func (r *GetSubmissions) GetReturnType() raiden.RpcReturnDataType {
return raiden.RpcReturnDataTypeTable
}
func (r *GetSubmissions) BindModels() {
r.BindModel(Submission{}, "s").BindModel(Scouter{}, "sc").BindModel(Candidate{}, "c")
}
func (r *GetSubmissions) GetRawDefinition() string {
return `BEGIN RETURN QUERY SELECT s.id, s.created_at, sc.name as sc_name, c.name as c_name FROM :s s INNER JOIN :sc sc ON s.scouter_id = sc.scouter_id INNER JOIN :c c ON s.candidate_id = c.candidate_id WHERE sc.name = :scouter_name AND c.name = :candidate_name; END;`
}
type RpcWithMissingReturn struct {
raiden.RpcBase
Params *GetSubmissionsParams `json:"-"`
}
func TestCreateQuery(t *testing.T) {
rpc := &GetSubmissions{}
e := raiden.BuildRpc(rpc)
assert.NoError(t, e)
assert.Equal(t, "get_submissions", rpc.GetName())
assert.Equal(t, "public", rpc.GetSchema())
expectedCompleteQuery := "create or replace function public.get_submissions(scouter_name character varying, candidate_name text) returns table(id integer, created_at timestamp without time zone, sc_name character varying, c_name character varying) language plpgsql set search_path = '' as $function$ begin return query select s.id, s.created_at, sc.name as sc_name, c.name as c_name from submission s inner join scouter sc on s.scouter_id = sc.scouter_id inner join candidate c on s.candidate_id = c.candidate_id where sc.name = scouter_name and c.name = candidate_name ; end; $function$"
assert.Equal(t, expectedCompleteQuery, rpc.GetCompleteStmt())
}
func TestExecuteRpc(t *testing.T) {
mockCtx := &mock.MockContext{
ConfigFn: func() *raiden.Config {
return &raiden.Config{
DeploymentTarget: raiden.DeploymentTargetCloud,
ProjectId: "test-project-id",
ProjectName: "My Great Project",
SupabaseApiBasePath: "/v1",
SupabaseApiUrl: "http://supabase.cloud.com",
SupabasePublicUrl: "http://supabase.cloud.com",
}
},
RequestContextFn: func() *fasthttp.RequestCtx {
rCtx := &fasthttp.RequestCtx{
Request: fasthttp.Request{
Header: fasthttp.RequestHeader{},
},
}
rCtx.Request.Header.Set("Authorization", "Bearer some token")
rCtx.Request.Header.Set("apiKey", "some api key")
return rCtx
},
}
mock := mock.MockSupabase{Cfg: mockCtx.Config()}
mock.Activate()
defer mock.Deactivate()
err := mock.MockExecuteRpcWithExpectedResponse(200, "get_submissions", GetSubmissionsResult{})
assert.NoError(t, err)
rpc := &GetSubmissions{
Params: &GetSubmissionsParams{
ScouterName: "test_1",
CandidateName: "test_2",
},
}
res, err := raiden.ExecuteRpc(mockCtx, rpc)
assert.NoError(t, err)
assert.NotNil(t, res)
}
func TestExecuteRpcWithParams(t *testing.T) {
requestCtx := &fasthttp.RequestCtx{
Request: fasthttp.Request{},
}
requestCtx.Request.URI().QueryArgs().Set("limit", "10")
mockCtx := &mock.MockContext{
ConfigFn: func() *raiden.Config {
return &raiden.Config{
DeploymentTarget: raiden.DeploymentTargetCloud,
ProjectId: "test-project-id",
ProjectName: "My Great Project",
SupabaseApiBasePath: "/v1",
SupabaseApiUrl: "http://supabase.cloud.com",
SupabasePublicUrl: "http://supabase.cloud.com",
}
},
RequestContextFn: func() *fasthttp.RequestCtx {
rCtx := &fasthttp.RequestCtx{
Request: fasthttp.Request{
Header: fasthttp.RequestHeader{},
},
}
rCtx.Request.Header.Set("Authorization", "Bearer some token")
rCtx.Request.Header.Set("apiKey", "some api key")
return rCtx
},
}
mock := mock.MockSupabase{Cfg: mockCtx.Config()}
mock.Activate()
defer mock.Deactivate()
err := mock.MockExecuteRpcWithExpectedResponse(401, "get_submissions", map[string]interface{}{
"message": "Invalid API key",
"status": 401,
"code": "invalid_auth",
})
assert.NoError(t, err)
rpc := &GetSubmissions{
Params: &GetSubmissionsParams{
ScouterName: "test_1",
CandidateName: "test_2",
},
}
res, err := raiden.ExecuteRpc(mockCtx, rpc)
assert.Error(t, err)
assert.Nil(t, res)
}
func TestExecuteRpcErrWithMissingReturn(t *testing.T) {
mockCtx := &mock.MockContext{
ConfigFn: func() *raiden.Config {
return &raiden.Config{
DeploymentTarget: raiden.DeploymentTargetCloud,
ProjectId: "test-project-id",
ProjectName: "My Great Project",
SupabaseApiBasePath: "/v1",
SupabaseApiUrl: "http://supabase.cloud.com",
SupabasePublicUrl: "http://supabase.cloud.com",
}
},
RequestContextFn: func() *fasthttp.RequestCtx {
return &fasthttp.RequestCtx{}
},
}
rpc := &RpcWithMissingReturn{}
_, err := raiden.ExecuteRpc(mockCtx, rpc)
expectedErr := &raiden.ErrorResponse{
StatusCode: fasthttp.StatusInternalServerError,
Details: fmt.Sprintf("Struct %s doesn`t have Return field, define first because this attribute need for receive data from server", "RpcWithMissingReturn"),
Message: fmt.Sprintf("Undefined field Return in struct %s", "RpcWithMissingReturn"),
Hint: "Invalid Rpc",
Code: fasthttp.StatusMessage(fasthttp.StatusInternalServerError),
}
assert.Error(t, err)
assert.EqualError(t, expectedErr, err.Error())
mock := mock.MockSupabase{Cfg: mockCtx.Config()}
mock.Activate()
defer mock.Deactivate()
err = mock.MockExecuteRpcWithExpectedResponse(200, "get_submissions", GetSubmissionsResult{})
assert.NoError(t, err)
}
func TestRpcParamToGoType(t *testing.T) {
tests := []struct {
input raiden.RpcParamDataType
expected string
}{
{raiden.RpcParamDataTypeInteger, "int64"},
{raiden.RpcParamDataTypeBigInt, "int64"},
{raiden.RpcParamDataTypeReal, "float32"},
{raiden.RpcParamDataTypeDoublePreci, "float64"},
{raiden.RpcParamDataTypeText, "string"},
{raiden.RpcParamDataTypeVarchar, "string"},
{raiden.RpcParamDataTypeVarcharAlias, "string"},
{raiden.RpcParamDataTypeBoolean, "bool"},
{raiden.RpcParamDataTypeBytea, "[]byte"},
{raiden.RpcParamDataTypeTimestamp, "time.Time"},
{raiden.RpcParamDataTypeTimestampTZ, "time.Time"},
{raiden.RpcParamDataTypeJSON, "map[string]interface{}"},
{raiden.RpcParamDataTypeJSONB, "map[string]interface{}"},
}
for _, tt := range tests {
assert.Equal(t, tt.expected, raiden.RpcParamToGoType(tt.input))
}
}
func TestGetValidRpcParamType(t *testing.T) {
tests := []struct {
input string
returnAlias bool
expected raiden.RpcParamDataType
expectErr bool
}{
{"integer", false, raiden.RpcParamDataTypeInteger, false},
{"bigint", false, raiden.RpcParamDataTypeBigInt, false},
{"real", false, raiden.RpcParamDataTypeReal, false},
{"double precision", false, raiden.RpcParamDataTypeDoublePreci, false},
{"varchar", false, raiden.RpcParamDataTypeVarchar, false},
{"varchar", true, raiden.RpcParamDataTypeVarcharAlias, false},
{"boolean", true, raiden.RpcParamDataTypeBoolean, false},
{"bytea", true, raiden.RpcParamDataTypeBytea, false},
{"timestamp", true, raiden.RpcParamDataTypeTimestampAlias, false},
{"unsupported", false, "", true},
}
for _, tt := range tests {
result, err := raiden.GetValidRpcParamType(tt.input, tt.returnAlias)
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
}
}
func TestRpcReturnToGoType(t *testing.T) {
tests := []struct {
input raiden.RpcReturnDataType
expected string
}{
{raiden.RpcReturnDataTypeInteger, "int64"},
{raiden.RpcReturnDataTypeBigInt, "int64"},
{raiden.RpcReturnDataTypeReal, "float32"},
{raiden.RpcReturnDataTypeDoublePreci, "float64"},
{raiden.RpcReturnDataTypeText, "string"},
{raiden.RpcReturnDataTypeVarchar, "string"},
{raiden.RpcReturnDataTypeBoolean, "bool"},
{raiden.RpcReturnDataTypeBytea, "[]byte"},
{raiden.RpcReturnDataTypeTimestamp, "time.Time"},
{raiden.RpcReturnDataTypeTimestampTZ, "time.Time"},
{raiden.RpcReturnDataTypeJSON, "map[string]interface{}"},
{raiden.RpcReturnDataTypeJSONB, "map[string]interface{}"},
}
for _, tt := range tests {
assert.Equal(t, tt.expected, raiden.RpcReturnToGoType(tt.input))
}
}
func TestGetValidRpcReturnType(t *testing.T) {
tests := []struct {
input string
returnAlias bool
expected raiden.RpcReturnDataType
expectErr bool
}{
{"integer", false, raiden.RpcReturnDataTypeInteger, false},
{"bigint", false, raiden.RpcReturnDataTypeBigInt, false},
{"real", false, raiden.RpcReturnDataTypeReal, false},
{"double precision", false, raiden.RpcReturnDataTypeDoublePreci, false},
{"varchar", false, raiden.RpcReturnDataTypeVarchar, false},
{"varchar", true, raiden.RpcReturnDataTypeVarcharAlias, false},
{"boolean", true, raiden.RpcReturnDataTypeBoolean, false},
{"bytea", true, raiden.RpcReturnDataTypeBytea, false},
{"timestamp", true, raiden.RpcReturnDataTypeTimestampAlias, false},
{"unsupported", false, "", true},
}
for _, tt := range tests {
result, err := raiden.GetValidRpcReturnType(tt.input, tt.returnAlias)
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
}
}
func TestGetValidRpcReturnNameDecl(t *testing.T) {
tests := []struct {
input raiden.RpcReturnDataType
returnAlias bool
expected string
expectErr bool
}{
{raiden.RpcReturnDataTypeInteger, false, "RpcReturnDataTypeInteger", false},
{raiden.RpcReturnDataTypeVarchar, false, "RpcReturnDataTypeVarchar", false},
{raiden.RpcReturnDataTypeVarchar, true, "RpcReturnDataTypeVarcharAlias", false},
{raiden.RpcReturnDataTypeJSON, false, "RpcReturnDataTypeJSON", false},
{raiden.RpcReturnDataTypeSetOf, false, "RpcReturnDataTypeSetOf", false},
{raiden.RpcReturnDataTypeTable, false, "RpcReturnDataTypeTable", false},
{raiden.RpcReturnDataTypeVoid, false, "RpcReturnDataTypeVoid", false},
{raiden.RpcReturnDataType("unsupported"), false, "", true},
}
for _, tt := range tests {
result, err := raiden.GetValidRpcReturnNameDecl(tt.input, tt.returnAlias)
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
}
}
func TestMarshalRpcParamTag(t *testing.T) {
tests := []struct {
input *raiden.RpcParamTag
expected string
}{
{&raiden.RpcParamTag{Name: "id", Type: "integer", DefaultValue: "1"}, "name:id;type:integer;default:1"},
{&raiden.RpcParamTag{Name: "name", Type: "varchar"}, "name:name;type:varchar"},
{nil, ""},
}
for _, tt := range tests {
result, err := raiden.MarshalRpcParamTag(tt.input)
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
}
func TestUnmarshalRpcParamTag(t *testing.T) {
tests := []struct {
input string
expected raiden.RpcParamTag
expectErr bool
}{
{"name:id;type:integer;default:1", raiden.RpcParamTag{Name: "id", Type: "INTEGER", DefaultValue: "1"}, false},
{"name:name;type:varchar", raiden.RpcParamTag{Name: "name", Type: "VARCHAR"}, false},
{"type:rand;", raiden.RpcParamTag{}, true},
}
for _, tt := range tests {
result, err := raiden.UnmarshalRpcParamTag(tt.input)
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
}
}