Skip to content

Commit

Permalink
Add tests to all Klines APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
marianogappa committed Aug 8, 2021
1 parent 678e3c3 commit 7548eb8
Show file tree
Hide file tree
Showing 4 changed files with 446 additions and 0 deletions.
111 changes: 111 additions & 0 deletions binance/api_klines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package binance
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/marianogappa/signal-checker/common"
Expand Down Expand Up @@ -402,6 +404,115 @@ func TestUnhappyToCandlesticks(t *testing.T) {
}
}

func TestKlinesInvalidUrl(t *testing.T) {
i := 0
replies := []string{
`[
[
1499040000000,
"0.01634790",
"0.80000000",
"0.01575800",
"0.01577100",
"148976.11427815",
1499644799999,
"2434.19055334",
308,
"1756.87402397",
"28.46694368",
"17928899.62484339"
]
]`,
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, replies[i%len(replies)])
i++
}))
defer ts.Close()

b := NewBinance()
b.overrideAPIURL("invalid url")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to invalid url")
}
}

func TestKlinesErrReadingResponseBody(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "1")
}))
defer ts.Close()

b := NewBinance()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to invalid response body")
}
}

func TestKlinesErrorResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `{"code":-1100,"msg":"Illegal characters found in parameter 'symbol'; legal range is '^[A-Z0-9-_.]{1,20}$'."}`)
}))
defer ts.Close()

b := NewBinance()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to error response")
}
}
func TestKlinesInvalidJSONResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `invalid json`)
}))
defer ts.Close()

b := NewBinance()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to invalid json")
}
}

func TestKlinesInvalidFloatsInJSONResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `[
[
1499040000000,
"invalid",
"0.80000000",
"0.01575800",
"0.01577100",
"148976.11427815",
1499644799999,
"2434.19055334",
308,
"1756.87402397",
"28.46694368",
"17928899.62484339"
]
]`)
}))
defer ts.Close()

b := NewBinance()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to invalid floats in json")
}
}

func f(fl float64) common.JsonFloat64 {
return common.JsonFloat64(fl)
}
111 changes: 111 additions & 0 deletions binanceusdmfutures/api_klines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package binanceusdmfutures
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/marianogappa/signal-checker/common"
Expand Down Expand Up @@ -402,6 +404,115 @@ func TestUnhappyToCandlesticks(t *testing.T) {
}
}

func TestKlinesInvalidUrl(t *testing.T) {
i := 0
replies := []string{
`[
[
1499040000000,
"0.01634790",
"0.80000000",
"0.01575800",
"0.01577100",
"148976.11427815",
1499644799999,
"2434.19055334",
308,
"1756.87402397",
"28.46694368",
"17928899.62484339"
]
]`,
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, replies[i%len(replies)])
i++
}))
defer ts.Close()

b := NewBinanceUSDMFutures()
b.overrideAPIURL("invalid url")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to invalid url")
}
}

func TestKlinesErrReadingResponseBody(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "1")
}))
defer ts.Close()

b := NewBinanceUSDMFutures()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to invalid response body")
}
}

func TestKlinesErrorResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `{"code":-1100,"msg":"Illegal characters found in parameter 'symbol'; legal range is '^[A-Z0-9-_.]{1,20}$'."}`)
}))
defer ts.Close()

b := NewBinanceUSDMFutures()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to error response")
}
}
func TestKlinesInvalidJSONResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `invalid json`)
}))
defer ts.Close()

b := NewBinanceUSDMFutures()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to invalid json")
}
}

func TestKlinesInvalidFloatsInJSONResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `[
[
1499040000000,
"invalid",
"0.80000000",
"0.01575800",
"0.01577100",
"148976.11427815",
1499644799999,
"2434.19055334",
308,
"1756.87402397",
"28.46694368",
"17928899.62484339"
]
]`)
}))
defer ts.Close()

b := NewBinanceUSDMFutures()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to invalid floats in json")
}
}

func f(fl float64) common.JsonFloat64 {
return common.JsonFloat64(fl)
}
97 changes: 97 additions & 0 deletions coinbase/api_klines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package coinbase
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/marianogappa/signal-checker/common"
Expand Down Expand Up @@ -64,6 +66,101 @@ func TestUnhappyToCandlesticks(t *testing.T) {
}
}

func TestKlinesInvalidUrl(t *testing.T) {
i := 0
replies := []string{
`[[1626868560,31540.72,31584.3,31540.72,31576.13,0.08432516]]`,
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, replies[i%len(replies)])
i++
}))
defer ts.Close()

b := NewCoinbase()
b.overrideAPIURL("invalid url")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to invalid url")
}
}

func TestKlinesErrReadingResponseBody(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "1")
}))
defer ts.Close()

b := NewCoinbase()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to invalid response body")
}
}

func TestKlinesErrorResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `{"message":"error!"}`)
}))
defer ts.Close()

b := NewCoinbase()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to error response")
}
}

func TestKlinesNon200Response(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
}))
defer ts.Close()

b := NewCoinbase()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to 500 response")
}
}

func TestKlinesInvalidJSONResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `invalid json`)
}))
defer ts.Close()

b := NewCoinbase()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to invalid json")
}
}

func TestKlinesInvalidFloatsInJSONResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `[["1626868560",31540.72,31584.3,31540.72,31576.13,0.08432516]]`)
}))
defer ts.Close()

b := NewCoinbase()
b.overrideAPIURL(ts.URL + "/")
ci := b.BuildCandlestickIterator("BTC", "USDT", "2021-07-04T14:14:18+00:00")
_, err := ci.Next()
if err == nil {
t.Fatalf("should have failed due to invalid floats in json")
}
}

func f(fl float64) common.JsonFloat64 {
return common.JsonFloat64(fl)
}
Loading

0 comments on commit 7548eb8

Please sign in to comment.