code.vegaprotocol.io/vega@v0.79.0/datanode/gateway/graphql/oracles_resolver_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 gql
    17  
    18  import (
    19  	"context"
    20  	"encoding/json"
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  
    25  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    26  	v1 "code.vegaprotocol.io/vega/protos/vega/data/v1"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func Test_oracleSpecResolver_DataSourceSpec(t *testing.T) {
    32  	type args struct {
    33  		in0 context.Context
    34  		obj *vegapb.OracleSpec
    35  	}
    36  	var timeBasic time.Time
    37  	timeNow := uint64(timeBasic.UnixNano())
    38  	tests := []struct {
    39  		name    string
    40  		o       oracleSpecResolver
    41  		args    args
    42  		wantJsn string
    43  		wantErr assert.ErrorAssertionFunc
    44  	}{
    45  		{
    46  			name: "success: DataSourceDefinition_External",
    47  			args: args{
    48  				obj: &vegapb.OracleSpec{
    49  					ExternalDataSourceSpec: &vegapb.ExternalDataSourceSpec{
    50  						Spec: &vegapb.DataSourceSpec{
    51  							Status: vegapb.DataSourceSpec_STATUS_ACTIVE,
    52  							Data: &vegapb.DataSourceDefinition{
    53  								SourceType: &vegapb.DataSourceDefinition_External{
    54  									External: &vegapb.DataSourceDefinitionExternal{
    55  										SourceType: &vegapb.DataSourceDefinitionExternal_Oracle{
    56  											Oracle: &vegapb.DataSourceSpecConfiguration{
    57  												Signers: []*v1.Signer{
    58  													{
    59  														Signer: &v1.Signer_PubKey{
    60  															PubKey: &v1.PubKey{
    61  																Key: "key",
    62  															},
    63  														},
    64  													}, {
    65  														Signer: &v1.Signer_EthAddress{
    66  															EthAddress: &v1.ETHAddress{
    67  																Address: "address",
    68  															},
    69  														},
    70  													},
    71  												},
    72  											},
    73  										},
    74  									},
    75  								},
    76  							},
    77  						},
    78  					},
    79  				},
    80  			},
    81  			wantJsn: `{"spec":{"id":"","createdAt":0,"data":{"SourceType":{"External":{"SourceType":{"Oracle":{"signers":[{"Signer":{"PubKey":{"key":"key"}}},{"Signer":{"EthAddress":{"address":"address"}}}]}}}}},"status":"STATUS_ACTIVE"}}`,
    82  			wantErr: assert.NoError,
    83  		},
    84  		{
    85  			name: "success: DataSourceDefinition_Internal",
    86  			args: args{
    87  				obj: &vegapb.OracleSpec{
    88  					ExternalDataSourceSpec: &vegapb.ExternalDataSourceSpec{
    89  						Spec: &vegapb.DataSourceSpec{
    90  							Status: vegapb.DataSourceSpec_STATUS_ACTIVE,
    91  							Data: &vegapb.DataSourceDefinition{
    92  								SourceType: &vegapb.DataSourceDefinition_Internal{
    93  									Internal: &vegapb.DataSourceDefinitionInternal{
    94  										SourceType: &vegapb.DataSourceDefinitionInternal_Time{
    95  											Time: &vegapb.DataSourceSpecConfigurationTime{
    96  												Conditions: []*v1.Condition{
    97  													{
    98  														Operator: 12,
    99  														Value:    "blah",
   100  													},
   101  												},
   102  											},
   103  										},
   104  									},
   105  								},
   106  							},
   107  						},
   108  					},
   109  				},
   110  			},
   111  			wantJsn: `{"spec":{"id":"","createdAt":0,"data":{"SourceType":{"Internal":{"SourceType":{"Time":{"conditions":[{"operator":12,"value":"blah"}]}}}}},"status":"STATUS_ACTIVE"}}`,
   112  			wantErr: assert.NoError,
   113  		},
   114  		{
   115  			name: "success: DataSourceDefinition_External",
   116  			args: args{
   117  				obj: &vegapb.OracleSpec{
   118  					ExternalDataSourceSpec: &vegapb.ExternalDataSourceSpec{
   119  						Spec: &vegapb.DataSourceSpec{
   120  							Status: vegapb.DataSourceSpec_STATUS_ACTIVE,
   121  							Data: &vegapb.DataSourceDefinition{
   122  								SourceType: &vegapb.DataSourceDefinition_External{
   123  									External: &vegapb.DataSourceDefinitionExternal{
   124  										SourceType: &vegapb.DataSourceDefinitionExternal_EthOracle{
   125  											EthOracle: &vegapb.EthCallSpec{
   126  												Address: "test-address",
   127  												Abi:     "",
   128  												Args:    nil,
   129  												Method:  "stake",
   130  												Trigger: &vegapb.EthCallTrigger{
   131  													Trigger: &vegapb.EthCallTrigger_TimeTrigger{
   132  														TimeTrigger: &vegapb.EthTimeTrigger{
   133  															Initial: &timeNow,
   134  														},
   135  													},
   136  												},
   137  												RequiredConfirmations: uint64(0),
   138  												Filters: []*v1.Filter{
   139  													{
   140  														Key: &v1.PropertyKey{
   141  															Name: "property-name",
   142  															Type: v1.PropertyKey_TYPE_BOOLEAN,
   143  														},
   144  													},
   145  												},
   146  											},
   147  										},
   148  									},
   149  								},
   150  							},
   151  						},
   152  					},
   153  				},
   154  			},
   155  			wantJsn: `{"spec":{"id":"","createdAt":0,"data":{"SourceType":{"External":{"SourceType":{"EthOracle":{"address":"test-address","method":"stake","trigger":{"Trigger":{"TimeTrigger":{"initial":11651379494838206464}}},"filters":[{"key":{"name":"property-name","type":4}}]}}}}},"status":"STATUS_ACTIVE"}}`,
   156  			wantErr: assert.NoError,
   157  		},
   158  	}
   159  	for _, tt := range tests {
   160  		t.Run(tt.name, func(t *testing.T) {
   161  			got, err := tt.o.DataSourceSpec(tt.args.in0, tt.args.obj)
   162  			if !tt.wantErr(t, err, fmt.Sprintf("DataSourceSpec(%v, %v)", tt.args.in0, tt.args.obj)) {
   163  				return
   164  			}
   165  
   166  			gotJsn, _ := json.Marshal(got)
   167  			assert.JSONEqf(t, tt.wantJsn, string(gotJsn), "mismatch(%v):\n\twant: %s \n\tgot: %s", tt.name, tt.wantJsn, string(gotJsn))
   168  		})
   169  	}
   170  }