github.com/condensat/bank-core@v0.1.0/database/query/accountstate_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"
    12  	"github.com/condensat/bank-core/database/model"
    13  	"github.com/condensat/bank-core/database/query/tests"
    14  )
    15  
    16  func TestAddOrUpdateAccountState(t *testing.T) {
    17  	const databaseName = "TestAddOrUpdateAccountState"
    18  	t.Parallel()
    19  
    20  	db := tests.Setup(databaseName, AccountOperationModel())
    21  	defer tests.Teardown(db, databaseName)
    22  
    23  	data := createTestAccountStateData(db)
    24  
    25  	accountToUpdate, err := AddOrUpdateAccountState(db, createAccountState(data.Accounts[1].ID, model.AccountStatusDisabled))
    26  	if err != nil {
    27  		t.Errorf("AddOrUpdateAccountState() error = %v", err)
    28  	}
    29  
    30  	type args struct {
    31  		accountSate model.AccountState
    32  	}
    33  	tests := []struct {
    34  		name    string
    35  		args    args
    36  		want    model.AccountState
    37  		wantErr bool
    38  	}{
    39  		// model.AccountState{AccountID: data.Accounts[0].ID, State: model.AccountStatusNormal}
    40  		{"Default", args{model.AccountState{}}, model.AccountState{}, true},
    41  		{"InvlalidAccountID", args{createAccountState(0, model.AccountStatusInvalid)}, model.AccountState{}, true},
    42  		{"InvlalidStatus", args{createAccountState(data.Accounts[0].ID, model.AccountStatusInvalid)}, model.AccountState{}, true},
    43  
    44  		{"Created", args{createAccountState(data.Accounts[0].ID, model.AccountStatusCreated)}, createAccountState(data.Accounts[0].ID, model.AccountStatusCreated), false},
    45  		{"Normal", args{createAccountState(data.Accounts[0].ID, model.AccountStatusNormal)}, createAccountState(data.Accounts[0].ID, model.AccountStatusNormal), false},
    46  		{"Locked", args{createAccountState(data.Accounts[0].ID, model.AccountStatusLocked)}, createAccountState(data.Accounts[0].ID, model.AccountStatusLocked), false},
    47  		{"Disabled", args{createAccountState(data.Accounts[0].ID, model.AccountStatusDisabled)}, createAccountState(data.Accounts[0].ID, model.AccountStatusDisabled), false},
    48  
    49  		{"Update", args{updateAccountState(accountToUpdate, model.AccountStatusNormal)}, createAccountState(accountToUpdate.AccountID, model.AccountStatusNormal), false},
    50  		{"InvalidUpdate", args{updateAccountState(accountToUpdate, model.AccountStatusInvalid)}, model.AccountState{}, true},
    51  	}
    52  	for _, tt := range tests {
    53  		tt := tt // capture range variable
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			got, err := AddOrUpdateAccountState(db, tt.args.accountSate)
    56  			if (err != nil) != tt.wantErr {
    57  				t.Errorf("AddOrUpdateAccountState() error = %v, wantErr %v", err, tt.wantErr)
    58  				return
    59  			}
    60  			if !reflect.DeepEqual(got, tt.want) {
    61  				t.Errorf("AddOrUpdateAccountState() = %v, want %v", got, tt.want)
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func TestGetAccountStatusByAccountID(t *testing.T) {
    68  	const databaseName = "TestGetAccountStatusByAccountID"
    69  	t.Parallel()
    70  
    71  	db := tests.Setup(databaseName, AccountOperationModel())
    72  	defer tests.Teardown(db, databaseName)
    73  
    74  	data := createTestAccountStateData(db)
    75  
    76  	refAccountState := model.AccountState{AccountID: data.Accounts[0].ID, State: model.AccountStatusNormal}
    77  
    78  	_, _ = AddOrUpdateAccountState(db, refAccountState)
    79  
    80  	type args struct {
    81  		accountID model.AccountID
    82  	}
    83  	tests := []struct {
    84  		name    string
    85  		args    args
    86  		want    model.AccountState
    87  		wantErr bool
    88  	}{
    89  		{"Default", args{}, model.AccountState{}, true},
    90  		{"Valid", args{refAccountState.AccountID}, refAccountState, false},
    91  	}
    92  	for _, tt := range tests {
    93  		tt := tt // capture range variable
    94  		t.Run(tt.name, func(t *testing.T) {
    95  			got, err := GetAccountStatusByAccountID(db, tt.args.accountID)
    96  			if (err != nil) != tt.wantErr {
    97  				t.Errorf("GetAccountStatusByID() error = %v, wantErr %v", err, tt.wantErr)
    98  				return
    99  			}
   100  			if !reflect.DeepEqual(got, tt.want) {
   101  				t.Errorf("GetAccountStatusByID() = %v, want %v", got, tt.want)
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  type AccountStateTestData struct {
   108  	Users      []model.User
   109  	Currencies []model.Currency
   110  	Accounts   []model.Account
   111  }
   112  
   113  func createAccountState(accountID model.AccountID, state model.AccountStatus) model.AccountState {
   114  	return model.AccountState{
   115  		AccountID: accountID,
   116  		State:     state,
   117  	}
   118  }
   119  
   120  func updateAccountState(accountState model.AccountState, newState model.AccountStatus) model.AccountState {
   121  	return model.AccountState{
   122  		AccountID: accountState.AccountID,
   123  		State:     newState,
   124  	}
   125  }
   126  
   127  func createTestAccountStateData(db database.Context) AccountStateTestData {
   128  	var data AccountStateTestData
   129  
   130  	userTest1, _ := FindOrCreateUser(db, model.User{Name: "test1", Email: "test1@condensat.tech"})
   131  	userTest2, _ := FindOrCreateUser(db, model.User{Name: "test2", Email: "test2@condensat.tech"})
   132  	currTest1, _ := AddOrUpdateCurrency(db, model.NewCurrency("TBTC1", "", 1, FlagCurencyAvailable, 1, 2))
   133  	currTest2, _ := AddOrUpdateCurrency(db, model.NewCurrency("TBTC2", "", 1, FlagCurencyAvailable, 1, 2))
   134  	accountTest1, _ := CreateAccount(db, model.Account{UserID: userTest1.ID, CurrencyName: currTest1.Name, Name: "accountTest1"})
   135  	accountTest2, _ := CreateAccount(db, model.Account{UserID: userTest1.ID, CurrencyName: currTest2.Name, Name: "accountTest2"})
   136  	accountTest3, _ := CreateAccount(db, model.Account{UserID: userTest2.ID, CurrencyName: currTest1.Name, Name: "accountTest3"})
   137  	accountTest4, _ := CreateAccount(db, model.Account{UserID: userTest2.ID, CurrencyName: currTest2.Name, Name: "accountTest4"})
   138  
   139  	data.Users = append(data.Users, userTest1)
   140  	data.Users = append(data.Users, userTest2)
   141  	data.Currencies = append(data.Currencies, currTest1)
   142  	data.Currencies = append(data.Currencies, currTest2)
   143  	data.Accounts = append(data.Accounts, accountTest1)
   144  	data.Accounts = append(data.Accounts, accountTest2)
   145  	data.Accounts = append(data.Accounts, accountTest3)
   146  	data.Accounts = append(data.Accounts, accountTest4)
   147  
   148  	return data
   149  }