github.com/erda-project/erda-infra@v1.0.9/examples/service/server/helloworld/user.service_test.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package example 16 17 import ( 18 "context" 19 "reflect" 20 "testing" 21 22 "github.com/erda-project/erda-infra/base/servicehub" 23 "github.com/erda-project/erda-infra/examples/service/protocol/pb" 24 ) 25 26 func Test_userService_GetUser(t *testing.T) { 27 type args struct { 28 ctx context.Context 29 req *pb.GetUserRequest 30 } 31 tests := []struct { 32 name string 33 service string 34 config string 35 args args 36 wantResp *pb.GetUserResponse 37 wantErr bool 38 }{ 39 // TODO: Add test cases. 40 // { 41 // "case 1", 42 // "erda.infra.example.UserService", 43 // `erda.infra.example:`, 44 // args{ 45 // context.TODO(), 46 // &pb.GetUserRequest{ 47 // // TODO: setup fields 48 // }, 49 // }, 50 // &pb.GetUserResponse{ 51 // // TODO: setup fields. 52 // }, 53 // false, 54 // }, 55 } 56 for _, tt := range tests { 57 t.Run(tt.name, func(t *testing.T) { 58 hub := servicehub.New() 59 events := hub.Events() 60 go func() { 61 hub.RunWithOptions(&servicehub.RunOptions{Content: tt.config}) 62 }() 63 err := <-events.Started() 64 if err != nil { 65 t.Error(err) 66 return 67 } 68 srv := hub.Service(tt.service).(pb.UserServiceServer) 69 got, err := srv.GetUser(tt.args.ctx, tt.args.req) 70 if (err != nil) != tt.wantErr { 71 t.Errorf("userService.GetUser() error = %v, wantErr %v", err, tt.wantErr) 72 return 73 } 74 if !reflect.DeepEqual(got, tt.wantResp) { 75 t.Errorf("userService.GetUser() = %v, want %v", got, tt.wantResp) 76 } 77 }) 78 } 79 } 80 81 func Test_userService_UpdateUser(t *testing.T) { 82 type args struct { 83 ctx context.Context 84 req *pb.UpdateUserRequest 85 } 86 tests := []struct { 87 name string 88 service string 89 config string 90 args args 91 wantResp *pb.UpdateUserResponse 92 wantErr bool 93 }{ 94 // TODO: Add test cases. 95 // { 96 // "case 1", 97 // "erda.infra.example.UserService", 98 // `erda.infra.example:`, 99 // args{ 100 // context.TODO(), 101 // &pb.UpdateUserRequest{ 102 // // TODO: setup fields 103 // }, 104 // }, 105 // &pb.UpdateUserResponse{ 106 // // TODO: setup fields. 107 // }, 108 // false, 109 // }, 110 } 111 for _, tt := range tests { 112 t.Run(tt.name, func(t *testing.T) { 113 hub := servicehub.New() 114 events := hub.Events() 115 go func() { 116 hub.RunWithOptions(&servicehub.RunOptions{Content: tt.config}) 117 }() 118 err := <-events.Started() 119 if err != nil { 120 t.Error(err) 121 return 122 } 123 srv := hub.Service(tt.service).(pb.UserServiceServer) 124 got, err := srv.UpdateUser(tt.args.ctx, tt.args.req) 125 if (err != nil) != tt.wantErr { 126 t.Errorf("userService.UpdateUser() error = %v, wantErr %v", err, tt.wantErr) 127 return 128 } 129 if !reflect.DeepEqual(got, tt.wantResp) { 130 t.Errorf("userService.UpdateUser() = %v, want %v", got, tt.wantResp) 131 } 132 }) 133 } 134 }