github.com/spi-ca/misc@v1.0.1/types/big_test.go (about)

     1  package types
     2  
     3  import (
     4  	"github.com/ericlagergren/decimal"
     5  	"testing"
     6  )
     7  
     8  func TestDecimal_Value(t *testing.T) {
     9  	t.Parallel()
    10  
    11  	tests := []string{
    12  		"3.14",
    13  		"0",
    14  		"43.4292",
    15  	}
    16  
    17  	for i, test := range tests {
    18  		var d Decimal
    19  		d.SetString(test)
    20  
    21  		val, err := d.Value()
    22  		if err != nil {
    23  			t.Errorf("%d) %+v", i, err)
    24  		}
    25  
    26  		s, ok := val.(string)
    27  		if !ok {
    28  			t.Errorf("%d) wrong type returned", i)
    29  		}
    30  
    31  		if s != test {
    32  			t.Errorf("%d) want: %s, got: %s", i, test, s)
    33  		}
    34  	}
    35  
    36  	var infinity Decimal
    37  	infinity.SetInf(true)
    38  	if _, err := infinity.Value(); err == nil {
    39  		t.Error("infinity should not be passed into the database")
    40  	}
    41  	var nan Decimal
    42  	nan.SetNaN(true)
    43  	if _, err := nan.Value(); err == nil {
    44  		t.Error("nan should not be passed into the database")
    45  	}
    46  }
    47  
    48  func TestDecimal_Scan(t *testing.T) {
    49  	t.Parallel()
    50  
    51  	tests := []string{
    52  		"3.14",
    53  		"0",
    54  		"43.4292",
    55  	}
    56  
    57  	for i, test := range tests {
    58  		var d Decimal
    59  		if err := d.Scan(test); err != nil {
    60  			t.Error(err)
    61  		}
    62  
    63  		if got := d.String(); got != test {
    64  			t.Errorf("%d) want: %s, got: %s", i, test, got)
    65  		}
    66  	}
    67  
    68  	var d Decimal
    69  	if err := d.Scan(nil); err == nil {
    70  		t.Error("it should disallow scanning from a null value")
    71  	}
    72  }
    73  
    74  func TestNullDecimal_Value(t *testing.T) {
    75  	t.Parallel()
    76  
    77  	tests := []string{
    78  		"3.14",
    79  		"0",
    80  		"43.4292",
    81  	}
    82  
    83  	for i, test := range tests {
    84  		d := NullDecimal{Big: new(decimal.Big)}
    85  		d.Big, _ = d.SetString(test)
    86  
    87  		val, err := d.Value()
    88  		if err != nil {
    89  			t.Errorf("%d) %+v", i, err)
    90  		}
    91  
    92  		s, ok := val.(string)
    93  		if !ok {
    94  			t.Errorf("%d) wrong type returned", i)
    95  		}
    96  
    97  		if s != test {
    98  			t.Errorf("%d) want: %s, got: %s", i, test, s)
    99  		}
   100  	}
   101  
   102  	infinity := NullDecimal{Big: new(decimal.Big).SetInf(true)}
   103  	if _, err := infinity.Value(); err == nil {
   104  		t.Error("infinity should not be passed into the database")
   105  	}
   106  	nan := NullDecimal{Big: new(decimal.Big).SetNaN(true)}
   107  	if _, err := nan.Value(); err == nil {
   108  		t.Error("nan should not be passed into the database")
   109  	}
   110  }
   111  
   112  func TestNullDecimal_Scan(t *testing.T) {
   113  	t.Parallel()
   114  
   115  	tests := []string{
   116  		"3.14",
   117  		"0",
   118  		"43.4292",
   119  	}
   120  
   121  	for i, test := range tests {
   122  		var d NullDecimal
   123  		if err := d.Scan(test); err != nil {
   124  			t.Error(err)
   125  		}
   126  
   127  		if got := d.String(); got != test {
   128  			t.Errorf("%d) want: %s, got: %s", i, test, got)
   129  		}
   130  	}
   131  
   132  	var d NullDecimal
   133  	if err := d.Scan(nil); err != nil {
   134  		t.Error(err)
   135  	}
   136  	if d.Big != nil {
   137  		t.Error("it should have been nil")
   138  	}
   139  }