github.com/condensat/bank-core@v0.1.0/database/query/currency_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 "sort" 10 "testing" 11 12 "github.com/condensat/bank-core/database" 13 "github.com/condensat/bank-core/database/model" 14 "github.com/condensat/bank-core/database/query/tests" 15 ) 16 17 func Test_currencyColumnNames(t *testing.T) { 18 t.Parallel() 19 20 fields := tests.GetSortedTypeFileds(reflect.TypeOf(model.Currency{})) 21 names := currencyColumnNames() 22 sort.Strings(names) 23 24 if !reflect.DeepEqual(names, fields) { 25 t.Errorf("columnsNames() = %v, want %v", names, fields) 26 } 27 } 28 29 func TestCurrency(t *testing.T) { 30 const databaseName = "TestAddCurrency" 31 t.Parallel() 32 33 db := tests.Setup(databaseName, CurrencyModel()) 34 defer tests.Teardown(db, databaseName) 35 36 entries := createTestData() 37 38 // check if table is empty 39 if count := CountCurrencies(db); count != 0 { 40 t.Errorf("Missing CountCurrencies() = %+v, want %+v", count, 0) 41 } 42 defer checkFinalState(t, db, entries) 43 44 type args struct { 45 currency model.Currency 46 } 47 tests := []struct { 48 name string 49 args args 50 wantErr bool 51 }{ 52 {"NotAvailable", args{entries[0]}, false}, 53 {"Available", args{entries[1]}, false}, 54 {"NotCrypto", args{entries[2]}, false}, 55 {"Crypto", args{entries[3]}, false}, 56 {"Precision0", args{entries[4]}, false}, 57 {"Precision12", args{entries[5]}, false}, 58 } 59 for _, tt := range tests { 60 tt := tt // capture range variable 61 t.Run(tt.name, func(t *testing.T) { 62 63 { 64 // Create Tests 65 got, err := AddOrUpdateCurrency(db, tt.args.currency) 66 if (err != nil) != tt.wantErr { 67 t.Errorf("AddOrUpdateCurrency() error = %v, wantErr %v", err, tt.wantErr) 68 } 69 if !reflect.DeepEqual(got, tt.args.currency) { 70 t.Errorf("GetCurrency() = %+v, want %+v", got, tt.args.currency) 71 } 72 73 got, err = GetCurrencyByName(db, tt.args.currency.Name) 74 if err != nil { 75 t.Errorf("GetCurrencyByName() failed error = %v", err) 76 } 77 if !reflect.DeepEqual(got, tt.args.currency) { 78 t.Errorf("GetCurrencyByName() = %+v, want %+v", got, tt.args.currency) 79 } 80 } 81 82 // Exists Tests 83 { 84 if !CurrencyExists(db, tt.args.currency.Name) { 85 t.Errorf("CurrencyExists() = %s should exists", tt.args.currency.Name) 86 } 87 } 88 89 // Update Tests 90 { 91 updateCurr, err := GetCurrencyByName(db, tt.args.currency.Name) 92 if err != nil { 93 t.Errorf("GetCurrencyByName() failed error = %v", err) 94 } 95 // change entry 96 *updateCurr.Available = 2 97 98 got, err := AddOrUpdateCurrency(db, updateCurr) 99 if (err != nil) != tt.wantErr { 100 t.Errorf("AddOrUpdateCurrency() error = %v, wantErr %v", err, tt.wantErr) 101 } 102 if !reflect.DeepEqual(got, updateCurr) { 103 t.Errorf("AddOrUpdateCurrency() = %+v, want %+v", got, updateCurr) 104 } 105 106 got, err = GetCurrencyByName(db, updateCurr.Name) 107 if err != nil { 108 t.Errorf("GetCurrencyByName() failed error = %v", err) 109 } 110 if !reflect.DeepEqual(got, updateCurr) { 111 t.Errorf("GetCurrencyByName() = %+v, want %+v", got, updateCurr) 112 } 113 114 updateCurr, err = GetCurrencyByName(db, tt.args.currency.Name) 115 if err != nil { 116 t.Errorf("GetCurrencyByName() failed error = %v", err) 117 } 118 // restore entry 119 *updateCurr.Available = *tt.args.currency.Available 120 121 _, err = AddOrUpdateCurrency(db, updateCurr) 122 if err != nil { 123 t.Errorf("WTF") 124 } 125 got, err = GetCurrencyByName(db, updateCurr.Name) 126 if err != nil { 127 t.Errorf("GetCurrencyByName() failed error = %v", err) 128 } 129 if !reflect.DeepEqual(got, updateCurr) { 130 t.Errorf("GetCurrencyByName() = %+v, want %+v", got, updateCurr) 131 } 132 } 133 134 }) 135 } 136 } 137 138 func createTestData() []model.Currency { 139 return []model.Currency{ 140 model.NewCurrency("USD", "", 0, 0, 1, 2), 141 model.NewCurrency("BTC", "", 0, 1, 1, 2), 142 model.NewCurrency("USD2", "", 2, 0, 0, 2), 143 model.NewCurrency("BTC2", "", 2, 1, 1, 2), 144 model.NewCurrency("USD3", "", 2, 0, 1, 0), 145 model.NewCurrency("BTC3", "", 1, 1, 1, 12), 146 } 147 } 148 149 func checkFinalState(t *testing.T, db database.Context, entries []model.Currency) { 150 // check if table has entries 151 if count := CountCurrencies(db); count != len(entries) { 152 t.Errorf("Missing CountCurrencies() = %+v, want %+v", count, len(entries)) 153 } 154 155 { 156 list, err := ListAllCurrency(db) 157 if err != nil { 158 t.Errorf("ListAllCurrency() Failed = %+v", err) 159 } 160 if len(list) != len(entries) { 161 t.Errorf("Missing ListAllCurrency() = %+v, want %+v", len(list), len(entries)) 162 } 163 } 164 165 { 166 list, err := ListAvailableCurrency(db) 167 if err != nil { 168 t.Errorf("ListAvailableCurrency() Failed = %+v", err) 169 } 170 if len(list) != len(entries)/2 { 171 t.Errorf("Missing ListAvailableCurrency() = %+v, want %+v", len(list), len(entries)/2) 172 } 173 174 for _, curr := range list { 175 if !curr.IsAvailable() { 176 t.Errorf("Currency IsAvailable must be true") 177 } 178 } 179 } 180 }