github.com/cloudwego/kitex@v0.9.0/pkg/generic/descriptor/value_mapping_test.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package descriptor 18 19 import ( 20 "context" 21 "reflect" 22 "testing" 23 ) 24 25 func Test_apiJSConv_Request(t *testing.T) { 26 type args struct { 27 ctx context.Context 28 val interface{} 29 field *FieldDescriptor 30 } 31 tests := []struct { 32 name string 33 args args 34 want interface{} 35 wantErr bool 36 }{ 37 // TODO: Add test cases. 38 {"string", args{context.Background(), "124", nil}, int64(124), false}, 39 {"not_valid_string", args{context.Background(), "124x", nil}, int64(0), false}, 40 {"[]string", args{context.Background(), []interface{}{"124", "567"}, nil}, []interface{}{int64(124), int64(567)}, false}, 41 {"not_valid_[]string", args{context.Background(), []interface{}{"124x", "567"}, nil}, []interface{}{int64(0), int64(567)}, false}, 42 {"map[string]string", args{context.Background(), map[string]interface{}{"a": "123"}, nil}, map[string]interface{}{"a": int64(123)}, false}, 43 {"map[string][]string", args{context.Background(), map[string]interface{}{"a": []interface{}{"123", "321"}}, nil}, map[string]interface{}{"a": []interface{}{int64(123), int64(321)}}, false}, 44 {"map[int][]string", args{context.Background(), map[interface{}]interface{}{789: []interface{}{"123", "321"}}, nil}, map[interface{}]interface{}{789: []interface{}{int64(123), int64(321)}}, false}, 45 {"map[int]map[string]string", args{context.Background(), map[interface{}]interface{}{789: map[string]interface{}{"a": "123"}}, nil}, map[interface{}]interface{}{789: map[string]interface{}{"a": int64(123)}}, false}, 46 } 47 for _, tt := range tests { 48 t.Run(tt.name, func(t *testing.T) { 49 m := &apiJSConv{} 50 got, err := m.Request(tt.args.ctx, tt.args.val, tt.args.field) 51 if (err != nil) != tt.wantErr { 52 t.Errorf("apiJSConv.Request() error = %v, wantErr %v", err, tt.wantErr) 53 return 54 } 55 if !reflect.DeepEqual(got, tt.want) { 56 t.Errorf("apiJSConv.Request() = %v, want %v", got, tt.want) 57 } 58 }) 59 } 60 } 61 62 func Test_apiJSConv_Response(t *testing.T) { 63 type args struct { 64 ctx context.Context 65 val interface{} 66 field *FieldDescriptor 67 } 68 tests := []struct { 69 name string 70 args args 71 want interface{} 72 wantErr bool 73 }{ 74 // TODO: Add test cases. 75 // TODO: Add test cases. 76 {"int64", args{context.Background(), int64(124), nil}, "124", false}, 77 {"[]int64", args{context.Background(), []interface{}{int64(124), int64(567)}, nil}, []interface{}{"124", "567"}, false}, 78 {"map[string]i64", args{context.Background(), map[string]interface{}{"a": int64(123)}, nil}, map[string]interface{}{"a": "123"}, false}, 79 {"map[string][]i64", args{context.Background(), map[string]interface{}{"a": []interface{}{int64(123), int64(321)}}, nil}, map[string]interface{}{"a": []interface{}{"123", "321"}}, false}, 80 {"map[int][]i64", args{context.Background(), map[interface{}]interface{}{789: []interface{}{int64(123), int64(321)}}, nil}, map[interface{}]interface{}{789: []interface{}{"123", "321"}}, false}, 81 {"map[int]map[string]i64", args{context.Background(), map[interface{}]interface{}{789: map[string]interface{}{"a": int64(123)}}, nil}, map[interface{}]interface{}{789: map[string]interface{}{"a": "123"}}, false}, 82 } 83 for _, tt := range tests { 84 t.Run(tt.name, func(t *testing.T) { 85 m := &apiJSConv{} 86 got, err := m.Response(tt.args.ctx, tt.args.val, tt.args.field) 87 if (err != nil) != tt.wantErr { 88 t.Errorf("apiJSConv.Response() error = %v, wantErr %v", err, tt.wantErr) 89 return 90 } 91 if !reflect.DeepEqual(got, tt.want) { 92 t.Errorf("apiJSConv.Response() = %v, want %v", got, tt.want) 93 } 94 }) 95 } 96 }