code.vegaprotocol.io/vega@v0.79.0/core/datasource/common/data_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 common_test
    17  
    18  import (
    19  	"testing"
    20  
    21  	"code.vegaprotocol.io/vega/core/datasource/common"
    22  	"code.vegaprotocol.io/vega/libs/num"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestOracleData(t *testing.T) {
    29  	t.Run("Getting integer when not present fails", testDataGetMissingIntegerFails)
    30  	t.Run("Getting decimal when not present fails", testDataGetMissingDecimalFails)
    31  	t.Run("Getting boolean when not present fails", testDataGetMissingBooleanFails)
    32  	t.Run("Getting timestamp when not present fails", testDataGetMissingTimestampFails)
    33  	t.Run("Getting string when not present fails", testDataGetMissingStringFails)
    34  	t.Run("Getting integer when not an integer fails", testDataGetIntegerFails)
    35  	t.Run("Getting decimal when not a decimal fails", testDataGetDecimalFails)
    36  	t.Run("Getting boolean when not a boolean fails", testDataGetBooleanFails)
    37  	t.Run("Getting timestamp when not a timestamp fails", testDataGetTimestampFails)
    38  	t.Run("Getting integer succeeds", testDataGetIntegerSucceeds)
    39  	t.Run("Getting decimal succeeds", testDataGetDecimalSucceeds)
    40  	t.Run("Getting boolean succeeds", testDataGetBooleanSucceeds)
    41  	t.Run("Getting timestamp succeeds", testDataGetTimestampSucceeds)
    42  	t.Run("Getting string succeeds", testDataGetStringSucceeds)
    43  	t.Run("Getting uint when not present fails", testDataGetMissingUintFails)
    44  	t.Run("Getting uint when not a uint fails", testDataGetUintFails)
    45  	t.Run("Getting uint succeeds", testDataGetUintSucceeds)
    46  	t.Run("Determining the origin succeeds", testDataDeterminingOriginSucceeds)
    47  }
    48  
    49  func testDataGetMissingUintFails(t *testing.T) {
    50  	// given
    51  	data := common.Data{
    52  		Signers: []*common.Signer{
    53  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
    54  		},
    55  		Data: map[string]string{
    56  			"my_key": "42",
    57  		},
    58  	}
    59  
    60  	// when
    61  	_, err := data.GetUint("my_other_key")
    62  
    63  	// then
    64  	require.Error(t, err)
    65  	assert.Equal(t, "property \"my_other_key\" not found", err.Error())
    66  }
    67  
    68  func testDataGetUintFails(t *testing.T) {
    69  	// given
    70  	data := common.Data{
    71  		Signers: []*common.Signer{
    72  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
    73  		},
    74  		Data: map[string]string{
    75  			"my_key": "not an integer",
    76  		},
    77  	}
    78  
    79  	// when
    80  	_, err := data.GetUint("my_key")
    81  
    82  	// then
    83  	require.Error(t, err)
    84  	assert.Equal(t, "could not parse value 'not an integer' for property 'my_key'", err.Error())
    85  }
    86  
    87  func testDataGetUintSucceeds(t *testing.T) {
    88  	expect := num.NewUint(123)
    89  	// given
    90  	data := common.Data{
    91  		Signers: []*common.Signer{
    92  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
    93  		},
    94  		Data: map[string]string{
    95  			"my_key": expect.String(),
    96  		},
    97  	}
    98  
    99  	// when
   100  	value, err := data.GetUint("my_key")
   101  
   102  	// then
   103  	require.NoError(t, err)
   104  	require.True(t, expect.EQ(value))
   105  }
   106  
   107  func testDataGetMissingIntegerFails(t *testing.T) {
   108  	// given
   109  	data := common.Data{
   110  		Signers: []*common.Signer{
   111  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   112  		},
   113  		Data: map[string]string{
   114  			"my_key": "42",
   115  		},
   116  	}
   117  
   118  	// when
   119  	_, err := data.GetInteger("my_other_key")
   120  
   121  	// then
   122  	require.Error(t, err)
   123  	assert.Equal(t, "property \"my_other_key\" not found", err.Error())
   124  }
   125  
   126  func testDataGetMissingDecimalFails(t *testing.T) {
   127  	// given
   128  	data := common.Data{
   129  		Signers: []*common.Signer{
   130  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   131  		},
   132  		Data: map[string]string{
   133  			"my_key": "12.34",
   134  		},
   135  	}
   136  
   137  	// when
   138  	_, err := data.GetDecimal("my_other_key")
   139  
   140  	// then
   141  	require.Error(t, err)
   142  	assert.Equal(t, "property \"my_other_key\" not found", err.Error())
   143  }
   144  
   145  func testDataGetMissingBooleanFails(t *testing.T) {
   146  	// given
   147  	data := common.Data{
   148  		Signers: []*common.Signer{
   149  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   150  		},
   151  		Data: map[string]string{
   152  			"my_key": "true",
   153  		},
   154  	}
   155  
   156  	// when
   157  	_, err := data.GetBoolean("my_other_key")
   158  
   159  	// then
   160  	require.Error(t, err)
   161  	assert.Equal(t, "property \"my_other_key\" not found", err.Error())
   162  }
   163  
   164  func testDataGetMissingTimestampFails(t *testing.T) {
   165  	// given
   166  	data := common.Data{
   167  		Signers: []*common.Signer{
   168  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   169  		},
   170  		Data: map[string]string{
   171  			"my_key": "10000000",
   172  		},
   173  	}
   174  
   175  	// when
   176  	_, err := data.GetTimestamp("my_other_key")
   177  
   178  	// then
   179  	require.Error(t, err)
   180  	assert.Equal(t, "property \"my_other_key\" not found", err.Error())
   181  }
   182  
   183  func testDataGetMissingStringFails(t *testing.T) {
   184  	// given
   185  	data := common.Data{
   186  		Signers: []*common.Signer{
   187  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   188  		},
   189  		Data: map[string]string{
   190  			"my_key": "hello",
   191  		},
   192  	}
   193  
   194  	// when
   195  	_, err := data.GetString("my_other_key")
   196  
   197  	// then
   198  	require.Error(t, err)
   199  	assert.Equal(t, "property \"my_other_key\" not found", err.Error())
   200  }
   201  
   202  func testDataGetIntegerFails(t *testing.T) {
   203  	// given
   204  	data := common.Data{
   205  		Signers: []*common.Signer{
   206  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   207  		},
   208  		Data: map[string]string{
   209  			"my_key": "not an integer",
   210  		},
   211  	}
   212  
   213  	// when
   214  	_, err := data.GetInteger("my_key")
   215  
   216  	// then
   217  	require.Error(t, err)
   218  }
   219  
   220  func testDataGetDecimalFails(t *testing.T) {
   221  	// given
   222  	data := common.Data{
   223  		Signers: []*common.Signer{
   224  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   225  		},
   226  		Data: map[string]string{
   227  			"my_key": "not a decimal",
   228  		},
   229  	}
   230  
   231  	// when
   232  	_, err := data.GetDecimal("my_key")
   233  
   234  	// then
   235  	require.Error(t, err)
   236  }
   237  
   238  func testDataGetBooleanFails(t *testing.T) {
   239  	// given
   240  	data := common.Data{
   241  		Signers: []*common.Signer{
   242  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   243  		},
   244  		Data: map[string]string{
   245  			"my_key": "not a boolean",
   246  		},
   247  	}
   248  
   249  	// when
   250  	_, err := data.GetBoolean("my_key")
   251  
   252  	// then
   253  	require.Error(t, err)
   254  }
   255  
   256  func testDataGetTimestampFails(t *testing.T) {
   257  	// given
   258  	data := common.Data{
   259  		Signers: []*common.Signer{
   260  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   261  		},
   262  		Data: map[string]string{
   263  			"my_key": "not an integer",
   264  		},
   265  	}
   266  
   267  	// when
   268  	_, err := data.GetTimestamp("my_key")
   269  
   270  	// then
   271  	require.Error(t, err)
   272  }
   273  
   274  func testDataGetIntegerSucceeds(t *testing.T) {
   275  	// given
   276  	data := common.Data{
   277  		Signers: []*common.Signer{
   278  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   279  		},
   280  		Data: map[string]string{
   281  			"my_key": "42",
   282  		},
   283  	}
   284  
   285  	// when
   286  	value, err := data.GetInteger("my_key")
   287  
   288  	// then
   289  	require.NoError(t, err)
   290  	assert.True(t, num.NewInt(42).EQ(value))
   291  }
   292  
   293  func testDataGetDecimalSucceeds(t *testing.T) {
   294  	// given
   295  	data := common.Data{
   296  		Signers: []*common.Signer{
   297  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   298  		},
   299  		Data: map[string]string{
   300  			"my_key": "1.2",
   301  		},
   302  	}
   303  
   304  	// when
   305  	value, err := data.GetDecimal("my_key")
   306  
   307  	// then
   308  	require.NoError(t, err)
   309  	assert.True(t, num.DecimalFromFloat(1.2).Equal(value))
   310  }
   311  
   312  func testDataGetBooleanSucceeds(t *testing.T) {
   313  	// given
   314  	data := common.Data{
   315  		Signers: []*common.Signer{
   316  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   317  		},
   318  		Data: map[string]string{
   319  			"my_key": "true",
   320  		},
   321  	}
   322  
   323  	// when
   324  	value, err := data.GetBoolean("my_key")
   325  
   326  	// then
   327  	require.NoError(t, err)
   328  	assert.True(t, value)
   329  }
   330  
   331  func testDataGetTimestampSucceeds(t *testing.T) {
   332  	// given
   333  	data := common.Data{
   334  		Signers: []*common.Signer{
   335  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   336  		},
   337  		Data: map[string]string{
   338  			"my_key": "10000000",
   339  		},
   340  	}
   341  
   342  	// when
   343  	value, err := data.GetTimestamp("my_key")
   344  
   345  	// then
   346  	require.NoError(t, err)
   347  	assert.EqualValues(t, 10000000, value)
   348  }
   349  
   350  func testDataGetStringSucceeds(t *testing.T) {
   351  	// given
   352  	data := common.Data{
   353  		Signers: []*common.Signer{
   354  			common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   355  		},
   356  		Data: map[string]string{
   357  			"my_key": "hello",
   358  		},
   359  	}
   360  
   361  	// when
   362  	value, err := data.GetString("my_key")
   363  
   364  	// then
   365  	require.NoError(t, err)
   366  	assert.Equal(t, "hello", value)
   367  }
   368  
   369  func testDataDeterminingOriginSucceeds(t *testing.T) {
   370  	tcs := []struct {
   371  		name                 string
   372  		pubkeys              []*common.Signer
   373  		isFromInternalOracle bool
   374  	}{
   375  		{
   376  			name:                 "considered from internal oracle without public keys",
   377  			pubkeys:              []*common.Signer{},
   378  			isFromInternalOracle: true,
   379  		}, {
   380  			name: "considered from external oracle with public keys",
   381  			pubkeys: []*common.Signer{
   382  				common.CreateSignerFromString("0xDEADBEEF", common.SignerTypePubKey),
   383  			},
   384  			isFromInternalOracle: false,
   385  		},
   386  	}
   387  
   388  	for _, tc := range tcs {
   389  		t.Run(tc.name, func(tt *testing.T) {
   390  			// given
   391  			data := common.Data{
   392  				Signers: tc.pubkeys,
   393  				Data: map[string]string{
   394  					"my_key": "hello",
   395  				},
   396  			}
   397  
   398  			// then
   399  			assert.Equal(tt, tc.isFromInternalOracle, data.FromInternalOracle())
   400  		})
   401  	}
   402  }