github.com/condensat/bank-core@v0.1.0/database/query/account_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_accountColumnNames(t *testing.T) { 18 t.Parallel() 19 20 fields := tests.GetSortedTypeFileds(reflect.TypeOf(model.Account{})) 21 names := accountColumnNames() 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 TestCreateAccount(t *testing.T) { 30 const databaseName = "TestCreateAccount" 31 t.Parallel() 32 33 db := tests.Setup(databaseName, AccountOperationModel()) 34 defer tests.Teardown(db, databaseName) 35 36 data := createTestAccountData(db) 37 38 type args struct { 39 account model.Account 40 } 41 tests := []struct { 42 name string 43 args args 44 validID bool 45 wantErr bool 46 }{ 47 {"Default", args{model.Account{}}, false, true}, 48 {"Valid", args{model.Account{UserID: data.Users[0].ID, CurrencyName: data.Currencies[0].Name, Name: data.Names[0]}}, true, false}, 49 {"Duplicate", args{model.Account{UserID: data.Users[0].ID, CurrencyName: data.Currencies[0].Name, Name: data.Names[0]}}, false, true}, 50 51 {"SameUser", args{model.Account{UserID: data.Users[0].ID, CurrencyName: data.Currencies[0].Name, Name: data.Names[1]}}, true, false}, 52 {"SecondCurr", args{model.Account{UserID: data.Users[0].ID, CurrencyName: data.Currencies[1].Name, Name: data.Names[0]}}, true, false}, 53 54 {"SecondUser", args{model.Account{UserID: data.Users[1].ID, CurrencyName: data.Currencies[0].Name, Name: data.Names[0]}}, true, false}, 55 {"SecondUserSecondCurr", args{model.Account{UserID: data.Users[1].ID, CurrencyName: data.Currencies[1].Name, Name: data.Names[0]}}, true, false}, 56 } 57 for _, tt := range tests { 58 tt := tt // capture range variable 59 t.Run(tt.name, func(t *testing.T) { 60 got, err := CreateAccount(db, tt.args.account) 61 if (err != nil) != tt.wantErr { 62 t.Errorf("CreateAccount() error = %v, wantErr %v", err, tt.wantErr) 63 return 64 } 65 if (got.ID != 0) != tt.validID { 66 t.Errorf("CreateAccount() = %v, unexpected ID", got.ID) 67 } 68 if !tt.wantErr && len(tt.args.account.Name) == 0 && got.Name != AccountNameDefault { 69 t.Errorf("CreateAccount() = %v, unexpected default account name", got.ID) 70 } 71 }) 72 } 73 } 74 75 func TestAccountsExists(t *testing.T) { 76 const databaseName = "TestAccountsExists" 77 t.Parallel() 78 79 db := tests.Setup(databaseName, AccountOperationModel()) 80 defer tests.Teardown(db, databaseName) 81 82 data := createTestAccountData(db) 83 84 refAccount := model.Account{UserID: data.Users[0].ID, CurrencyName: data.Currencies[0].Name, Name: data.Names[0]} 85 86 _, _ = CreateAccount(db, refAccount) 87 88 type args struct { 89 userID model.UserID 90 currency model.CurrencyName 91 name model.AccountName 92 } 93 tests := []struct { 94 name string 95 args args 96 want bool 97 }{ 98 {"Default", args{0, "", ""}, false}, 99 {"Valid", args{refAccount.UserID, refAccount.CurrencyName, refAccount.Name}, true}, 100 101 {"InvalidUserID", args{0, refAccount.CurrencyName, refAccount.Name}, false}, 102 {"InvalidCurrency", args{refAccount.UserID, "", refAccount.Name}, false}, 103 {"InvalidName", args{refAccount.UserID, refAccount.CurrencyName, "not-default"}, false}, 104 105 {"InvalidUserIDCurrency", args{0, "", refAccount.Name}, false}, 106 {"InvalidCurrencyName", args{refAccount.UserID, "", "not-default"}, false}, 107 {"InvalidUserIDName", args{0, refAccount.CurrencyName, "not-default"}, false}, 108 109 {"ValidWildcard", args{refAccount.UserID, refAccount.CurrencyName, AccountNameWildcard}, true}, 110 } 111 for _, tt := range tests { 112 tt := tt // capture range variable 113 t.Run(tt.name, func(t *testing.T) { 114 if got := AccountsExists(db, tt.args.userID, tt.args.currency, tt.args.name); got != tt.want { 115 t.Errorf("AccountsExists() = %v, want %v", got, tt.want) 116 } 117 }) 118 } 119 } 120 121 func TestQueryAccountList(t *testing.T) { 122 const databaseName = "TestQueryAccountList" 123 t.Parallel() 124 125 db := tests.Setup(databaseName, AccountOperationModel()) 126 defer tests.Teardown(db, databaseName) 127 128 data := createTestAccountData(db) 129 130 refAccount := model.Account{UserID: data.Users[0].ID, CurrencyName: data.Currencies[0].Name, Name: data.Names[1]} 131 _, _ = CreateAccount(db, refAccount) 132 133 refAccount = model.Account{UserID: data.Users[0].ID, CurrencyName: data.Currencies[0].Name, Name: data.Names[0]} 134 _, _ = CreateAccount(db, refAccount) 135 136 type args struct { 137 userID model.UserID 138 currency model.CurrencyName 139 name model.AccountName 140 } 141 tests := []struct { 142 name string 143 args args 144 count int 145 wantErr bool 146 }{ 147 {"Default", args{0, "", ""}, 0, true}, 148 {"Valid", args{refAccount.UserID, refAccount.CurrencyName, refAccount.Name}, 1, false}, 149 150 {"InvalidUserID", args{0, refAccount.CurrencyName, refAccount.Name}, 0, true}, 151 {"InvalidCurrency", args{refAccount.UserID, "", refAccount.Name}, 0, false}, 152 {"InvalidName", args{refAccount.UserID, refAccount.CurrencyName, "not-default"}, 0, false}, 153 154 {"ValidWildcard", args{refAccount.UserID, refAccount.CurrencyName, AccountNameWildcard}, 2, false}, 155 } 156 for _, tt := range tests { 157 tt := tt // capture range variable 158 t.Run(tt.name, func(t *testing.T) { 159 got, err := QueryAccountList(db, tt.args.userID, tt.args.currency, tt.args.name) 160 if (err != nil) != tt.wantErr { 161 t.Errorf("QueryAccountList() error = %v, wantErr %v", err, tt.wantErr) 162 return 163 } 164 if len(got) != tt.count { 165 t.Errorf("QueryAccountList() = %v, want %v", len(got), tt.count) 166 } 167 }) 168 } 169 } 170 171 type AccountTestData struct { 172 Users []model.User 173 Currencies []model.Currency 174 Names []model.AccountName 175 } 176 177 func createTestAccountData(db database.Context) AccountTestData { 178 var data AccountTestData 179 180 userTest1, _ := FindOrCreateUser(db, model.User{Name: "test1", Email: "test1@condensat.tech"}) 181 userTest2, _ := FindOrCreateUser(db, model.User{Name: "test2", Email: "test2@condensat.tech"}) 182 currTest1, _ := AddOrUpdateCurrency(db, model.NewCurrency("TBTC1", "", 1, FlagCurencyAvailable, 1, 2)) 183 currTest2, _ := AddOrUpdateCurrency(db, model.NewCurrency("TBTC2", "", 1, FlagCurencyAvailable, 1, 2)) 184 185 data.Users = append(data.Users, userTest1) 186 data.Users = append(data.Users, userTest2) 187 data.Currencies = append(data.Currencies, currTest1) 188 data.Currencies = append(data.Currencies, currTest2) 189 data.Names = append(data.Names, "") // empty account name is "default" 190 data.Names = append(data.Names, "Vault") 191 192 return data 193 }