github.com/lovung/GoCleanArchitecture@v0.0.0-20210302152432-50d91fd29f9f/app/internal/usecase/interactor/user_usecase_test.go (about) 1 package interactor 2 3 import ( 4 "context" 5 "errors" 6 "reflect" 7 "testing" 8 9 "github.com/lovung/GoCleanArchitecture/app/internal/domain/entity" 10 "github.com/lovung/GoCleanArchitecture/app/internal/domain/repository" 11 "github.com/lovung/GoCleanArchitecture/app/internal/domain/repository/mockrepo" 12 "github.com/lovung/GoCleanArchitecture/app/internal/usecase/dto" 13 14 "github.com/golang/mock/gomock" 15 ) 16 17 func TestUserUseCase_Create(t *testing.T) { 18 type fields struct { 19 userRepo repository.UserRepository 20 } 21 type args struct { 22 ctx context.Context 23 candidate dto.CreateUserRequest 24 } 25 type wants struct { 26 wantCreated dto.OneUserResponse 27 wantErr bool 28 } 29 30 t.Parallel() 31 mockCtrl := gomock.NewController(t) 32 defer mockCtrl.Finish() 33 ctx := context.Background() 34 runFunc := func(t *testing.T, fields fields, args args, w wants) { 35 u := &UserUseCase{ 36 userRepo: fields.userRepo, 37 } 38 gotCreated, err := u.Create(args.ctx, args.candidate) 39 if (err != nil) != w.wantErr { 40 t.Errorf("UserUseCase.Create() error = %v, wantErr %v", err, w.wantErr) 41 return 42 } 43 if !reflect.DeepEqual(gotCreated, w.wantCreated) { 44 t.Errorf("UserUseCase.Create() = %v, want %v", gotCreated, w.wantCreated) 45 } 46 } 47 48 mUserRepo := mockrepo.NewMockUserRepository(mockCtrl) 49 _fields := fields{ 50 userRepo: mUserRepo, 51 } 52 53 t.Run("#1: Create new user failed", func(t *testing.T) { 54 mUserRepo.EXPECT().Create(ctx, gomock.Any()). 55 Return(entity.User{}, errors.New("error when creating")) 56 runFunc(t, _fields, args{ 57 ctx, 58 dto.CreateUserRequest{ 59 Password: "password", 60 Username: "username", 61 }, 62 }, wants{ 63 dto.OneUserResponse{}, 64 true, 65 }) 66 }) 67 t.Run("#2: Success", func(t *testing.T) { 68 mUserRepo.EXPECT().Create(ctx, gomock.Any()). 69 Return(entity.User{ 70 ID: "01ENSP2J4H9QRMWYJ3ZQTYEGYJ", 71 Username: "username", 72 }, nil) 73 runFunc(t, _fields, args{ 74 ctx, 75 dto.CreateUserRequest{ 76 Password: "password", 77 Username: "username", 78 }, 79 }, wants{ 80 dto.OneUserResponse{ 81 ID: "01ENSP2J4H9QRMWYJ3ZQTYEGYJ", 82 Username: "username", 83 }, 84 false, 85 }) 86 }) 87 } 88 89 func TestNewUserUseCase(t *testing.T) { 90 type args struct { 91 userRepo repository.UserRepository 92 } 93 t.Parallel() 94 mockCtrl := gomock.NewController(t) 95 defer mockCtrl.Finish() 96 mUserRepo := mockrepo.NewMockUserRepository(mockCtrl) 97 tests := []struct { 98 name string 99 args args 100 want *UserUseCase 101 }{ 102 { 103 args: args{ 104 userRepo: mUserRepo, 105 }, 106 want: &UserUseCase{ 107 userRepo: mUserRepo, 108 }, 109 }, 110 } 111 for _, tt := range tests { 112 t.Run(tt.name, func(t *testing.T) { 113 if got := NewUserUseCase(tt.args.userRepo); !reflect.DeepEqual(got, tt.want) { 114 t.Errorf("NewUserUseCase() = %v, want %v", got, tt.want) 115 } 116 }) 117 } 118 } 119 120 func TestUserUseCase_GetByID(t *testing.T) { 121 type fields struct { 122 userRepo repository.UserRepository 123 } 124 type args struct { 125 ctx context.Context 126 id interface{} 127 } 128 type wants struct { 129 wantExist dto.OneUserResponse 130 wantErr bool 131 } 132 t.Parallel() 133 mockCtrl := gomock.NewController(t) 134 defer mockCtrl.Finish() 135 ctx := context.Background() 136 runFunc := func(t *testing.T, fields fields, args args, w wants) { 137 u := &UserUseCase{ 138 userRepo: fields.userRepo, 139 } 140 gotExist, err := u.GetByID(args.ctx, args.id) 141 if (err != nil) != w.wantErr { 142 t.Errorf("UserUseCase.GetByID() error = %v, wantErr %v", err, w.wantErr) 143 return 144 } 145 if !reflect.DeepEqual(gotExist, w.wantExist) { 146 t.Errorf("UserUseCase.GetByID() = %v, want %v", gotExist, w.wantExist) 147 } 148 } 149 mUserRepo := mockrepo.NewMockUserRepository(mockCtrl) 150 _fields := fields{ 151 userRepo: mUserRepo, 152 } 153 154 t.Run("#1: Get user by ID failed", func(t *testing.T) { 155 mUserRepo.EXPECT().GetByID(ctx, "01ENSP2J4H9QRMWYJ3ZQTYEGYJ"). 156 Return(entity.User{}, errors.New("error or not found")) 157 runFunc(t, _fields, args{ 158 ctx, 159 "01ENSP2J4H9QRMWYJ3ZQTYEGYJ", 160 }, wants{ 161 dto.OneUserResponse{}, 162 true, 163 }) 164 }) 165 t.Run("#2: Found", func(t *testing.T) { 166 mUserRepo.EXPECT().GetByID(ctx, "01ENSP2J4H9QRMWYJ3ZQTYEGYJ"). 167 Return(entity.User{ 168 ID: "01ENSP2J4H9QRMWYJ3ZQTYEGYJ", 169 Username: "username", 170 }, nil) 171 runFunc(t, _fields, args{ 172 ctx, 173 "01ENSP2J4H9QRMWYJ3ZQTYEGYJ", 174 }, wants{ 175 dto.OneUserResponse{ 176 ID: "01ENSP2J4H9QRMWYJ3ZQTYEGYJ", 177 Username: "username", 178 }, 179 false, 180 }) 181 }) 182 }