mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-22 07:26:50 +00:00
Config: Refactor version packages (#1887)
* Config: Move config versions to separate pacakges * Config: Move version tests to blackbox texts * Config: Protect registerVersion from overflow * Config: Protect against version already registered
This commit is contained in:
29
config/versions/v2/v2.go
Normal file
29
config/versions/v2/v2.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/buger/jsonparser"
|
||||
)
|
||||
|
||||
// Version is an ExchangeVersion to change the name of GDAX to CoinbasePro
|
||||
type Version struct{}
|
||||
|
||||
// Exchanges returns just GDAX and CoinbasePro
|
||||
func (*Version) Exchanges() []string { return []string{"GDAX", "CoinbasePro"} }
|
||||
|
||||
// UpgradeExchange will change the exchange name from GDAX to CoinbasePro
|
||||
func (*Version) UpgradeExchange(_ context.Context, e []byte) ([]byte, error) {
|
||||
if n, err := jsonparser.GetString(e, "name"); err == nil && n == "GDAX" {
|
||||
return jsonparser.Set(e, []byte(`"CoinbasePro"`), "name")
|
||||
}
|
||||
return e, nil
|
||||
}
|
||||
|
||||
// DowngradeExchange will change the exchange name from CoinbasePro to GDAX
|
||||
func (*Version) DowngradeExchange(_ context.Context, e []byte) ([]byte, error) {
|
||||
if n, err := jsonparser.GetString(e, "name"); err == nil && n == "CoinbasePro" {
|
||||
return jsonparser.Set(e, []byte(`"GDAX"`), "name")
|
||||
}
|
||||
return e, nil
|
||||
}
|
||||
38
config/versions/v2/v2_test.go
Normal file
38
config/versions/v2/v2_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package v2_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
v2 "github.com/thrasher-corp/gocryptotrader/config/versions/v2"
|
||||
)
|
||||
|
||||
func TestUpgradeExchange(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, tt := range [][]string{
|
||||
{"GDAX", "CoinbasePro"},
|
||||
{"Kraken", "Kraken"},
|
||||
{"CoinbasePro", "CoinbasePro"},
|
||||
} {
|
||||
out, err := new(v2.Version).UpgradeExchange(context.Background(), []byte(`{"name":"`+tt[0]+`"}`))
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, out)
|
||||
assert.Equalf(t, `{"name":"`+tt[1]+`"}`, string(out), "Test exchange name %s", tt[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestDowngradeExchange(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, tt := range [][]string{
|
||||
{"GDAX", "GDAX"},
|
||||
{"Kraken", "Kraken"},
|
||||
{"CoinbasePro", "GDAX"},
|
||||
} {
|
||||
out, err := new(v2.Version).DowngradeExchange(context.Background(), []byte(`{"name":"`+tt[0]+`"}`))
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, out)
|
||||
assert.Equalf(t, `{"name":"`+tt[1]+`"}`, string(out), "Test exchange name %s", tt[0])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user