github.com/condensat/bank-core@v0.1.0/database/query/batchinfo_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/encoding" 13 "github.com/condensat/bank-core/database/model" 14 "github.com/condensat/bank-core/database/query/tests" 15 ) 16 17 func TestAddBatchInfo(t *testing.T) { 18 const databaseName = "TestAddBatchInfo" 19 t.Parallel() 20 21 db := tests.Setup(databaseName, WithdrawModel()) 22 defer tests.Teardown(db, databaseName) 23 24 ref, _ := AddBatch(db, model.BatchNetworkBitcoin, "") 25 26 type args struct { 27 batchID model.BatchID 28 status model.BatchStatus 29 dataType encoding.DataType 30 data model.BatchInfoData 31 } 32 tests := []struct { 33 name string 34 args args 35 want model.BatchInfo 36 wantErr bool 37 }{ 38 {"default", args{}, model.BatchInfo{}, true}, 39 {"invalid_status", args{ref.ID, "", model.BatchInfoCrypto, "{}"}, model.BatchInfo{}, true}, 40 {"invalid_datatype", args{ref.ID, model.BatchStatusCreated, "", "{}"}, model.BatchInfo{}, true}, 41 42 {"valid_data", args{ref.ID, model.BatchStatusCreated, model.BatchInfoCrypto, ""}, createBatchInfo(ref.ID, model.BatchStatusCreated, model.BatchInfoCrypto, "{}"), false}, 43 {"valid", args{ref.ID, model.BatchStatusCreated, model.BatchInfoCrypto, "{}"}, createBatchInfo(ref.ID, model.BatchStatusCreated, model.BatchInfoCrypto, "{}"), false}, 44 } 45 for _, tt := range tests { 46 tt := tt // capture range variable 47 t.Run(tt.name, func(t *testing.T) { 48 got, err := AddBatchInfo(db, tt.args.batchID, tt.args.status, tt.args.dataType, tt.args.data) 49 50 if !tt.wantErr { 51 if got.Timestamp.IsZero() || got.Timestamp.After(time.Now()) { 52 t.Errorf("AddBatchInfo() wrong Timestamp %v", got.Timestamp) 53 } 54 } 55 56 if (err != nil) != tt.wantErr { 57 t.Errorf("AddBatchInfo() error = %v, wantErr %v", err, tt.wantErr) 58 return 59 } 60 61 tt.want.ID = got.ID 62 tt.want.Timestamp = got.Timestamp 63 if !reflect.DeepEqual(got, tt.want) { 64 t.Errorf("AddBatchInfo() = %v, want %v", got, tt.want) 65 } 66 }) 67 } 68 } 69 70 func TestGetBatchInfo(t *testing.T) { 71 const databaseName = "TestGetBatchInfo" 72 t.Parallel() 73 74 db := tests.Setup(databaseName, WithdrawModel()) 75 defer tests.Teardown(db, databaseName) 76 77 batch, _ := AddBatch(db, model.BatchNetworkBitcoin, "{}") 78 79 ref, _ := AddBatchInfo(db, batch.ID, model.BatchStatusCreated, model.BatchInfoCrypto, "{}") 80 81 type args struct { 82 ID model.BatchInfoID 83 } 84 tests := []struct { 85 name string 86 args args 87 want model.BatchInfo 88 wantErr bool 89 }{ 90 {"default", args{}, model.BatchInfo{}, true}, 91 {"ref", args{ref.ID}, ref, false}, 92 } 93 for _, tt := range tests { 94 tt := tt // capture range variable 95 t.Run(tt.name, func(t *testing.T) { 96 got, err := GetBatchInfo(db, tt.args.ID) 97 98 if !tt.wantErr { 99 if got.Timestamp.IsZero() || got.Timestamp.After(time.Now()) { 100 t.Errorf("GetBatchInfo() wrong Timestamp %v", got.Timestamp) 101 } 102 } 103 104 if (err != nil) != tt.wantErr { 105 t.Errorf("GetBatchInfo() error = %v, wantErr %v", err, tt.wantErr) 106 return 107 } 108 if !reflect.DeepEqual(got, tt.want) { 109 t.Errorf("GetBatchInfo() = %v, want %v", got, tt.want) 110 } 111 }) 112 } 113 } 114 115 func TestGetBatchHistory(t *testing.T) { 116 const databaseName = "TestGetBatchHistory" 117 t.Parallel() 118 119 db := tests.Setup(databaseName, WithdrawModel()) 120 defer tests.Teardown(db, databaseName) 121 122 ref, _ := AddBatch(db, model.BatchNetworkBitcoin, "{}") 123 124 ref1, _ := AddBatchInfo(db, ref.ID, model.BatchStatusCreated, model.BatchInfoCrypto, "{}") 125 ref2, _ := AddBatchInfo(db, ref.ID, model.BatchStatusProcessing, model.BatchInfoCrypto, "{}") 126 ref3, _ := AddBatchInfo(db, ref.ID, model.BatchStatusCanceled, model.BatchInfoCrypto, "{}") 127 ref4, _ := AddBatchInfo(db, ref.ID, model.BatchStatusSettled, model.BatchInfoCrypto, "{}") 128 129 type args struct { 130 batchID model.BatchID 131 } 132 tests := []struct { 133 name string 134 args args 135 want []model.BatchInfo 136 wantErr bool 137 }{ 138 {"default", args{}, nil, true}, 139 {"ref", args{ref.ID}, createBatchInfoList(ref1, ref2, ref3, ref4), false}, 140 } 141 for _, tt := range tests { 142 tt := tt // capture range variable 143 t.Run(tt.name, func(t *testing.T) { 144 got, err := GetBatchHistory(db, tt.args.batchID) 145 if (err != nil) != tt.wantErr { 146 t.Errorf("GetBatchHistory() error = %v, wantErr %v", err, tt.wantErr) 147 return 148 } 149 if !reflect.DeepEqual(got, tt.want) { 150 t.Errorf("GetBatchHistory() = %v, want %v", got, tt.want) 151 } 152 }) 153 } 154 } 155 156 func TestGetBatchInfoByStatusAndType(t *testing.T) { 157 const databaseName = "TestGetBatchInfoByStatusAndType" 158 t.Parallel() 159 160 db := tests.Setup(databaseName, WithdrawModel()) 161 defer tests.Teardown(db, databaseName) 162 163 ref, _ := AddBatch(db, model.BatchNetworkBitcoin, "{}") 164 165 ref1, _ := AddBatchInfo(db, ref.ID, model.BatchStatusCreated, model.BatchInfoCrypto, "{}") 166 ref2, _ := AddBatchInfo(db, ref.ID, model.BatchStatusProcessing, model.BatchInfoCrypto, "{}") 167 ref3, _ := AddBatchInfo(db, ref.ID, model.BatchStatusCanceled, model.BatchInfoCrypto, "{}") 168 ref4, _ := AddBatchInfo(db, ref.ID, model.BatchStatusSettled, model.BatchInfoCrypto, "{}") 169 170 type args struct { 171 status model.BatchStatus 172 dataType encoding.DataType 173 } 174 tests := []struct { 175 name string 176 args args 177 want []model.BatchInfo 178 wantErr bool 179 }{ 180 {"default", args{}, nil, true}, 181 182 {"invalid_status", args{"", model.BatchInfoCrypto}, nil, true}, 183 {"invalid_datatype", args{model.BatchStatusCreated, ""}, nil, true}, 184 185 {"created", args{"other", model.BatchInfoCrypto}, nil, false}, 186 187 {"created", args{model.BatchStatusCreated, model.BatchInfoCrypto}, createBatchInfoList(ref1), false}, 188 {"processing", args{model.BatchStatusProcessing, model.BatchInfoCrypto}, createBatchInfoList(ref2), false}, 189 {"canceled", args{model.BatchStatusCanceled, model.BatchInfoCrypto}, createBatchInfoList(ref3), false}, 190 {"settled", args{model.BatchStatusSettled, model.BatchInfoCrypto}, createBatchInfoList(ref4), false}, 191 } 192 for _, tt := range tests { 193 tt := tt // capture range variable 194 t.Run(tt.name, func(t *testing.T) { 195 got, err := GetBatchInfoByStatusAndType(db, tt.args.status, tt.args.dataType) 196 if (err != nil) != tt.wantErr { 197 t.Errorf("GetBatchInfoByStatusAndType() error = %v, wantErr %v", err, tt.wantErr) 198 return 199 } 200 if !reflect.DeepEqual(got, tt.want) { 201 t.Errorf("GetBatchInfoByStatusAndType() = %v, want %v", got, tt.want) 202 } 203 }) 204 } 205 } 206 207 func TestGetLastBatchInfo(t *testing.T) { 208 const databaseName = "TestGetLastBatchInfo" 209 t.Parallel() 210 211 db := tests.Setup(databaseName, WithdrawModel()) 212 defer tests.Teardown(db, databaseName) 213 214 b1, _ := AddBatch(db, model.BatchNetworkBitcoin, "{}") 215 b2, _ := AddBatch(db, model.BatchNetworkBitcoin, "{}") 216 217 data, _ := encoding.EncodeData(&model.BatchInfoCryptoData{ 218 TxID: "", 219 }) 220 221 ref1, _ := AddBatchInfo(db, b1.ID, model.BatchStatusCreated, model.BatchInfoCrypto, model.BatchInfoData(data)) 222 _, _ = AddBatchInfo(db, b2.ID, model.BatchStatusCreated, model.BatchInfoCrypto, model.BatchInfoData(data)) 223 ref2, _ := AddBatchInfo(db, b2.ID, model.BatchStatusProcessing, model.BatchInfoCrypto, model.BatchInfoData(data)) 224 225 type args struct { 226 batchID model.BatchID 227 } 228 tests := []struct { 229 name string 230 args args 231 want model.BatchInfo 232 wantErr bool 233 }{ 234 {"default", args{}, model.BatchInfo{}, true}, 235 {"absent", args{42}, model.BatchInfo{}, true}, 236 237 {"created", args{ref1.BatchID}, ref1, false}, 238 {"processing", args{ref2.BatchID}, ref2, false}, 239 } 240 for _, tt := range tests { 241 tt := tt // capture range variable 242 t.Run(tt.name, func(t *testing.T) { 243 got, err := GetLastBatchInfo(db, tt.args.batchID) 244 if (err != nil) != tt.wantErr { 245 t.Errorf("GetLastBatchInfo() error = %v, wantErr %v", err, tt.wantErr) 246 return 247 } 248 if !reflect.DeepEqual(got, tt.want) { 249 t.Errorf("GetLastBatchInfo() = %v, want %v", got, tt.want) 250 } 251 }) 252 } 253 } 254 255 func TestGetLastBatchInfoByStatusAndNetwork(t *testing.T) { 256 const databaseName = "TestGetLastBatchInfoByStatusAndNetwork" 257 t.Parallel() 258 259 db := tests.Setup(databaseName, WithdrawModel()) 260 defer tests.Teardown(db, databaseName) 261 262 b1, _ := AddBatch(db, model.BatchNetworkBitcoin, "{}") 263 b2, _ := AddBatch(db, model.BatchNetworkBitcoin, "{}") 264 265 data, _ := encoding.EncodeData(&model.BatchInfoCryptoData{ 266 TxID: "", 267 }) 268 269 ref1, _ := AddBatchInfo(db, b1.ID, model.BatchStatusCreated, model.BatchInfoCrypto, model.BatchInfoData(data)) 270 _, _ = AddBatchInfo(db, b2.ID, model.BatchStatusCreated, model.BatchInfoCrypto, model.BatchInfoData(data)) 271 ref2, _ := AddBatchInfo(db, b2.ID, model.BatchStatusProcessing, model.BatchInfoCrypto, model.BatchInfoData(data)) 272 273 type args struct { 274 status model.BatchStatus 275 network model.BatchNetwork 276 dataType encoding.DataType 277 } 278 tests := []struct { 279 name string 280 args args 281 want []model.BatchInfo 282 wantErr bool 283 }{ 284 {"default", args{}, nil, true}, 285 286 {"absent", args{model.BatchStatusCreated, model.BatchNetworkSepa, "absent"}, nil, false}, 287 {"created", args{model.BatchStatusCreated, model.BatchNetworkBitcoin, model.BatchInfoCrypto}, createBatchInfoList(ref1), false}, 288 {"processing", args{model.BatchStatusProcessing, model.BatchNetworkBitcoin, model.BatchInfoCrypto}, createBatchInfoList(ref2), false}, 289 } 290 for _, tt := range tests { 291 tt := tt // capture range variable 292 t.Run(tt.name, func(t *testing.T) { 293 got, err := GetLastBatchInfoByStatusAndNetwork(db, tt.args.status, tt.args.network) 294 if (err != nil) != tt.wantErr { 295 t.Errorf("GetLastBatchInfoByStatusAndNetwork() error = %v, wantErr %v", err, tt.wantErr) 296 return 297 } 298 if !reflect.DeepEqual(got, tt.want) { 299 t.Errorf("GetLastBatchInfoByStatusAndNetwork() = %v, want %v", got, tt.want) 300 } 301 }) 302 } 303 } 304 305 func createBatchInfoList(list ...model.BatchInfo) []model.BatchInfo { 306 return append([]model.BatchInfo{}, list...) 307 } 308 309 func createBatchInfo(batchID model.BatchID, status model.BatchStatus, dataType encoding.DataType, data model.BatchInfoData) model.BatchInfo { 310 return model.BatchInfo{ 311 BatchID: batchID, 312 Status: status, 313 Type: dataType, 314 Data: data, 315 } 316 }