Skip to content

Commit

Permalink
Merge pull request #11 from itsksaurabh/unit/testing
Browse files Browse the repository at this point in the history
Unit tests with test data
  • Loading branch information
itsksaurabh authored Mar 14, 2020
2 parents 34f35b7 + 036f1e8 commit 249dc5f
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 16 deletions.
5 changes: 1 addition & 4 deletions marketDepth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package wazirx_test
import (
"context"
"testing"

wazirx "github.com/itsksaurabh/go-wazirx"
)

func TestMarketDepth(t *testing.T) {
c := wazirx.Client{}
market := "btcusdt"
_, err := c.MarketDepth(context.Background(), market)
_, err := client(t).MarketDepth(context.Background(), market)
if err != nil {
if err != nil {
t.Fatal(err)
Expand Down
5 changes: 1 addition & 4 deletions marketStatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package wazirx_test
import (
"context"
"testing"

wazirx "github.com/itsksaurabh/go-wazirx"
)

func TestMarketStatus(t *testing.T) {
c := wazirx.Client{}
_, err := c.MarketStatus(context.Background())
_, err := client(t).MarketStatus(context.Background())
if err != nil {
if err != nil {
t.Fatal(err)
Expand Down
5 changes: 1 addition & 4 deletions marketTicker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package wazirx_test
import (
"context"
"testing"

wazirx "github.com/itsksaurabh/go-wazirx"
)

func TestMarketTicker(t *testing.T) {
c := wazirx.Client{}
_, err := c.MarketTicker(context.Background())
_, err := client(t).MarketTicker(context.Background())
if err != nil {
if err != nil {
t.Fatal(err)
Expand Down
5 changes: 1 addition & 4 deletions marketTrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package wazirx_test
import (
"context"
"testing"

wazirx "github.com/itsksaurabh/go-wazirx"
)

func TestMarketTrade(t *testing.T) {
c := wazirx.Client{}
market := "btcusdt"
_, err := c.MarketTrade(context.Background(), market)
_, err := client(t).MarketTrade(context.Background(), market)
if err != nil {
if err != nil {
t.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions testdata/MarketDepth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"timestamp":1584182448,"asks":[["5369.0","0.1772"],["5377.0","1.1558"],["5382.0","1.1871"],["5500.0","0.7949"],["5519.0","0.1272"],["5530.0","0.0567"],["5555.0","0.1786"],["5588.0","1.46"],["5600.0","0.3385"],["5650.0","0.0212"],["5700.0","0.1609"],["5701.0","0.1"],["5730.0","0.8397"],["5749.0","0.001"],["5750.0","0.0948"],["5800.0","0.1372"]],"bids":[["5349.0","0.0261"],["5340.0","0.0088"],["5327.0","1.2933"],["5325.0","2.3665"],["5310.0","0.057"],["5290.0","0.0568"],["5230.0","0.0604"],["5223.0","0.0091"],["5205.0","0.0019"],["5200.0","0.1316"],["5119.0","0.0023"],["5111.0","0.005"],["5100.0","0.1032"],["5050.0","0.011"],["5001.0","0.6249"],["5000.0","2.177"]]}
1 change: 1 addition & 0 deletions testdata/MarketStatus.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions testdata/MarketTicker.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions testdata/MarketTrade.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":2601969,"price":"5368.0","volume":"0.0224","funds":"120.2432","market":"btcusdt","created_at":"2020-03-14T16:10:33+05:30","side":null},{"id":2601962,"price":"5348.0","volume":"0.0157","funds":"83.9636","market":"btcusdt","created_at":"2020-03-14T16:10:07+05:30","side":null},{"id":2601958,"price":"5358.0","volume":"0.09","funds":"482.22","market":"btcusdt","created_at":"2020-03-14T16:09:23+05:30","side":null},{"id":2601931,"price":"5369.0","volume":"0.0317","funds":"170.1973","market":"btcusdt","created_at":"2020-03-14T16:07:58+05:30","side":null},{"id":2601930,"price":"5369.0","volume":"0.0025","funds":"13.4225","market":"btcusdt","created_at":"2020-03-14T16:07:47+05:30","side":null},{"id":2601924,"price":"5350.0","volume":"0.0067","funds":"35.845","market":"btcusdt","created_at":"2020-03-14T16:07:09+05:30","side":null},{"id":2601923,"price":"5347.0","volume":"0.0095","funds":"50.7965","market":"btcusdt","created_at":"2020-03-14T16:06:56+05:30","side":null},{"id":2601914,"price":"5347.0","volume":"0.0068","funds":"36.3596","market":"btcusdt","created_at":"2020-03-14T16:06:37+05:30","side":null},{"id":2601895,"price":"5358.0","volume":"0.0111","funds":"59.4738","market":"btcusdt","created_at":"2020-03-14T16:05:22+05:30","side":null},{"id":2601874,"price":"5325.0","volume":"0.0024","funds":"12.78","market":"btcusdt","created_at":"2020-03-14T16:02:33+05:30","side":null},{"id":2601858,"price":"5350.0","volume":"0.0366","funds":"195.81","market":"btcusdt","created_at":"2020-03-14T16:01:04+05:30","side":null},{"id":2601857,"price":"5322.0","volume":"0.2442","funds":"1299.6324","market":"btcusdt","created_at":"2020-03-14T16:01:04+05:30","side":null},{"id":2601852,"price":"5322.0","volume":"0.0157","funds":"83.5554","market":"btcusdt","created_at":"2020-03-14T16:00:50+05:30","side":null},{"id":2601843,"price":"5309.0","volume":"0.0014","funds":"7.4326","market":"btcusdt","created_at":"2020-03-14T16:00:40+05:30","side":null},{"id":2601830,"price":"5300.0","volume":"0.2497","funds":"1323.41","market":"btcusdt","created_at":"2020-03-14T16:00:03+05:30","side":null},{"id":2601829,"price":"5300.0","volume":"0.0834","funds":"442.02","market":"btcusdt","created_at":"2020-03-14T16:00:03+05:30","side":null},{"id":2601817,"price":"5300.0","volume":"0.0094","funds":"49.82","market":"btcusdt","created_at":"2020-03-14T15:59:48+05:30","side":null},{"id":2601792,"price":"5300.0","volume":"0.0933","funds":"494.49","market":"btcusdt","created_at":"2020-03-14T15:58:01+05:30","side":null},{"id":2601789,"price":"5278.0","volume":"0.0022","funds":"11.6116","market":"btcusdt","created_at":"2020-03-14T15:57:55+05:30","side":null},{"id":2601766,"price":"5266.0","volume":"0.0035","funds":"18.431","market":"btcusdt","created_at":"2020-03-14T15:56:58+05:30","side":null},{"id":2601765,"price":"5266.0","volume":"0.0182","funds":"95.8412","market":"btcusdt","created_at":"2020-03-14T15:56:58+05:30","side":null},{"id":2601764,"price":"5265.0","volume":"0.0819","funds":"431.2035","market":"btcusdt","created_at":"2020-03-14T15:56:58+05:30","side":null},{"id":2601760,"price":"5265.0","volume":"0.0182","funds":"95.823","market":"btcusdt","created_at":"2020-03-14T15:56:17+05:30","side":null},{"id":2601734,"price":"5242.0","volume":"0.016","funds":"83.872","market":"btcusdt","created_at":"2020-03-14T15:55:05+05:30","side":null},{"id":2601704,"price":"5266.0","volume":"0.0177","funds":"93.2082","market":"btcusdt","created_at":"2020-03-14T15:53:01+05:30","side":null},{"id":2601703,"price":"5266.0","volume":"0.0048","funds":"25.2768","market":"btcusdt","created_at":"2020-03-14T15:53:01+05:30","side":null},{"id":2601702,"price":"5248.0","volume":"0.041","funds":"215.168","market":"btcusdt","created_at":"2020-03-14T15:53:01+05:30","side":null},{"id":2601698,"price":"5248.0","volume":"0.0261","funds":"136.9728","market":"btcusdt","created_at":"2020-03-14T15:52:55+05:30","side":null},{"id":2601662,"price":"5241.0","volume":"0.074","funds":"387.834","market":"btcusdt","created_at":"2020-03-14T15:50:49+05:30","side":null},{"id":2601601,"price":"5243.0","volume":"0.0022","funds":"11.5346","market":"btcusdt","created_at":"2020-03-14T15:46:56+05:30","side":null},{"id":2601590,"price":"5243.0","volume":"0.0022","funds":"11.5346","market":"btcusdt","created_at":"2020-03-14T15:46:19+05:30","side":null},{"id":2601585,"price":"5243.0","volume":"0.0095","funds":"49.8085","market":"btcusdt","created_at":"2020-03-14T15:46:08+05:30","side":null},{"id":2601555,"price":"5243.0","volume":"0.0389","funds":"203.9527","market":"btcusdt","created_at":"2020-03-14T15:43:29+05:30","side":null},{"id":2601488,"price":"5223.0","volume":"0.0092","funds":"48.0516","market":"btcusdt","created_at":"2020-03-14T15:38:02+05:30","side":null},{"id":2601478,"price":"5230.0","volume":"0.4126","funds":"2157.898","market":"btcusdt","created_at":"2020-03-14T15:37:32+05:30","side":null},{"id":2601460,"price":"5222.0","volume":"1.1163","funds":"5829.3186","market":"btcusdt","created_at":"2020-03-14T15:36:18+05:30","side":null},{"id":2601436,"price":"5245.0","volume":"0.0012","funds":"6.294","market":"btcusdt","created_at":"2020-03-14T15:35:13+05:30","side":null},{"id":2601433,"price":"5216.0","volume":"0.02","funds":"104.32","market":"btcusdt","created_at":"2020-03-14T15:35:08+05:30","side":null},{"id":2601412,"price":"5232.0","volume":"0.1","funds":"523.2","market":"btcusdt","created_at":"2020-03-14T15:34:08+05:30","side":null},{"id":2601407,"price":"5260.0","volume":"0.0302","funds":"158.852","market":"btcusdt","created_at":"2020-03-14T15:33:54+05:30","side":null},{"id":2601389,"price":"5231.0","volume":"0.097","funds":"507.407","market":"btcusdt","created_at":"2020-03-14T15:33:08+05:30","side":null},{"id":2601388,"price":"5244.0","volume":"0.003","funds":"15.732","market":"btcusdt","created_at":"2020-03-14T15:33:08+05:30","side":null},{"id":2601364,"price":"5254.0","volume":"0.0763","funds":"400.8802","market":"btcusdt","created_at":"2020-03-14T15:31:58+05:30","side":null},{"id":2601363,"price":"5245.0","volume":"0.0012","funds":"6.294","market":"btcusdt","created_at":"2020-03-14T15:31:58+05:30","side":null},{"id":2601352,"price":"5230.0","volume":"0.2714","funds":"1419.422","market":"btcusdt","created_at":"2020-03-14T15:31:08+05:30","side":null},{"id":2601349,"price":"5245.0","volume":"0.0041","funds":"21.5045","market":"btcusdt","created_at":"2020-03-14T15:30:48+05:30","side":null},{"id":2601347,"price":"5215.0","volume":"0.001","funds":"5.215","market":"btcusdt","created_at":"2020-03-14T15:30:38+05:30","side":null},{"id":2601346,"price":"5214.0","volume":"0.0431","funds":"224.7234","market":"btcusdt","created_at":"2020-03-14T15:30:38+05:30","side":null},{"id":2601337,"price":"5213.0","volume":"0.0041","funds":"21.3733","market":"btcusdt","created_at":"2020-03-14T15:30:12+05:30","side":null},{"id":2601319,"price":"5214.0","volume":"0.0058","funds":"30.2412","market":"btcusdt","created_at":"2020-03-14T15:29:22+05:30","side":null}]
95 changes: 95 additions & 0 deletions wazirx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package wazirx_test

import (
"bytes"
"encoding/json"
"flag"
"io/ioutil"
"net/http"
"strings"
"testing"

wazirx "github.com/itsksaurabh/go-wazirx"
"github.com/pkg/errors"
)

var (
updateTestData = flag.Bool(
"update",
false,
"if set then update testdata else use saved testdata for testing.",
)
)

type roundTripFunc func(r *http.Request) (*http.Response, error)

// RoundTrip implements http.RoundTripper interface.
func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return f(r)
}

// client returns a wazirx.Client mainly for testing purposes.
// It behaves as a reverse proxy agent, it reads testfile and
// returns data as a response to requests made through it.
//
// If update flag is set, it will save the real data to testfile.
func client(t *testing.T) wazirx.Client {
c := wazirx.Client{
HTTP: http.DefaultClient,
}
c.HTTP.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
if flag.Parse(); *updateTestData {
if err := writeTestData(t, r); err != nil {
return nil, errors.Wrap(err, "write testdata failed")
}
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(readTestData(t)),
}, nil
})

return c
}

// returns filename for saving response JSON
func filename(t *testing.T) string {
name := t.Name()
if strings.Contains(name, "/") { // If a subtest
name = name[strings.LastIndex(t.Name(), "/")+1:]
}
name = strings.TrimPrefix(name, "Test")
return name + ".json"
}

// readTestData loads response from testdata files and returns
// a new Reader reading from []bytes
// It creates filename with the help on test name
func readTestData(t *testing.T) *bytes.Reader {
raw, err := ioutil.ReadFile("./testdata/" + filename(t))
if err != nil {
t.Error(err)
}
return bytes.NewReader(raw)
}

// writeTestData writes testdata to files
// It creates filename with the help on test name
func writeTestData(t *testing.T, r *http.Request) error {
resp, err := http.DefaultTransport.RoundTrip(r)
if err != nil {
return errors.Wrap(err, "request failed")
}

// decodes resp.Body into raw
var raw json.RawMessage
if err := json.NewDecoder(resp.Body).Decode(&raw); err != nil {
return errors.Wrap(err, "body decode failed")
}

err = ioutil.WriteFile("./testdata/"+filename(t), raw, 0644)
if err != nil {
return errors.Wrap(err, "write test data failed")
}
return nil
}

0 comments on commit 249dc5f

Please sign in to comment.