code.vegaprotocol.io/vega@v0.79.0/core/datasource/internal/vegatime/vegatime_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 vegatime_test
    17  
    18  import (
    19  	"testing"
    20  
    21  	"code.vegaprotocol.io/vega/core/datasource"
    22  	"code.vegaprotocol.io/vega/core/datasource/common"
    23  	"code.vegaprotocol.io/vega/core/datasource/internal/vegatime"
    24  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    25  	datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
    26  	v1 "code.vegaprotocol.io/vega/protos/vega/data/v1"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestSpecConfigurationIntoProto(t *testing.T) {
    32  	t.Run("non-empty time source with empty lists", func(t *testing.T) {
    33  		ds := datasource.NewDefinitionWith(vegatime.SpecConfiguration{})
    34  		protoDs := ds.IntoProto()
    35  		assert.IsType(t, &vegapb.DataSourceDefinition{}, protoDs)
    36  		assert.NotNil(t, protoDs.SourceType)
    37  		ext := protoDs.GetInternal()
    38  		assert.NotNil(t, ext)
    39  		o := ext.GetTime()
    40  		assert.Equal(t, 0, len(o.Conditions))
    41  	})
    42  
    43  	t.Run("non-empty time source with data", func(t *testing.T) {
    44  		ds := datasource.NewDefinitionWith(vegatime.SpecConfiguration{
    45  			Conditions: []*common.SpecCondition{
    46  				{},
    47  				{
    48  					Operator: datapb.Condition_OPERATOR_EQUALS,
    49  					Value:    "14",
    50  				},
    51  				{
    52  					Operator: datapb.Condition_OPERATOR_GREATER_THAN,
    53  					Value:    "9",
    54  				},
    55  			},
    56  		})
    57  
    58  		protoDs := ds.IntoProto()
    59  		assert.IsType(t, &vegapb.DataSourceDefinition{}, protoDs)
    60  		assert.NotNil(t, protoDs.SourceType)
    61  		ext := protoDs.GetInternal()
    62  		assert.NotNil(t, ext)
    63  		o := ext.GetTime()
    64  		assert.Equal(t, 3, len(o.Conditions))
    65  		assert.Equal(t, datapb.Condition_Operator(0), o.Conditions[0].Operator)
    66  		assert.Equal(t, "", o.Conditions[0].Value)
    67  		assert.Equal(t, datapb.Condition_OPERATOR_EQUALS, o.Conditions[1].Operator)
    68  		assert.Equal(t, "14", o.Conditions[1].Value)
    69  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, o.Conditions[2].Operator)
    70  		assert.Equal(t, "9", o.Conditions[2].Value)
    71  	})
    72  }
    73  
    74  func TestSpecConfigurationString(t *testing.T) {
    75  	t.Run("non-empty time source with empty lists", func(t *testing.T) {
    76  		ds := datasource.NewDefinitionWith(vegatime.SpecConfiguration{}).String()
    77  		assert.Equal(t, "conditions([])", ds)
    78  	})
    79  
    80  	t.Run("non-empty time source with data", func(t *testing.T) {
    81  		ds := datasource.NewDefinitionWith(vegatime.SpecConfiguration{
    82  			Conditions: []*common.SpecCondition{
    83  				{},
    84  				{
    85  					Operator: datapb.Condition_OPERATOR_EQUALS,
    86  					Value:    "14",
    87  				},
    88  				{
    89  					Operator: datapb.Condition_OPERATOR_GREATER_THAN,
    90  					Value:    "9",
    91  				},
    92  			},
    93  		}).String()
    94  
    95  		assert.Equal(t, "conditions([value() operator(OPERATOR_UNSPECIFIED), value(14) operator(OPERATOR_EQUALS), value(9) operator(OPERATOR_GREATER_THAN)])", ds)
    96  	})
    97  }
    98  
    99  func TestSpecConfigurationFromProto(t *testing.T) {
   100  	t.Run("empty", func(t *testing.T) {
   101  		s := vegatime.SpecConfigurationFromProto(nil)
   102  
   103  		assert.NotNil(t, s)
   104  		assert.IsType(t, vegatime.SpecConfiguration{}, s)
   105  
   106  		assert.Nil(t, s.Conditions)
   107  	})
   108  
   109  	t.Run("non-empty with empty lists", func(t *testing.T) {
   110  		s := vegatime.SpecConfigurationFromProto(
   111  			&vegapb.DataSourceSpecConfigurationTime{
   112  				Conditions: nil,
   113  			},
   114  		)
   115  		assert.NotNil(t, s)
   116  		assert.IsType(t, vegatime.SpecConfiguration{}, s)
   117  		assert.NotNil(t, s.Conditions)
   118  		assert.Equal(t, 0, len(s.Conditions))
   119  	})
   120  
   121  	t.Run("non-empty with data", func(t *testing.T) {
   122  		s := vegatime.SpecConfigurationFromProto(
   123  			&vegapb.DataSourceSpecConfigurationTime{
   124  				Conditions: []*v1.Condition{
   125  					{},
   126  					{
   127  						Operator: datapb.Condition_OPERATOR_EQUALS,
   128  						Value:    "14",
   129  					},
   130  					{
   131  						Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   132  						Value:    "9",
   133  					},
   134  				},
   135  			},
   136  		)
   137  		assert.NotNil(t, s)
   138  		assert.Equal(t, 3, len(s.Conditions))
   139  		assert.Equal(t, v1.Condition_Operator(1), s.Conditions[1].Operator)
   140  		assert.Equal(t, "14", s.Conditions[1].Value)
   141  		assert.Equal(t, v1.Condition_Operator(2), s.Conditions[2].Operator)
   142  		assert.Equal(t, "9", s.Conditions[2].Value)
   143  	})
   144  }