code.vegaprotocol.io/vega@v0.79.0/datanode/entities/candle_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package entities_test
    17  
    18  import (
    19  	"testing"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/datanode/entities"
    23  	"code.vegaprotocol.io/vega/protos/vega"
    24  
    25  	"github.com/shopspring/decimal"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestProtoFromCandle(t *testing.T) {
    30  	periodStart := time.Now()
    31  	lastUpdate := periodStart.Add(5 * time.Microsecond)
    32  	candle := entities.Candle{
    33  		PeriodStart:        periodStart,
    34  		LastUpdateInPeriod: lastUpdate,
    35  		Open:               decimal.NewFromInt(4),
    36  		Close:              decimal.NewFromInt(5),
    37  		High:               decimal.NewFromInt(6),
    38  		Low:                decimal.NewFromInt(7),
    39  		Volume:             30,
    40  	}
    41  
    42  	p, err := candle.ToV1CandleProto(vega.Interval_INTERVAL_I6H)
    43  	if err != nil {
    44  		t.Fatalf("failed to conver proto to candle:%s", err)
    45  	}
    46  
    47  	assert.Equal(t, periodStart.UnixNano(), p.Timestamp)
    48  	assert.Equal(t, lastUpdate.Format(time.RFC3339Nano), p.Datetime)
    49  	assert.Equal(t, "4", p.Open)
    50  	assert.Equal(t, "5", p.Close)
    51  	assert.Equal(t, "6", p.High)
    52  	assert.Equal(t, "7", p.Low)
    53  	assert.Equal(t, uint64(30), p.Volume)
    54  	assert.Equal(t, vega.Interval_INTERVAL_I6H, p.Interval)
    55  }