github.com/condensat/bank-core@v0.1.0/database/query/withdraw_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/model"
    13  	"github.com/condensat/bank-core/database/query/tests"
    14  )
    15  
    16  func TestAddWithdraw(t *testing.T) {
    17  	const databaseName = "TestAddWithdraw"
    18  	t.Parallel()
    19  
    20  	db := tests.Setup(databaseName, WithdrawModel())
    21  	defer tests.Teardown(db, databaseName)
    22  
    23  	data := createTestAccountStateData(db)
    24  	a1 := data.Accounts[0]
    25  	a2 := data.Accounts[2]
    26  	a3 := data.Accounts[1]
    27  
    28  	type args struct {
    29  		from   model.AccountID
    30  		to     model.AccountID
    31  		amount model.Float
    32  		batch  model.BatchMode
    33  		data   model.WithdrawData
    34  	}
    35  	tests := []struct {
    36  		name    string
    37  		args    args
    38  		want    model.Withdraw
    39  		wantErr bool
    40  	}{
    41  		{"default", args{}, model.Withdraw{}, true},
    42  		{"invalid_from", args{0, a2.ID, 0.1, model.BatchModeNormal, "{}"}, model.Withdraw{}, true},
    43  		{"invalid_to", args{a1.ID, 0, 0.1, model.BatchModeNormal, "{}"}, model.Withdraw{}, true},
    44  		{"same_from_to", args{a1.ID, a1.ID, 0.1, model.BatchModeNormal, "{}"}, model.Withdraw{}, true},
    45  		{"wrong_currency", args{a1.ID, a3.ID, 0.1, model.BatchModeNormal, "{}"}, model.Withdraw{}, true},
    46  		{"invalid_amount", args{a1.ID, a2.ID, 0.0, model.BatchModeNormal, "{}"}, model.Withdraw{}, true},
    47  		{"negative_amount", args{a1.ID, a2.ID, -0.1, model.BatchModeNormal, "{}"}, model.Withdraw{}, true},
    48  		{"invalid_batch", args{a1.ID, a2.ID, 0.0, "", "{}"}, model.Withdraw{}, true},
    49  
    50  		{"default_data", args{a1.ID, a2.ID, 0.1, model.BatchModeNormal, ""}, createWithdraw(a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}"), false},
    51  		{"valid", args{a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}"}, createWithdraw(a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}"), false},
    52  	}
    53  	for _, tt := range tests {
    54  		tt := tt // capture range variable
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			got, err := AddWithdraw(db, tt.args.from, tt.args.to, tt.args.amount, tt.args.batch, tt.args.data)
    57  			if (err != nil) != tt.wantErr {
    58  				t.Errorf("AddWithdraw() error = %v, wantErr %v", err, tt.wantErr)
    59  				return
    60  			}
    61  
    62  			if !tt.wantErr {
    63  				if got.Timestamp.IsZero() || got.Timestamp.After(time.Now()) {
    64  					t.Errorf("AddWithdraw() wrong Timestamp %v", got.Timestamp)
    65  				}
    66  			}
    67  
    68  			tt.want.ID = got.ID
    69  			tt.want.Timestamp = got.Timestamp
    70  			if !reflect.DeepEqual(got, tt.want) {
    71  				t.Errorf("AddWithdraw() = %v, want %v", got, tt.want)
    72  			}
    73  		})
    74  	}
    75  }
    76  
    77  func TestGetWithdraw(t *testing.T) {
    78  	const databaseName = "TestGetWithdraw"
    79  	t.Parallel()
    80  
    81  	db := tests.Setup(databaseName, WithdrawModel())
    82  	defer tests.Teardown(db, databaseName)
    83  
    84  	data := createTestAccountStateData(db)
    85  	a1 := data.Accounts[0]
    86  	a2 := data.Accounts[2]
    87  
    88  	ref, _ := AddWithdraw(db, a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}")
    89  
    90  	type args struct {
    91  		ID model.WithdrawID
    92  	}
    93  	tests := []struct {
    94  		name    string
    95  		args    args
    96  		want    model.Withdraw
    97  		wantErr bool
    98  	}{
    99  		{"default", args{}, model.Withdraw{}, true},
   100  		{"ref", args{ref.ID}, ref, false},
   101  	}
   102  	for _, tt := range tests {
   103  		tt := tt // capture range variable
   104  		t.Run(tt.name, func(t *testing.T) {
   105  			got, err := GetWithdraw(db, tt.args.ID)
   106  
   107  			if !tt.wantErr {
   108  				if got.Timestamp.IsZero() || got.Timestamp.After(time.Now()) {
   109  					t.Errorf("GetWithdraw() wrong Timestamp %v", got.Timestamp)
   110  				}
   111  			}
   112  
   113  			if (err != nil) != tt.wantErr {
   114  				t.Errorf("GetWithdraw() error = %v, wantErr %v", err, tt.wantErr)
   115  				return
   116  			}
   117  			if !reflect.DeepEqual(got, tt.want) {
   118  				t.Errorf("GetWithdraw() = %v, want %v", got, tt.want)
   119  			}
   120  		})
   121  	}
   122  }
   123  
   124  func TestFindWithdrawByCurrencyNameAndStatus(t *testing.T) {
   125  	const databaseName = "TestFindWithdrawByCurrencyNameAndStatus"
   126  	t.Parallel()
   127  
   128  	db := tests.Setup(databaseName, WithdrawModel())
   129  	defer tests.Teardown(db, databaseName)
   130  	data := createTestAccountStateData(db)
   131  	c1 := data.Currencies[0]
   132  	a1 := data.Accounts[0]
   133  	a2 := data.Accounts[2]
   134  
   135  	withdraw, _ := AddWithdraw(db, a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}")
   136  	_, _ = AddWithdrawInfo(db, withdraw.ID, model.WithdrawStatusCreated, "{}")
   137  
   138  	type args struct {
   139  		currency model.CurrencyName
   140  		status   model.WithdrawStatus
   141  	}
   142  	tests := []struct {
   143  		name    string
   144  		args    args
   145  		want    int
   146  		wantErr bool
   147  	}{
   148  		{"default", args{}, 0, false},
   149  		{"other_currency", args{"other", model.WithdrawStatusCreated}, 0, false},
   150  		{"other_status", args{c1.Name, model.WithdrawStatusSettled}, 0, false},
   151  
   152  		{"found", args{c1.Name, model.WithdrawStatusCreated}, 1, false},
   153  	}
   154  	for _, tt := range tests {
   155  		tt := tt // capture range variable
   156  		t.Run(tt.name, func(t *testing.T) {
   157  			got, err := FindWithdrawByCurrencyNameAndStatus(db, tt.args.currency, tt.args.status)
   158  			if (err != nil) != tt.wantErr {
   159  				t.Errorf("FindWithdrawByCurrencyNameAndStatus() error = %v, wantErr %v", err, tt.wantErr)
   160  				return
   161  			}
   162  			if len(got) != tt.want {
   163  				t.Errorf("FindWithdrawByCurrencyNameAndStatus() = %v, want %v", len(got), tt.want)
   164  			}
   165  		})
   166  	}
   167  }
   168  
   169  func TestFindWithdrawByUser(t *testing.T) {
   170  	const databaseName = "TestFindWithdrawByUser"
   171  	t.Parallel()
   172  
   173  	db := tests.Setup(databaseName, WithdrawModel())
   174  	defer tests.Teardown(db, databaseName)
   175  	data := createTestAccountStateData(db)
   176  	// c1 := data.Currencies[0]
   177  	a1 := data.Accounts[0]
   178  	a2 := data.Accounts[2]
   179  
   180  	w1, _ := AddWithdraw(db, a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}")
   181  	w2, _ := AddWithdraw(db, a2.ID, a1.ID, 0.1, model.BatchModeNormal, "{}")
   182  
   183  	type args struct {
   184  		userID model.UserID
   185  	}
   186  	tests := []struct {
   187  		name    string
   188  		args    args
   189  		want    []model.Withdraw
   190  		wantErr bool
   191  	}{
   192  		{"default", args{}, nil, true},
   193  		{"first", args{a1.UserID}, []model.Withdraw{w1}, false},
   194  		{"second", args{a2.UserID}, []model.Withdraw{w2}, false},
   195  	}
   196  	for _, tt := range tests {
   197  		tt := tt // capture range variable
   198  		t.Run(tt.name, func(t *testing.T) {
   199  			got, err := FindWithdrawByUser(db, tt.args.userID)
   200  			if (err != nil) != tt.wantErr {
   201  				t.Errorf("FindWithdrawByUser() error = %v, wantErr %v", err, tt.wantErr)
   202  				return
   203  			}
   204  			if !reflect.DeepEqual(got, tt.want) {
   205  				t.Errorf("FindWithdrawByUser() = %v, want %v", got, tt.want)
   206  			}
   207  		})
   208  	}
   209  }
   210  
   211  func createWithdraw(from model.AccountID, to model.AccountID, amount model.Float, batch model.BatchMode, data model.WithdrawData) model.Withdraw {
   212  	return model.Withdraw{
   213  		From:   from,
   214  		To:     to,
   215  		Amount: &amount,
   216  		Batch:  batch,
   217  		Data:   data,
   218  	}
   219  }