Files
gocryptotrader/exchanges/mock/common_test.go
Samuael A. fc0f262c42 exchanges: Limit mock test JSON data size by truncating slices and maps (#1968)
* set limiter to first level mock data list and updated unit tests

* address nested slices length limit

* minor fix recording file and update unit tests

* minor updates on unit tests

* re-record mock files and minor fix on the unit tests ti adapt the mock data change

* improve http recording limit value and fix issues with mock data in binance

* added MockDataSliceLimit in request items and resolve minor unit test issues

* resolve missed conflict

* rename mock variables, resolve unit test issues, and other updates

* minor fix to CheckJSON and update unit tests

* minor unit test fix

* further optimization on mock CheckJSON method, unit tests, and re-record poloniex

* common and recording unit tests fix

* minor linter issues fix

* unit tests format fix

* fix miscellaneous error

* unit tests fix and minor docs update

* re-record and reduce mock file size

* indentation fix

* minor assertion test fix

* reverted log.Printf line in live testing

* rename variables

* update NewVCRServer unit test

* replace string comparison with *net.OpError check

* restructur net error test

* exchanges/mock: Remove redundant error assertion message in TestNewVCRServer

---------

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
2025-08-26 10:27:07 +10:00

90 lines
2.3 KiB
Go

package mock
import (
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
)
func TestMatchURLVals(t *testing.T) {
t.Parallel()
testVal, testVal2, testVal3, emptyVal := url.Values{}, url.Values{}, url.Values{}, url.Values{}
testVal.Add("test", "test")
testVal2.Add("test2", "test2")
testVal3.Add("test", "diferentValString")
nonceVal1, nonceVal2 := url.Values{}, url.Values{}
nonceVal1.Add("nonce", "012349723587")
nonceVal2.Add("nonce", "9327373874")
tests := []struct {
a url.Values
b url.Values
exp bool
}{
{testVal, emptyVal, false},
{emptyVal, testVal, false},
{testVal, testVal2, false},
{testVal2, testVal, false},
{testVal, testVal3, false},
{nonceVal1, testVal2, false},
{emptyVal, emptyVal, true},
{testVal, testVal, true},
{nonceVal1, nonceVal2, true},
}
for _, tc := range tests {
got := MatchURLVals(tc.a, tc.b)
assert.Equalf(t, tc.exp, got, "MatchURLVals should return correctly for (%q, %q)", tc.a, tc.b)
}
}
func TestDeriveURLValsFromJSON(t *testing.T) {
type class struct {
Numbers []int `json:"numbers"`
Number float64 `json:"number"`
SomeString string `json:"somestring"`
}
test1 := struct {
Things []string `json:"things"`
Data class `json:"data"`
}{
Things: []string{"hello", "world"},
Data: class{
Numbers: []int{1, 3, 3, 7},
Number: 3.14,
SomeString: "hello, peoples",
},
}
payload, err := json.Marshal(test1)
require.NoError(t, err, "Marshal must not error")
values, err := DeriveURLValsFromJSONMap(payload)
assert.NoError(t, err, "DeriveURLValsFromJSONMap should not error")
assert.Len(t, values, 2)
test2 := map[string]string{
"val": "1",
"val2": "2",
"val3": "3",
"val4": "4",
"val5": "5",
"val6": "6",
"val7": "7",
}
payload, err = json.Marshal(test2)
require.NoError(t, err, "Marshal must not error")
values, err = DeriveURLValsFromJSONMap(payload)
require.NoError(t, err, "DeriveURLValsFromJSONMap must not error")
require.Equal(t, 7, len(values), "DeriveURLValsFromJSONMap must return the correct number of values")
for key, val := range values {
require.Len(t, val, 1)
assert.Equalf(t, test2[key], val[0], "DeriveURLValsFromJSON should return the correct value for %s", key)
}
}