github.com/condensat/bank-core@v0.1.0/database/model/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 model
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  func newInt(value Int) ZeroInt {
    13  	return &value
    14  }
    15  
    16  func TestNewCurrency(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	type args struct {
    20  		name         CurrencyName
    21  		displayName  CurrencyName
    22  		currencyType Int
    23  		available    Int
    24  		crypto       Int
    25  		precision    Int
    26  	}
    27  	tests := []struct {
    28  		name string
    29  		args args
    30  		want Currency
    31  	}{
    32  		{"Default", args{"", "", 0, 0, 0, 0}, Currency{}},
    33  		{"InvalidCurrency", args{"", "", 0, 1, 1, 2}, Currency{}},
    34  		{"DefaultAvailable", args{"BTC", "", 1, -1, 1, 2}, Currency{"BTC", "", newInt(1), newInt(0), newInt(1), newInt(2), false}},
    35  		{"DefaultCrypto", args{"BTC", "", 1, 1, -1, 2}, Currency{"BTC", "", newInt(1), newInt(1), newInt(0), newInt(2), false}},
    36  		{"DefaultPrecision", args{"BTC", "", 1, 1, 1, -2}, Currency{"BTC", "", newInt(1), newInt(1), newInt(1), newInt(2), false}},
    37  
    38  		{"Valid", args{"BTC", "", 1, 0, 0, 0}, Currency{"BTC", "", newInt(1), newInt(0), newInt(0), newInt(0), false}},
    39  		{"ValidDisplayName", args{"BTC", "Bitcoin", 1, 0, 0, 0}, Currency{"BTC", "Bitcoin", newInt(1), newInt(0), newInt(0), newInt(0), false}},
    40  		{"ValidAvailable", args{"BTC", "", 1, 1, 0, 0}, Currency{"BTC", "", newInt(1), newInt(1), newInt(0), newInt(0), false}},
    41  		{"ValidCrypto", args{"BTC", "", 1, 0, 1, 0}, Currency{"BTC", "", newInt(1), newInt(0), newInt(1), newInt(0), false}},
    42  		{"ValidPrecision", args{"BTC", "", 1, 0, 0, 1}, Currency{"BTC", "", newInt(1), newInt(0), newInt(0), newInt(1), false}},
    43  	}
    44  	for _, tt := range tests {
    45  		tt := tt // capture range variable
    46  		t.Run(tt.name, func(t *testing.T) {
    47  			if got := NewCurrency(tt.args.name, tt.args.displayName, tt.args.currencyType, tt.args.available, tt.args.crypto, tt.args.precision); !reflect.DeepEqual(got, tt.want) {
    48  				t.Errorf("NewCurrency() = %v, want %v", got, tt.want)
    49  			}
    50  		})
    51  	}
    52  }
    53  
    54  func TestCurrency_IsAvailable(t *testing.T) {
    55  	t.Parallel()
    56  
    57  	type fields struct {
    58  		Name      CurrencyName
    59  		Available ZeroInt
    60  		Crypto    ZeroInt
    61  		Precision ZeroInt
    62  	}
    63  	tests := []struct {
    64  		name   string
    65  		fields fields
    66  		want   bool
    67  	}{
    68  		{"Default", fields{}, false},
    69  		{"InvalidCurrency", fields{"", newInt(0), newInt(0), newInt(0)}, false},
    70  		{"InvalidCurrencyAvailable", fields{"", newInt(0), newInt(0), newInt(0)}, false},
    71  		{"InvalidAvailable", fields{"BTC", nil, newInt(0), newInt(0)}, false},
    72  
    73  		{"ValidAvailable", fields{"BTC", newInt(1), newInt(0), newInt(0)}, true},
    74  		{"ValidNotAvailable", fields{"BTC", newInt(0), newInt(0), newInt(0)}, false},
    75  	}
    76  	for _, tt := range tests {
    77  		tt := tt // capture range variable
    78  		t.Run(tt.name, func(t *testing.T) {
    79  			p := &Currency{
    80  				Name:      tt.fields.Name,
    81  				Available: tt.fields.Available,
    82  			}
    83  			if got := p.IsAvailable(); got != tt.want {
    84  				t.Errorf("Currency.IsAvailable() = %v, want %v", got, tt.want)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestCurrency_IsCrypto(t *testing.T) {
    91  	t.Parallel()
    92  
    93  	type fields struct {
    94  		Name      CurrencyName
    95  		Available ZeroInt
    96  		Crypto    ZeroInt
    97  		Precision ZeroInt
    98  	}
    99  	tests := []struct {
   100  		name   string
   101  		fields fields
   102  		want   bool
   103  	}{
   104  		{"Default", fields{}, false},
   105  		{"InvalidCurrency", fields{"", newInt(0), newInt(0), newInt(0)}, false},
   106  		{"InvalidCurrencyCrypto", fields{"", newInt(0), newInt(0), newInt(0)}, false},
   107  		{"InvalidCrypto", fields{"BTC", newInt(0), nil, newInt(0)}, false},
   108  
   109  		{"ValidCrypto", fields{"BTC", newInt(0), newInt(1), newInt(0)}, true},
   110  		{"ValidNotCrypto", fields{"BTC", newInt(0), newInt(0), newInt(0)}, false},
   111  	}
   112  	for _, tt := range tests {
   113  		tt := tt // capture range variable
   114  		t.Run(tt.name, func(t *testing.T) {
   115  			p := &Currency{
   116  				Name:      tt.fields.Name,
   117  				Available: tt.fields.Available,
   118  				Crypto:    tt.fields.Crypto,
   119  				Precision: tt.fields.Precision,
   120  			}
   121  			if got := p.IsCrypto(); got != tt.want {
   122  				t.Errorf("Currency.IsCrypto() = %v, want %v", got, tt.want)
   123  			}
   124  		})
   125  	}
   126  }
   127  
   128  func TestCurrency_GetType(t *testing.T) {
   129  	t.Parallel()
   130  
   131  	type fields struct {
   132  		Type ZeroInt
   133  	}
   134  	tests := []struct {
   135  		name   string
   136  		fields fields
   137  		want   Int
   138  	}{
   139  		{"default", fields{}, 0},
   140  		{"type", fields{newInt(42)}, 42},
   141  	}
   142  	for _, tt := range tests {
   143  		tt := tt // capture range variable
   144  		t.Run(tt.name, func(t *testing.T) {
   145  			p := &Currency{
   146  				Type: tt.fields.Type,
   147  			}
   148  			if got := p.GetType(); got != tt.want {
   149  				t.Errorf("Currency.GetType() = %v, want %v", got, tt.want)
   150  			}
   151  		})
   152  	}
   153  }
   154  
   155  func TestCurrency_DisplayPrecision(t *testing.T) {
   156  	type fields struct {
   157  		Name      CurrencyName
   158  		Available ZeroInt
   159  		Crypto    ZeroInt
   160  		Precision ZeroInt
   161  	}
   162  	tests := []struct {
   163  		name   string
   164  		fields fields
   165  		want   Int
   166  	}{
   167  		{"Default", fields{}, Int(0)},
   168  		{"InvalidCurrency", fields{"", newInt(0), newInt(0), newInt(0)}, Int(0)},
   169  		{"InvalidCurrencyDisplayPrecision", fields{"", newInt(0), newInt(0), newInt(0)}, Int(0)},
   170  		{"InvalidDisplayPrecision", fields{"BTC", newInt(0), newInt(0), nil}, Int(0)},
   171  
   172  		{"ValidDisplayPrecision", fields{"BTC", newInt(0), newInt(0), newInt(1)}, Int(1)},
   173  		{"ValidNotDisplayPrecision", fields{"BTC", newInt(0), newInt(0), newInt(0)}, Int(0)},
   174  	}
   175  	for _, tt := range tests {
   176  		t.Run(tt.name, func(t *testing.T) {
   177  			p := &Currency{
   178  				Name:      tt.fields.Name,
   179  				Available: tt.fields.Available,
   180  				Crypto:    tt.fields.Crypto,
   181  				Precision: tt.fields.Precision,
   182  			}
   183  			if got := p.DisplayPrecision(); got != tt.want {
   184  				t.Errorf("Currency.DisplayPrecision() = %v, want %v", got, tt.want)
   185  			}
   186  		})
   187  	}
   188  }