mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-07 15:11:03 +00:00
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>
This commit is contained in:
@@ -4,10 +4,13 @@ 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")
|
||||
@@ -17,87 +20,39 @@ func TestMatchURLVals(t *testing.T) {
|
||||
nonceVal1.Add("nonce", "012349723587")
|
||||
nonceVal2.Add("nonce", "9327373874")
|
||||
|
||||
expected := false
|
||||
received := MatchURLVals(testVal, emptyVal)
|
||||
if received != expected {
|
||||
t.Errorf("MatchURLVals error expected %v received %v",
|
||||
expected,
|
||||
received)
|
||||
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},
|
||||
}
|
||||
|
||||
received = MatchURLVals(emptyVal, testVal)
|
||||
if received != expected {
|
||||
t.Errorf("MatchURLVals error expected %v received %v",
|
||||
expected,
|
||||
received)
|
||||
}
|
||||
|
||||
received = MatchURLVals(testVal, testVal2)
|
||||
if received != expected {
|
||||
t.Errorf("MatchURLVals error expected %v received %v",
|
||||
expected,
|
||||
received)
|
||||
}
|
||||
|
||||
received = MatchURLVals(testVal2, testVal)
|
||||
if received != expected {
|
||||
t.Errorf("MatchURLVals error expected %v received %v",
|
||||
expected,
|
||||
received)
|
||||
}
|
||||
|
||||
received = MatchURLVals(testVal, testVal3)
|
||||
if received != expected {
|
||||
t.Errorf("MatchURLVals error expected %v received %v",
|
||||
expected,
|
||||
received)
|
||||
}
|
||||
|
||||
received = MatchURLVals(nonceVal1, testVal2)
|
||||
if received != expected {
|
||||
t.Errorf("MatchURLVals error expected %v received %v",
|
||||
expected,
|
||||
received)
|
||||
}
|
||||
|
||||
expected = true
|
||||
received = MatchURLVals(emptyVal, emptyVal)
|
||||
if received != expected {
|
||||
t.Errorf("MatchURLVals error expected %v received %v",
|
||||
expected,
|
||||
received)
|
||||
}
|
||||
|
||||
received = MatchURLVals(testVal, testVal)
|
||||
if received != expected {
|
||||
t.Errorf("MatchURLVals error expected %v received %v",
|
||||
expected,
|
||||
received)
|
||||
}
|
||||
|
||||
received = MatchURLVals(nonceVal1, nonceVal2)
|
||||
if received != expected {
|
||||
t.Errorf("MatchURLVals error expected %v received %v",
|
||||
expected,
|
||||
received)
|
||||
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 struct {
|
||||
Numbers []int `json:"numbers"`
|
||||
Number float64 `json:"number"`
|
||||
SomeString string `json:"somestring"`
|
||||
} `json:"data"`
|
||||
Data class `json:"data"`
|
||||
}{
|
||||
Things: []string{"hello", "world"},
|
||||
Data: struct {
|
||||
Numbers []int `json:"numbers"`
|
||||
Number float64 `json:"number"`
|
||||
SomeString string `json:"somestring"`
|
||||
}{
|
||||
Data: class{
|
||||
Numbers: []int{1, 3, 3, 7},
|
||||
Number: 3.14,
|
||||
SomeString: "hello, peoples",
|
||||
@@ -105,14 +60,11 @@ func TestDeriveURLValsFromJSON(t *testing.T) {
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(test1)
|
||||
if err != nil {
|
||||
t.Error("marshal error", err)
|
||||
}
|
||||
require.NoError(t, err, "Marshal must not error")
|
||||
|
||||
_, err = DeriveURLValsFromJSONMap(payload)
|
||||
if err != nil {
|
||||
t.Error("DeriveURLValsFromJSON error", err)
|
||||
}
|
||||
values, err := DeriveURLValsFromJSONMap(payload)
|
||||
assert.NoError(t, err, "DeriveURLValsFromJSONMap should not error")
|
||||
assert.Len(t, values, 2)
|
||||
|
||||
test2 := map[string]string{
|
||||
"val": "1",
|
||||
@@ -125,17 +77,13 @@ func TestDeriveURLValsFromJSON(t *testing.T) {
|
||||
}
|
||||
|
||||
payload, err = json.Marshal(test2)
|
||||
if err != nil {
|
||||
t.Error("marshal error", err)
|
||||
}
|
||||
require.NoError(t, err, "Marshal must not error")
|
||||
|
||||
vals, err := DeriveURLValsFromJSONMap(payload)
|
||||
if err != nil {
|
||||
t.Error("DeriveURLValsFromJSON error", err)
|
||||
}
|
||||
|
||||
if vals["val"][0] != "1" {
|
||||
t.Error("DeriveURLValsFromJSON unexpected value",
|
||||
vals["val"][0])
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user