github.com/condensat/bank-core@v0.1.0/database/query/withdrawinfo_test.go (about) 1 // Copyright 2020 Condensat Tech. All rights reserved. 2 // Use of this source code is governed by a MIT 3 // license that can be found in the LICENSE file. 4 5 package query 6 7 import ( 8 "reflect" 9 "testing" 10 "time" 11 12 "github.com/condensat/bank-core/database/model" 13 "github.com/condensat/bank-core/database/query/tests" 14 ) 15 16 func TestAddWithdrawInfo(t *testing.T) { 17 const databaseName = "TestAddWithdrawInfo" 18 t.Parallel() 19 20 db := tests.Setup(databaseName, WithdrawModel()) 21 defer tests.Teardown(db, databaseName) 22 23 data := createTestAccountStateData(db) 24 a1 := data.Accounts[0] 25 a2 := data.Accounts[2] 26 27 ref, _ := AddWithdraw(db, a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}") 28 29 type args struct { 30 withdrawID model.WithdrawID 31 status model.WithdrawStatus 32 data model.WithdrawInfoData 33 } 34 tests := []struct { 35 name string 36 args args 37 want model.WithdrawInfo 38 wantErr bool 39 }{ 40 {"default", args{}, model.WithdrawInfo{}, true}, 41 {"invalid_withdraw", args{0, model.WithdrawStatusCreated, "{}"}, model.WithdrawInfo{}, true}, 42 {"invalid_status", args{ref.ID, "", "{}"}, model.WithdrawInfo{}, true}, 43 44 {"default_data", args{ref.ID, model.WithdrawStatusCreated, ""}, createWithdrawInfo(ref.ID, model.WithdrawStatusCreated, "{}"), false}, 45 {"valid", args{ref.ID, model.WithdrawStatusCreated, "{}"}, createWithdrawInfo(ref.ID, model.WithdrawStatusCreated, "{}"), false}, 46 } 47 for _, tt := range tests { 48 tt := tt // capture range variable 49 t.Run(tt.name, func(t *testing.T) { 50 got, err := AddWithdrawInfo(db, tt.args.withdrawID, tt.args.status, tt.args.data) 51 52 if !tt.wantErr { 53 if got.Timestamp.IsZero() || got.Timestamp.After(time.Now()) { 54 t.Errorf("AddWithdrawInfo() wrong Timestamp %v", got.Timestamp) 55 } 56 } 57 58 if (err != nil) != tt.wantErr { 59 t.Errorf("AddWithdrawInfo() error = %v, wantErr %v", err, tt.wantErr) 60 return 61 } 62 63 tt.want.ID = got.ID 64 tt.want.Timestamp = got.Timestamp 65 if !reflect.DeepEqual(got, tt.want) { 66 t.Errorf("AddWithdrawInfo() = %v, want %v", got, tt.want) 67 } 68 }) 69 } 70 } 71 72 func TestGetWithdrawInfo(t *testing.T) { 73 const databaseName = "TestGetWithdrawInfo" 74 t.Parallel() 75 76 db := tests.Setup(databaseName, WithdrawModel()) 77 defer tests.Teardown(db, databaseName) 78 79 data := createTestAccountStateData(db) 80 a1 := data.Accounts[0] 81 a2 := data.Accounts[2] 82 83 withdraw, _ := AddWithdraw(db, a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}") 84 85 ref, _ := AddWithdrawInfo(db, withdraw.ID, model.WithdrawStatusCreated, "{}") 86 87 type args struct { 88 ID model.WithdrawInfoID 89 } 90 tests := []struct { 91 name string 92 args args 93 want model.WithdrawInfo 94 wantErr bool 95 }{ 96 {"default", args{}, model.WithdrawInfo{}, true}, 97 {"ref", args{ref.ID}, ref, false}, 98 } 99 for _, tt := range tests { 100 tt := tt // capture range variable 101 t.Run(tt.name, func(t *testing.T) { 102 got, err := GetWithdrawInfo(db, tt.args.ID) 103 104 if !tt.wantErr { 105 if got.Timestamp.IsZero() || got.Timestamp.After(time.Now()) { 106 t.Errorf("GetWithdrawInfo() wrong Timestamp %v", got.Timestamp) 107 } 108 } 109 110 if (err != nil) != tt.wantErr { 111 t.Errorf("GetWithdrawInfo() error = %v, wantErr %v", err, tt.wantErr) 112 return 113 } 114 if !reflect.DeepEqual(got, tt.want) { 115 t.Errorf("GetWithdrawInfo() = %v, want %v", got, tt.want) 116 } 117 }) 118 } 119 } 120 121 func TestGetLastWithdrawInfo(t *testing.T) { 122 const databaseName = "TestGetLastWithdrawInfo" 123 t.Parallel() 124 125 db := tests.Setup(databaseName, WithdrawModel()) 126 defer tests.Teardown(db, databaseName) 127 128 data := createTestAccountStateData(db) 129 a1 := data.Accounts[0] 130 a2 := data.Accounts[2] 131 132 ref, _ := AddWithdraw(db, a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}") 133 134 _, _ = AddWithdrawInfo(db, ref.ID, model.WithdrawStatusCreated, "{}") 135 _, _ = AddWithdrawInfo(db, ref.ID, model.WithdrawStatusProcessing, "{}") 136 _, _ = AddWithdrawInfo(db, ref.ID, model.WithdrawStatusCanceled, "{}") 137 ref4, _ := AddWithdrawInfo(db, ref.ID, model.WithdrawStatusSettled, "{}") 138 139 type args struct { 140 withdrawID model.WithdrawID 141 } 142 tests := []struct { 143 name string 144 args args 145 want model.WithdrawInfo 146 wantErr bool 147 }{ 148 {"default", args{}, model.WithdrawInfo{}, true}, 149 {"settled", args{ref.ID}, ref4, false}, 150 } 151 for _, tt := range tests { 152 tt := tt // capture range variable 153 t.Run(tt.name, func(t *testing.T) { 154 got, err := GetLastWithdrawInfo(db, tt.args.withdrawID) 155 if (err != nil) != tt.wantErr { 156 t.Errorf("GetLastWithdrawInfo() error = %v, wantErr %v", err, tt.wantErr) 157 return 158 } 159 if !reflect.DeepEqual(got, tt.want) { 160 t.Errorf("GetLastWithdrawInfo() = %v, want %v", got, tt.want) 161 } 162 }) 163 } 164 } 165 166 func TestGetWithdrawHistory(t *testing.T) { 167 const databaseName = "TestGetWithdrawHistory" 168 t.Parallel() 169 170 db := tests.Setup(databaseName, WithdrawModel()) 171 defer tests.Teardown(db, databaseName) 172 173 data := createTestAccountStateData(db) 174 a1 := data.Accounts[0] 175 a2 := data.Accounts[2] 176 177 ref, _ := AddWithdraw(db, a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}") 178 179 ref1, _ := AddWithdrawInfo(db, ref.ID, model.WithdrawStatusCreated, "{}") 180 ref2, _ := AddWithdrawInfo(db, ref.ID, model.WithdrawStatusProcessing, "{}") 181 ref3, _ := AddWithdrawInfo(db, ref.ID, model.WithdrawStatusCanceled, "{}") 182 ref4, _ := AddWithdrawInfo(db, ref.ID, model.WithdrawStatusSettled, "{}") 183 184 type args struct { 185 withdrawID model.WithdrawID 186 } 187 tests := []struct { 188 name string 189 args args 190 want []model.WithdrawInfo 191 wantErr bool 192 }{ 193 {"default", args{}, nil, true}, 194 {"ref", args{ref.ID}, createWithdrawInfoList(ref1, ref2, ref3, ref4), false}, 195 } 196 for _, tt := range tests { 197 tt := tt // capture range variable 198 t.Run(tt.name, func(t *testing.T) { 199 got, err := GetWithdrawHistory(db, tt.args.withdrawID) 200 if (err != nil) != tt.wantErr { 201 t.Errorf("GetWithdrawHistory() error = %v, wantErr %v", err, tt.wantErr) 202 return 203 } 204 if !reflect.DeepEqual(got, tt.want) { 205 t.Errorf("GetWithdrawHistory() = %v, want %v", got, tt.want) 206 } 207 }) 208 } 209 } 210 211 func TestListCancelingWithdrawsAccountOperations(t *testing.T) { 212 const databaseName = "TestListCancelingWithdrawsAccountOperations" 213 t.Parallel() 214 215 db := tests.Setup(databaseName, WithdrawModel()) 216 defer tests.Teardown(db, databaseName) 217 218 tests := []struct { 219 name string 220 want []model.AccountOperation 221 wantErr bool 222 }{ 223 {"default", nil, false}, 224 } 225 for _, tt := range tests { 226 t.Run(tt.name, func(t *testing.T) { 227 got, err := ListCancelingWithdrawsAccountOperations(db) 228 if (err != nil) != tt.wantErr { 229 t.Errorf("ListCancelingWithdrawsAccountOperations() error = %v, wantErr %v", err, tt.wantErr) 230 return 231 } 232 if !reflect.DeepEqual(got, tt.want) { 233 t.Errorf("ListCancelingWithdrawsAccountOperations() = %v, want %v", got, tt.want) 234 } 235 }) 236 } 237 } 238 239 func createWithdrawInfo(withdrawID model.WithdrawID, status model.WithdrawStatus, data model.WithdrawInfoData) model.WithdrawInfo { 240 return model.WithdrawInfo{ 241 WithdrawID: withdrawID, 242 Status: status, 243 Data: data, 244 } 245 } 246 247 func createWithdrawInfoList(list ...model.WithdrawInfo) []model.WithdrawInfo { 248 return append([]model.WithdrawInfo{}, list...) 249 }