github.com/viacoin/blockbook@v0.3.2-0.20200124170434-77b4f2555a4b/api/types_test.go (about)

     1  // +build unittest
     2  
     3  package api
     4  
     5  import (
     6  	"encoding/json"
     7  	"math/big"
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  func TestAmount_MarshalJSON(t *testing.T) {
    13  	type amounts struct {
    14  		A1  Amount  `json:"a1"`
    15  		A2  Amount  `json:"a2,omitempty"`
    16  		PA1 *Amount `json:"pa1"`
    17  		PA2 *Amount `json:"pa2,omitempty"`
    18  	}
    19  	tests := []struct {
    20  		name string
    21  		a    amounts
    22  		want string
    23  	}{
    24  		{
    25  			name: "empty",
    26  			want: `{"a1":"0","a2":"0","pa1":null}`,
    27  		},
    28  		{
    29  			name: "1",
    30  			a: amounts{
    31  				A1:  (Amount)(*big.NewInt(123456)),
    32  				A2:  (Amount)(*big.NewInt(787901)),
    33  				PA1: (*Amount)(big.NewInt(234567)),
    34  				PA2: (*Amount)(big.NewInt(890123)),
    35  			},
    36  			want: `{"a1":"123456","a2":"787901","pa1":"234567","pa2":"890123"}`,
    37  		},
    38  	}
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			b, err := json.Marshal(&tt.a)
    42  			if err != nil {
    43  				t.Errorf("json.Marshal() error = %v", err)
    44  				return
    45  			}
    46  			if !reflect.DeepEqual(string(b), tt.want) {
    47  				t.Errorf("json.Marshal() = %v, want %v", string(b), tt.want)
    48  			}
    49  		})
    50  	}
    51  }
    52  
    53  func TestBalanceHistories_SortAndAggregate(t *testing.T) {
    54  	tests := []struct {
    55  		name        string
    56  		a           BalanceHistories
    57  		groupByTime uint32
    58  		want        BalanceHistories
    59  	}{
    60  		{
    61  			name:        "empty",
    62  			a:           []BalanceHistory{},
    63  			groupByTime: 3600,
    64  			want:        []BalanceHistory{},
    65  		},
    66  		{
    67  			name: "one",
    68  			a: []BalanceHistory{
    69  				{
    70  					ReceivedSat: (*Amount)(big.NewInt(1)),
    71  					SentSat:     (*Amount)(big.NewInt(2)),
    72  					Time:        1521514812,
    73  					Txid:        "00b2c06055e5e90e9c82bd4181fde310104391a7fa4f289b1704e5d90caa3840",
    74  					Txs:         1,
    75  				},
    76  			},
    77  			groupByTime: 3600,
    78  			want: []BalanceHistory{
    79  				{
    80  					ReceivedSat: (*Amount)(big.NewInt(1)),
    81  					SentSat:     (*Amount)(big.NewInt(2)),
    82  					Time:        1521514800,
    83  					Txs:         1,
    84  				},
    85  			},
    86  		},
    87  		{
    88  			name: "aggregate",
    89  			a: []BalanceHistory{
    90  				{
    91  					ReceivedSat: (*Amount)(big.NewInt(1)),
    92  					SentSat:     (*Amount)(big.NewInt(2)),
    93  					Time:        1521504812,
    94  					Txid:        "0011223344556677889900112233445566778899001122334455667788990011",
    95  					Txs:         1,
    96  				},
    97  				{
    98  					ReceivedSat: (*Amount)(big.NewInt(3)),
    99  					SentSat:     (*Amount)(big.NewInt(4)),
   100  					Time:        1521504812,
   101  					Txid:        "00b2c06055e5e90e9c82bd4181fde310104391a7fa4f289b1704e5d90caa3840",
   102  					Txs:         1,
   103  				},
   104  				{
   105  					ReceivedSat: (*Amount)(big.NewInt(5)),
   106  					SentSat:     (*Amount)(big.NewInt(6)),
   107  					Time:        1521514812,
   108  					Txid:        "00b2c06055e5e90e9c82bd4181fde310104391a7fa4f289b1704e5d90caa3840",
   109  					Txs:         1,
   110  				},
   111  				{
   112  					ReceivedSat: (*Amount)(big.NewInt(7)),
   113  					SentSat:     (*Amount)(big.NewInt(8)),
   114  					Time:        1521504812,
   115  					Txid:        "00b2c06055e5e90e9c82bd4181fde310104391a7fa4f289b1704e5d90caa3840",
   116  					Txs:         1,
   117  				},
   118  				{
   119  					ReceivedSat: (*Amount)(big.NewInt(9)),
   120  					SentSat:     (*Amount)(big.NewInt(10)),
   121  					Time:        1521534812,
   122  					Txid:        "0011223344556677889900112233445566778899001122334455667788990011",
   123  					Txs:         1,
   124  				},
   125  				{
   126  					ReceivedSat: (*Amount)(big.NewInt(11)),
   127  					SentSat:     (*Amount)(big.NewInt(12)),
   128  					Time:        1521534812,
   129  					Txid:        "1122334455667788990011223344556677889900112233445566778899001100",
   130  					Txs:         1,
   131  				},
   132  			},
   133  			groupByTime: 3600,
   134  			want: []BalanceHistory{
   135  				{
   136  					ReceivedSat: (*Amount)(big.NewInt(11)),
   137  					SentSat:     (*Amount)(big.NewInt(14)),
   138  					Time:        1521504000,
   139  					Txs:         2,
   140  				},
   141  				{
   142  					ReceivedSat: (*Amount)(big.NewInt(5)),
   143  					SentSat:     (*Amount)(big.NewInt(6)),
   144  					Time:        1521514800,
   145  					Txs:         1,
   146  				},
   147  				{
   148  					ReceivedSat: (*Amount)(big.NewInt(20)),
   149  					SentSat:     (*Amount)(big.NewInt(22)),
   150  					Time:        1521532800,
   151  					Txs:         2,
   152  				},
   153  			},
   154  		},
   155  	}
   156  	for _, tt := range tests {
   157  		t.Run(tt.name, func(t *testing.T) {
   158  			if got := tt.a.SortAndAggregate(tt.groupByTime); !reflect.DeepEqual(got, tt.want) {
   159  				t.Errorf("BalanceHistories.SortAndAggregate() = %+v, want %+v", got, tt.want)
   160  			}
   161  		})
   162  	}
   163  }