github.com/condensat/bank-core@v0.1.0/database/query/fee_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 TestAddFee(t *testing.T) {
    16  	const databaseName = "TestAddFee"
    17  	t.Parallel()
    18  
    19  	db := tests.Setup(databaseName, WithdrawModel())
    20  	defer tests.Teardown(db, databaseName)
    21  
    22  	data := createTestAccountStateData(db)
    23  	a1 := data.Accounts[0]
    24  	a2 := data.Accounts[2]
    25  
    26  	ref1, _ := AddWithdraw(db, a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}")
    27  	ref2, _ := AddWithdraw(db, a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}")
    28  
    29  	type args struct {
    30  		withdrawID model.WithdrawID
    31  		amount     model.Float
    32  		data       model.FeeData
    33  	}
    34  	tests := []struct {
    35  		name    string
    36  		args    args
    37  		want    model.Fee
    38  		wantErr bool
    39  	}{
    40  		{"default", args{}, model.Fee{}, true},
    41  		{"invalid_withdraw", args{0, 0.1, ""}, model.Fee{}, true},
    42  		{"invalid_fee", args{ref1.ID, 0.0, ""}, model.Fee{}, true},
    43  		{"negative_fee", args{ref1.ID, -0.1, ""}, model.Fee{}, true},
    44  
    45  		{"valid_data", args{ref1.ID, 0.1, ""}, createFee(ref1.ID, 0.1, "{}"), false},
    46  		{"valid", args{ref2.ID, 0.1, "{}"}, createFee(ref2.ID, 0.1, "{}"), false},
    47  	}
    48  	for _, tt := range tests {
    49  		tt := tt // capture range variable
    50  		t.Run(tt.name, func(t *testing.T) {
    51  			got, err := AddFee(db, tt.args.withdrawID, tt.args.amount, tt.args.data)
    52  			if (err != nil) != tt.wantErr {
    53  				t.Errorf("AddFee() error = %v, wantErr %v", err, tt.wantErr)
    54  				return
    55  			}
    56  
    57  			tt.want.ID = got.ID
    58  			if !reflect.DeepEqual(got, tt.want) {
    59  				t.Errorf("AddFee() = %v, want %v", got, tt.want)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestGetFee(t *testing.T) {
    66  	const databaseName = "TestGetFee"
    67  	t.Parallel()
    68  
    69  	db := tests.Setup(databaseName, WithdrawModel())
    70  	defer tests.Teardown(db, databaseName)
    71  
    72  	data := createTestAccountStateData(db)
    73  	a1 := data.Accounts[0]
    74  	a2 := data.Accounts[2]
    75  
    76  	withdraw, _ := AddWithdraw(db, a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}")
    77  
    78  	ref, _ := AddFee(db, withdraw.ID, 0.1, "{}")
    79  
    80  	type args struct {
    81  		ID model.FeeID
    82  	}
    83  	tests := []struct {
    84  		name    string
    85  		args    args
    86  		want    model.Fee
    87  		wantErr bool
    88  	}{
    89  		{"default", args{}, model.Fee{}, true},
    90  		{"ref", args{ref.ID}, ref, 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 := GetFee(db, tt.args.ID)
    96  			if (err != nil) != tt.wantErr {
    97  				t.Errorf("GetFee() error = %v, wantErr %v", err, tt.wantErr)
    98  				return
    99  			}
   100  			if !reflect.DeepEqual(got, tt.want) {
   101  				t.Errorf("GetFee() = %v, want %v", got, tt.want)
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  func TestGetFeeByWithdrawID(t *testing.T) {
   108  	const databaseName = "TestGetFeeByWithdrawID"
   109  	t.Parallel()
   110  
   111  	db := tests.Setup(databaseName, WithdrawModel())
   112  	defer tests.Teardown(db, databaseName)
   113  
   114  	data := createTestAccountStateData(db)
   115  	a1 := data.Accounts[0]
   116  	a2 := data.Accounts[2]
   117  
   118  	withdraw, _ := AddWithdraw(db, a1.ID, a2.ID, 0.1, model.BatchModeNormal, "{}")
   119  
   120  	ref, _ := AddFee(db, withdraw.ID, 0.1, "{}")
   121  
   122  	type args struct {
   123  		withdrawID model.WithdrawID
   124  	}
   125  	tests := []struct {
   126  		name    string
   127  		args    args
   128  		want    model.Fee
   129  		wantErr bool
   130  	}{
   131  		{"default", args{}, model.Fee{}, true},
   132  		{"ref", args{withdraw.ID}, ref, false},
   133  	}
   134  	for _, tt := range tests {
   135  		tt := tt // capture range variable
   136  		t.Run(tt.name, func(t *testing.T) {
   137  			got, err := GetFeeByWithdrawID(db, tt.args.withdrawID)
   138  			if (err != nil) != tt.wantErr {
   139  				t.Errorf("GetFeeByWithdrawID() error = %v, wantErr %v", err, tt.wantErr)
   140  				return
   141  			}
   142  			if !reflect.DeepEqual(got, tt.want) {
   143  				t.Errorf("GetFeeByWithdrawID() = %v, want %v", got, tt.want)
   144  			}
   145  		})
   146  	}
   147  }
   148  
   149  func createFee(withdrawID model.WithdrawID, amount model.Float, data model.FeeData) model.Fee {
   150  	return model.Fee{
   151  		WithdrawID: withdrawID,
   152  		Amount:     &amount,
   153  		Data:       data,
   154  	}
   155  }