github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/types/decimal_test.go (about)

     1  package types
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/ericlagergren/decimal"
     9  	"github.com/volatiletech/sqlboiler/v4/queries/qmhelper"
    10  )
    11  
    12  func TestDecimal_Value(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	tests := []string{
    16  		"3.14",
    17  		"0",
    18  		"43.4292",
    19  	}
    20  
    21  	for i, test := range tests {
    22  		d := Decimal{Big: new(decimal.Big)}
    23  		d.Big, _ = d.SetString(test)
    24  
    25  		val, err := d.Value()
    26  		if err != nil {
    27  			t.Errorf("%d) %+v", i, err)
    28  		}
    29  
    30  		s, ok := val.(string)
    31  		if !ok {
    32  			t.Errorf("%d) wrong type returned", i)
    33  		}
    34  
    35  		if s != test {
    36  			t.Errorf("%d) want: %s, got: %s", i, test, s)
    37  		}
    38  	}
    39  
    40  	zero := Decimal{}
    41  	if _, err := zero.Value(); err != nil {
    42  		t.Error("zero value should not error")
    43  	}
    44  	infinity := Decimal{Big: new(decimal.Big).SetInf(true)}
    45  	if _, err := infinity.Value(); err == nil {
    46  		t.Error("infinity should not be passed into the database")
    47  	}
    48  	nan := Decimal{Big: new(decimal.Big).SetNaN(true)}
    49  	if _, err := nan.Value(); err == nil {
    50  		t.Error("nan should not be passed into the database")
    51  	}
    52  }
    53  
    54  func TestDecimal_Scan(t *testing.T) {
    55  	t.Parallel()
    56  
    57  	tests := []string{
    58  		"3.14",
    59  		"0",
    60  		"43.4292",
    61  	}
    62  
    63  	for i, test := range tests {
    64  		var d Decimal
    65  		if err := d.Scan(test); err != nil {
    66  			t.Error(err)
    67  		}
    68  
    69  		if got := d.String(); got != test {
    70  			t.Errorf("%d) want: %s, got: %s", i, test, got)
    71  		}
    72  	}
    73  
    74  	var d Decimal
    75  	if err := d.Scan(nil); err == nil {
    76  		t.Error("it should disallow scanning from a null value")
    77  	}
    78  }
    79  
    80  func TestNullDecimal_Value(t *testing.T) {
    81  	t.Parallel()
    82  
    83  	tests := []string{
    84  		"3.14",
    85  		"0",
    86  		"43.4292",
    87  	}
    88  
    89  	for i, test := range tests {
    90  		d := NullDecimal{Big: new(decimal.Big)}
    91  		d.Big, _ = d.SetString(test)
    92  
    93  		val, err := d.Value()
    94  		if err != nil {
    95  			t.Errorf("%d) %+v", i, err)
    96  		}
    97  
    98  		s, ok := val.(string)
    99  		if !ok {
   100  			t.Errorf("%d) wrong type returned", i)
   101  		}
   102  
   103  		if s != test {
   104  			t.Errorf("%d) want: %s, got: %s", i, test, s)
   105  		}
   106  	}
   107  
   108  	zero := NullDecimal{}
   109  	if _, err := zero.Value(); err != nil {
   110  		t.Error("zero value should not error")
   111  	}
   112  	infinity := NullDecimal{Big: new(decimal.Big).SetInf(true)}
   113  	if _, err := infinity.Value(); err == nil {
   114  		t.Error("infinity should not be passed into the database")
   115  	}
   116  	nan := NullDecimal{Big: new(decimal.Big).SetNaN(true)}
   117  	if _, err := nan.Value(); err == nil {
   118  		t.Error("nan should not be passed into the database")
   119  	}
   120  }
   121  
   122  func TestNullDecimal_Scan(t *testing.T) {
   123  	t.Parallel()
   124  
   125  	tests := []string{
   126  		"3.14",
   127  		"0",
   128  		"43.4292",
   129  	}
   130  
   131  	for i, test := range tests {
   132  		var d NullDecimal
   133  		if err := d.Scan(test); err != nil {
   134  			t.Error(err)
   135  		}
   136  
   137  		if got := d.String(); got != test {
   138  			t.Errorf("%d) want: %s, got: %s", i, test, got)
   139  		}
   140  	}
   141  
   142  	var d NullDecimal
   143  	if err := d.Scan(nil); err != nil {
   144  		t.Error(err)
   145  	}
   146  	if d.Big != nil {
   147  		t.Error("it should have been nil")
   148  	}
   149  }
   150  
   151  func TestDecimal_JSON(t *testing.T) {
   152  	t.Parallel()
   153  
   154  	s := struct {
   155  		D Decimal `json:"d"`
   156  	}{}
   157  
   158  	err := json.Unmarshal([]byte(`{"d":"54.45"}`), &s)
   159  	if err != nil {
   160  		t.Error(err)
   161  	}
   162  
   163  	want, _ := new(decimal.Big).SetString("54.45")
   164  	if s.D.Cmp(want) != 0 {
   165  		t.Error("D was wrong:", s.D)
   166  	}
   167  }
   168  
   169  func TestNullDecimal_JSON(t *testing.T) {
   170  	t.Parallel()
   171  
   172  	s := struct {
   173  		N NullDecimal `json:"n"`
   174  	}{}
   175  
   176  	err := json.Unmarshal([]byte(`{"n":"54.45"}`), &s)
   177  	if err != nil {
   178  		t.Error(err)
   179  	}
   180  
   181  	want, _ := new(decimal.Big).SetString("54.45")
   182  	if s.N.Cmp(want) != 0 {
   183  		fmt.Println(want, s.N)
   184  		t.Error("N was wrong:", s.N)
   185  	}
   186  }
   187  
   188  func TestNullDecimal_JSONNil(t *testing.T) {
   189  	t.Parallel()
   190  
   191  	var n NullDecimal
   192  	b, _ := json.Marshal(n)
   193  	if string(b) != `null` {
   194  		t.Errorf("want: null, got: %s", b)
   195  	}
   196  
   197  	n2 := new(NullDecimal)
   198  	b, _ = json.Marshal(n2)
   199  	if string(b) != `null` {
   200  		t.Errorf("want: null, got: %s", b)
   201  	}
   202  }
   203  
   204  func TestNullDecimal_IsZero(t *testing.T) {
   205  	t.Parallel()
   206  
   207  	var nullable qmhelper.Nullable = NullDecimal{}
   208  
   209  	if !nullable.IsZero() {
   210  		t.Error("it should be zero")
   211  	}
   212  
   213  	nullable = NullDecimal{Big: new(decimal.Big)}
   214  	if nullable.IsZero() {
   215  		t.Error("it should not be zero")
   216  	}
   217  }