From 608eb71d32c93003580bf168379cfc198ae171d9 Mon Sep 17 00:00:00 2001 From: YashK Date: Wed, 12 Jun 2024 19:14:44 +0530 Subject: [PATCH] feat: added new tests for new json array and negative switch case scenarios --- utils/api_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++ utils/asset_test.go | 24 ++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/utils/api_test.go b/utils/api_test.go index 8808ec050..d4e0c5e9f 100644 --- a/utils/api_test.go +++ b/utils/api_test.go @@ -2,6 +2,8 @@ package utils import ( "encoding/hex" + "encoding/json" + "github.com/stretchr/testify/assert" "razor/cache" "razor/core/types" "reflect" @@ -179,6 +181,57 @@ func TestGetDataFromAPI(t *testing.T) { } } +func TestParseJSONData(t *testing.T) { + tests := []struct { + name string + input string + selector string + expected interface{} + expectedErr string + }{ + { + name: "JSON Object", + input: `{"key1": "value1", "key2": "value2"}`, + selector: "key1", + expected: "value1", + }, + { + name: "Array of JSON Objects", + input: `[{"key1": "value1", "key2": "value2"}, {"key1": "value3", "key2": "value4"}]`, + selector: "key2", + expected: "value2", + }, + { + name: "Empty JSON Array", + input: `[]`, + selector: "key1", + expectedErr: "empty JSON array", + }, + { + name: "Unexpected JSON Structure", + input: `"unexpected structure"`, + selector: "key1", + expectedErr: "unexpected JSON structure", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var parsedJSON interface{} + err := json.Unmarshal([]byte(tt.input), &parsedJSON) + assert.NoError(t, err) + + result, err := parseJSONData(parsedJSON, tt.selector) + if tt.expectedErr != "" { + assert.EqualError(t, err, tt.expectedErr) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.expected, result) + } + }) + } +} + func TestGetDataFromJSON(t *testing.T) { type args struct { jsonObject map[string]interface{} diff --git a/utils/asset_test.go b/utils/asset_test.go index 4df204b48..7651bd8c8 100644 --- a/utils/asset_test.go +++ b/utils/asset_test.go @@ -662,6 +662,16 @@ func TestGetDataToCommitFromJob(t *testing.T) { Url: `{"type": "POST","url": "https://rpc.ankr.com/eth","body": {"jsonrpc":"2.0","id":7269270904970082,"method":"eth_call","params":[{"from":"0x0000000000000000000000000000000000000000","data":"0xd06ca61f0000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000050de6856358cc35f3a9a57eaaa34bd4cb707d2cd0000000000000000000000008e870d67f660d95d5be530380d0ec0bd388289e1","to":"0x7a250d5630b4cf539739df2c5dacb4c659f2488d"},"latest"]},"header": {"content-type": "application/json"}, "returnType": "hexArray[1]"}`, } + arrayOfObjectsJob := bindings.StructsJob{Id: 1, SelectorType: 0, Weight: 100, + Power: 2, Name: "ethusd_bitfinex", Selector: "last_price", + Url: "https://api.bitfinex.com/v1/pubticker/ethusd", + } + + arrayOfArraysJob := bindings.StructsJob{Id: 1, SelectorType: 0, Weight: 100, + Power: 2, Name: "ethusd_bitfinex_v2", Selector: "last_price", + Url: "https://api-pub.bitfinex.com/v2/tickers?symbols=tXDCUSD", + } + invalidDataSourceStructJob := bindings.StructsJob{Id: 1, SelectorType: 0, Weight: 100, Power: 2, Name: "ethusd_sample", Selector: "result", Url: `{"type": true,"url1": {}}`, @@ -727,6 +737,20 @@ func TestGetDataToCommitFromJob(t *testing.T) { want: nil, wantErr: false, }, + { + name: "Test 7: When GetDataToCommitFromJob() executes successfully for job returning response of type array of objects", + args: args{ + job: arrayOfObjectsJob, + }, + wantErr: false, + }, + { + name: "Test 8: When GetDataToCommitFromJob() fails for job returning response of type arrays of arrays as element in array is not a json object", + args: args{ + job: arrayOfArraysJob, + }, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {