code.vegaprotocol.io/vega@v0.79.0/core/datasource/internal/timetrigger/timetrigger_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  //lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types
    17  
    18  package timetrigger_test
    19  
    20  import (
    21  	"testing"
    22  	"time"
    23  
    24  	"code.vegaprotocol.io/vega/core/datasource"
    25  	"code.vegaprotocol.io/vega/core/datasource/common"
    26  	"code.vegaprotocol.io/vega/core/datasource/internal/timetrigger"
    27  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    28  	datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestSpecConfigurationIntoProto(t *testing.T) {
    34  	t.Run("non-empty time source with empty lists", func(t *testing.T) {
    35  		ds := datasource.NewDefinitionWith(timetrigger.SpecConfiguration{})
    36  		protoDs := ds.IntoProto()
    37  		assert.IsType(t, &vegapb.DataSourceDefinition{}, protoDs)
    38  		assert.NotNil(t, protoDs.SourceType)
    39  		ext := protoDs.GetInternal()
    40  		assert.NotNil(t, ext)
    41  		o := ext.GetTimeTrigger()
    42  		assert.Equal(t, 0, len(o.Conditions))
    43  	})
    44  
    45  	t.Run("non-empty time source with data", func(t *testing.T) {
    46  		timeNow := time.Now()
    47  		ds := datasource.NewDefinitionWith(timetrigger.SpecConfiguration{
    48  			Triggers: common.InternalTimeTriggers{
    49  				{
    50  					Initial: &timeNow,
    51  					Every:   int64(15),
    52  				},
    53  			},
    54  			Conditions: []*common.SpecCondition{
    55  				{},
    56  				{
    57  					Operator: datapb.Condition_OPERATOR_EQUALS,
    58  					Value:    "14",
    59  				},
    60  				{
    61  					Operator: datapb.Condition_OPERATOR_GREATER_THAN,
    62  					Value:    "9",
    63  				},
    64  			},
    65  		})
    66  
    67  		protoDs := ds.IntoProto()
    68  		assert.IsType(t, &vegapb.DataSourceDefinition{}, protoDs)
    69  		assert.NotNil(t, protoDs.SourceType)
    70  		internal := protoDs.GetInternal()
    71  		assert.NotNil(t, internal)
    72  		o := internal.GetTimeTrigger()
    73  		assert.Equal(t, 3, len(o.Conditions))
    74  		assert.Equal(t, datapb.Condition_Operator(0), o.Conditions[0].Operator)
    75  		assert.Equal(t, "", o.Conditions[0].Value)
    76  		assert.Equal(t, datapb.Condition_OPERATOR_EQUALS, o.Conditions[1].Operator)
    77  		assert.Equal(t, "14", o.Conditions[1].Value)
    78  		assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, o.Conditions[2].Operator)
    79  		assert.Equal(t, "9", o.Conditions[2].Value)
    80  		assert.IsType(t, &datapb.InternalTimeTrigger{}, o.Triggers[0])
    81  		assert.Equal(t, timeNow.Unix(), *o.Triggers[0].Initial)
    82  		assert.Equal(t, int64(15), o.Triggers[0].Every)
    83  	})
    84  }
    85  
    86  func TestSpecConfigurationGetFilters(t *testing.T) {
    87  	timeNow := time.Now()
    88  	ds := datasource.NewDefinitionWith(timetrigger.SpecConfiguration{
    89  		Triggers: common.InternalTimeTriggers{
    90  			{
    91  				Initial: &timeNow,
    92  				Every:   int64(15),
    93  			},
    94  		},
    95  		Conditions: []*common.SpecCondition{
    96  			{},
    97  			{
    98  				Operator: datapb.Condition_OPERATOR_EQUALS,
    99  				Value:    "14",
   100  			},
   101  			{
   102  				Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   103  				Value:    "9",
   104  			},
   105  		},
   106  	})
   107  
   108  	filters := ds.GetFilters()
   109  	assert.NotNil(t, filters)
   110  	assert.Equal(t, 1, len(filters))
   111  	assert.IsType(t, &common.SpecFilter{}, filters[0])
   112  	assert.Equal(t, timetrigger.InternalTimeTriggerKey, filters[0].Key.Name)
   113  	assert.Equal(t, datapb.PropertyKey_TYPE_TIMESTAMP, filters[0].Key.Type)
   114  	assert.Equal(t, 3, len(filters[0].Conditions))
   115  	assert.Equal(t, datapb.Condition_OPERATOR_EQUALS, filters[0].Conditions[1].Operator)
   116  	assert.Equal(t, "14", filters[0].Conditions[1].Value)
   117  	assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, filters[0].Conditions[2].Operator)
   118  	assert.Equal(t, "14", filters[0].Conditions[1].Value)
   119  }
   120  
   121  func TestSpecConfigurationGetTimeTriggers(t *testing.T) {
   122  	timeNow := time.Now()
   123  	ds := datasource.NewDefinitionWith(timetrigger.SpecConfiguration{
   124  		Triggers: common.InternalTimeTriggers{
   125  			{
   126  				Initial: &timeNow,
   127  				Every:   int64(15),
   128  			},
   129  		},
   130  		Conditions: []*common.SpecCondition{
   131  			{},
   132  			{
   133  				Operator: datapb.Condition_OPERATOR_EQUALS,
   134  				Value:    "14",
   135  			},
   136  			{
   137  				Operator: datapb.Condition_OPERATOR_GREATER_THAN,
   138  				Value:    "9",
   139  			},
   140  		},
   141  	})
   142  
   143  	triggers := ds.GetTimeTriggers()
   144  	assert.NotNil(t, triggers)
   145  	assert.Equal(t, 1, len(triggers))
   146  	assert.IsType(t, &common.InternalTimeTrigger{}, triggers[0])
   147  	assert.Equal(t, timeNow, *triggers[0].Initial)
   148  	assert.Equal(t, int64(15), triggers[0].Every)
   149  }