github.com/condensat/bank-core@v0.1.0/database/query/operationinfo_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 11 "github.com/condensat/bank-core/database/model" 12 "github.com/condensat/bank-core/database/query/tests" 13 ) 14 15 func TestAddOperationInfo(t *testing.T) { 16 const databaseName = "TestAddOperationInfo" 17 t.Parallel() 18 19 db := tests.Setup(databaseName, OperationInfoModel()) 20 defer tests.Teardown(db, databaseName) 21 22 const data = "{}" 23 24 type args struct { 25 operation model.OperationInfo 26 } 27 tests := []struct { 28 name string 29 args args 30 want model.OperationInfo 31 wantErr bool 32 }{ 33 {"default", args{}, model.OperationInfo{}, true}, 34 {"invalidUpdate", args{model.OperationInfo{ID: 1, CryptoAddressID: 42, TxID: ":txid"}}, model.OperationInfo{}, true}, 35 36 {"valid", args{model.OperationInfo{CryptoAddressID: 42, TxID: ":txid1"}}, model.OperationInfo{CryptoAddressID: 42, TxID: ":txid1"}, false}, 37 {"validWithAmount", args{model.OperationInfo{CryptoAddressID: 42, TxID: ":txid2", Amount: 0.1337, Data: data}}, model.OperationInfo{CryptoAddressID: 42, TxID: ":txid2", Amount: 0.1337, Data: data}, false}, 38 {"validWithData", args{model.OperationInfo{CryptoAddressID: 42, TxID: ":txid3", Data: data}}, model.OperationInfo{CryptoAddressID: 42, TxID: ":txid3", Data: data}, false}, 39 } 40 for _, tt := range tests { 41 tt := tt // capture range variable 42 t.Run(tt.name, func(t *testing.T) { 43 got, err := AddOperationInfo(db, tt.args.operation) 44 if (err != nil) != tt.wantErr { 45 t.Errorf("AddOperationInfo() error = %v, wantErr %v", err, tt.wantErr) 46 return 47 } 48 // copy new db fields 49 tt.want.ID = got.ID 50 tt.want.Timestamp = got.Timestamp 51 if !reflect.DeepEqual(got, tt.want) { 52 t.Errorf("AddOperationInfo() = %+v, want %+v", got, tt.want) 53 } 54 }) 55 } 56 } 57 58 func TestGetOperationInfo(t *testing.T) { 59 const databaseName = "TestGetOperationInfo" 60 t.Parallel() 61 62 db := tests.Setup(databaseName, OperationInfoModel()) 63 defer tests.Teardown(db, databaseName) 64 65 const cryptoAddressID = model.CryptoAddressID(42) 66 ref1, _ := AddOperationInfo(db, model.OperationInfo{CryptoAddressID: cryptoAddressID, TxID: ":txid1"}) 67 ref2, _ := AddOperationInfo(db, model.OperationInfo{CryptoAddressID: cryptoAddressID, TxID: ":txid2", Amount: 0.1337}) 68 69 type args struct { 70 operationID model.OperationInfoID 71 } 72 tests := []struct { 73 name string 74 args args 75 want model.OperationInfo 76 wantErr bool 77 }{ 78 {"default", args{}, model.OperationInfo{}, true}, 79 {"notExists", args{1337}, model.OperationInfo{}, true}, 80 81 {"valid", args{ref1.ID}, ref1, false}, 82 {"validWithAmount", args{ref2.ID}, ref2, false}, 83 } 84 for _, tt := range tests { 85 tt := tt // capture range variable 86 t.Run(tt.name, func(t *testing.T) { 87 got, err := GetOperationInfo(db, tt.args.operationID) 88 if (err != nil) != tt.wantErr { 89 t.Errorf("GetOperationInfo() error = %v, wantErr %v", err, tt.wantErr) 90 return 91 } 92 if !reflect.DeepEqual(got, tt.want) { 93 t.Errorf("GetOperationInfo() = %v, want %v", got, tt.want) 94 } 95 }) 96 } 97 } 98 99 func TestGetOperationInfoByTxId(t *testing.T) { 100 const databaseName = "TestGetOperationInfoByTxId" 101 t.Parallel() 102 103 db := tests.Setup(databaseName, OperationInfoModel()) 104 defer tests.Teardown(db, databaseName) 105 106 const cryptoAddressID = model.CryptoAddressID(42) 107 ref1, _ := AddOperationInfo(db, model.OperationInfo{CryptoAddressID: cryptoAddressID, TxID: ":txid1"}) 108 109 type args struct { 110 txID model.TxID 111 } 112 tests := []struct { 113 name string 114 args args 115 want model.OperationInfo 116 wantErr bool 117 }{ 118 {"default", args{}, model.OperationInfo{}, true}, 119 {"invalidTxTd", args{":wrnongTx"}, model.OperationInfo{}, true}, 120 121 {"valid", args{ref1.TxID}, ref1, false}, 122 } 123 124 for _, tt := range tests { 125 tt := tt // capture range variable 126 t.Run(tt.name, func(t *testing.T) { 127 got, err := GetOperationInfoByTxId(db, tt.args.txID) 128 if (err != nil) != tt.wantErr { 129 t.Errorf("GetOperationInfoByTxId() error = %v, wantErr %v", err, tt.wantErr) 130 return 131 } 132 if !reflect.DeepEqual(got, tt.want) { 133 t.Errorf("GetOperationInfoByTxId() = %v, want %v", got, tt.want) 134 } 135 }) 136 } 137 } 138 139 func TestGetOperationInfoByCryptoAddress(t *testing.T) { 140 const databaseName = "TestGetOperationInfoByCryptoAddress" 141 t.Parallel() 142 143 db := tests.Setup(databaseName, OperationInfoModel()) 144 defer tests.Teardown(db, databaseName) 145 146 const cryptoAddressID = model.CryptoAddressID(42) 147 ref1, _ := AddOperationInfo(db, model.OperationInfo{CryptoAddressID: cryptoAddressID, TxID: ":txid1"}) 148 ref2, _ := AddOperationInfo(db, model.OperationInfo{CryptoAddressID: cryptoAddressID, TxID: ":txid2"}) 149 ref3, _ := AddOperationInfo(db, model.OperationInfo{CryptoAddressID: cryptoAddressID, TxID: ":txid3"}) 150 151 type args struct { 152 cryptoAddressID model.CryptoAddressID 153 } 154 tests := []struct { 155 name string 156 args args 157 want []model.OperationInfo 158 wantErr bool 159 }{ 160 {"default", args{}, nil, true}, 161 {"notExists", args{1337}, nil, false}, 162 163 {"valid", args{cryptoAddressID}, createOperationInfoList(ref1, ref2, ref3), false}, 164 } 165 for _, tt := range tests { 166 tt := tt // capture range variable 167 t.Run(tt.name, func(t *testing.T) { 168 got, err := GetOperationInfoByCryptoAddress(db, tt.args.cryptoAddressID) 169 if (err != nil) != tt.wantErr { 170 t.Errorf("GetOperationInfoByCryptoAddress() error = %v, wantErr %v", err, tt.wantErr) 171 return 172 } 173 if !reflect.DeepEqual(got, tt.want) { 174 t.Errorf("GetOperationInfoByCryptoAddress() = %v, want %v", got, tt.want) 175 } 176 }) 177 } 178 } 179 180 func createOperationInfoList(operations ...model.OperationInfo) []model.OperationInfo { 181 var result []model.OperationInfo 182 return append(result, operations...) 183 } 184 185 func TestFindCryptoAddressesByOperationInfoState(t *testing.T) { 186 const databaseName = "TestFindCryptoAddressesByOperationInfoState" 187 t.Parallel() 188 189 db := tests.Setup(databaseName, OperationInfoModel()) 190 defer tests.Teardown(db, databaseName) 191 192 type args struct { 193 chain model.String 194 state model.String 195 } 196 tests := []struct { 197 name string 198 args args 199 want []model.CryptoAddress 200 wantErr bool 201 }{ 202 {"default", args{}, nil, true}, 203 {"validEmpty", args{"chain", "state"}, nil, false}, 204 } 205 for _, tt := range tests { 206 tt := tt // capture range variable 207 t.Run(tt.name, func(t *testing.T) { 208 got, err := FindCryptoAddressesByOperationInfoState(db, tt.args.chain, tt.args.state) 209 if (err != nil) != tt.wantErr { 210 t.Errorf("FindCryptoAddressesByOperationInfoState() error = %v, wantErr %v", err, tt.wantErr) 211 return 212 } 213 if !reflect.DeepEqual(got, tt.want) { 214 t.Errorf("FindCryptoAddressesByOperationInfoState() = %v, want %v", got, tt.want) 215 } 216 }) 217 } 218 }