From c610fc3e71e1142c074ab615b18e8ae7cab7891f Mon Sep 17 00:00:00 2001 From: Kirill Sysoev Date: Sat, 17 Aug 2024 19:03:42 +0800 Subject: [PATCH] Address linter complains --- api.go | 2 +- custom_subscription_calls.go | 4 + subscriptions_test.go | 202 +++++++++-------------------------- 3 files changed, 57 insertions(+), 151 deletions(-) diff --git a/api.go b/api.go index 15fbf6a..fb66544 100644 --- a/api.go +++ b/api.go @@ -23,7 +23,7 @@ const ( ) // DerivAPI is the main struct for the DerivAPI client. -type DerivAPI struct { +type DerivAPI struct { //nolint:revive // don't want to change the name for now reqChan chan APIReqest Endpoint *url.URL keepAliveOnDisconnect chan bool diff --git a/custom_subscription_calls.go b/custom_subscription_calls.go index fd25b39..019ed9d 100644 --- a/custom_subscription_calls.go +++ b/custom_subscription_calls.go @@ -3,6 +3,8 @@ package deriv import "github.com/ksysoev/deriv-api/schema" // SubscribeTicksHistory Get historic tick data for a given symbol. +// +//nolint:gocritic // don't want to break backward compatibility for now func (api *DerivAPI) SubscribeTicksHistory(r schema.TicksHistory) (rsp schema.TicksHistoryResp, s *Subsciption[schema.TicksHistoryResp, schema.TicksResp], err error) { var f schema.TicksHistorySubscribe = 1 @@ -17,6 +19,8 @@ func (api *DerivAPI) SubscribeTicksHistory(r schema.TicksHistory) (rsp schema.Ti } // SubscribeTicksHistory Get historic candles data for a given symbol. +// +//nolint:gocritic // don't want to break backward compatibility for now func (api *DerivAPI) SubscribeCandlesHistory(r schema.TicksHistory) (rsp schema.TicksHistoryResp, s *Subsciption[schema.TicksHistoryResp, schema.TicksHistoryResp], err error) { var f schema.TicksHistorySubscribe = 1 diff --git a/subscriptions_test.go b/subscriptions_test.go index 0027806..81300ec 100644 --- a/subscriptions_test.go +++ b/subscriptions_test.go @@ -383,171 +383,72 @@ func TestForgetFailed(t *testing.T) { } } -func TestStartAPIError(t *testing.T) { - testResp := `{ - "echo_req": { - "subscribe": 1, - "ticks": "R_50" +func TestStartInvalidResponses(t *testing.T) { + tests := []struct{ name, resp string }{ + { + name: "InvalidResponse", + resp: `{ + "req_id": 1, + "msg_type": "tick", + "subscription": { + "id": "9ed45a5e-8f87-c735-2b63-36108719eadd" + }, + "tick": 1 + }`, + }, + { + name: "APIError", + resp: `{ + "echo_req": { + "subscribe": 1, + "ticks": "R_50" + }, + "req_id": 1, + "msg_type": "tick", + "error": { + "code": "WrongRequest", + "message": "Invalid request" + } + }`, }, - "req_id": 1, - "msg_type": "tick", - "error": { - "code": "WrongRequest", - "message": "Invalid request" - } - }` - - server := newMockWSServer( - onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) { - if err := ws.Write(context.Background(), websocket.MessageText, []byte(testResp)); err != nil { - t.Errorf("Unexpected error: %v", err) - } - - time.Sleep(time.Second) // to keep the connection open - })) - - defer server.Close() - - url := "ws://" + server.Listener.Addr().String() - api, _ := NewDerivAPI(url, 123, "en", "http://example.com") - - if err := api.Connect(); err != nil { - t.Errorf("Unexpected error: %v", err) - } - - sub := NewSubcription[schema.TicksResp, schema.TicksResp](api) - - if sub == nil { - t.Errorf("Expected a subscription, but got nil") } - reqID := 1 + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + server := newMockWSServer( + onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) { + if err := ws.Write(context.Background(), websocket.MessageText, []byte(test.resp)); err != nil { + t.Errorf("Unexpected error: %v", err) + } - var f schema.TicksSubscribe = 1 + time.Sleep(time.Second) // to keep the connection open + })) - req := schema.Ticks{Ticks: "R50", Subscribe: &f, ReqId: &reqID} + defer server.Close() - if _, err := sub.Start(reqID, req); err == nil { - t.Errorf("Expected an error, but got nil") - } -} + url := "ws://" + server.Listener.Addr().String() + api, _ := NewDerivAPI(url, 123, "en", "http://example.com") -func TestStartInvalidResponse(t *testing.T) { - testResp := `{ - "req_id": 1, - "msg_type": "tick", - "subscription": { - "id": "9ed45a5e-8f87-c735-2b63-36108719eadd" - }, - "tick": 1 - }` - server := newMockWSServer( - onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) { - if err := ws.Write(context.Background(), websocket.MessageText, []byte(testResp)); err != nil { + if err := api.Connect(); err != nil { t.Errorf("Unexpected error: %v", err) } - time.Sleep(time.Second) // to keep the connection open - })) - - defer server.Close() - - url := "ws://" + server.Listener.Addr().String() - api, _ := NewDerivAPI(url, 123, "en", "http://example.com") - - if err := api.Connect(); err != nil { - t.Errorf("Unexpected error: %v", err) - } - - sub := NewSubcription[schema.TicksResp, schema.TicksResp](api) - - if sub == nil { - t.Errorf("Expected a subscription, but got nil") - } - - reqID := 1 - - var f schema.TicksSubscribe = 1 - - req := schema.Ticks{Ticks: "R50", Subscribe: &f, ReqId: &reqID} - - if _, err := sub.Start(reqID, req); err == nil { - t.Errorf("Expected an error, but got nil") - } -} - -func TestStartInvalidResponseInSubscription(t *testing.T) { - responses := []string{`{ - "echo_req": { - "subscribe": 1, - "ticks": "R_50" - }, - "req_id": 1, - "msg_type": "tick", - "subscription": { - "id": "9ed45a5e-8f87-c735-2b63-36108719eadd" - }, - "tick": { - "ask": 186.9688, - "bid": 186.9488, - "epoch": 1679722832, - "id": "9ed45a5e-8f87-c735-2b63-36108719eadd", - "pip_size": 4, - "quote": 186.9588, - "symbol": "R_50" - } - }`, - `{ "req_id": 1 }`} + sub := NewSubcription[schema.TicksResp, schema.TicksResp](api) - server := newMockWSServer( - onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) { - for _, resp := range responses { - if err := ws.Write(context.Background(), websocket.MessageText, []byte(resp)); err != nil { - t.Errorf("Unexpected error: %v", err) - } + if sub == nil { + t.Errorf("Expected a subscription, but got nil") } - time.Sleep(time.Second) // to keep the connection open - })) - - defer server.Close() - - url := "ws://" + server.Listener.Addr().String() - api, _ := NewDerivAPI(url, 123, "en", "http://example.com") - - if err := api.Connect(); err != nil { - t.Errorf("Unexpected error: %v", err) - } - - sub := NewSubcription[schema.TicksResp, schema.TicksResp](api) - - if sub == nil { - t.Errorf("Expected a subscription, but got nil") - } - - reqID := 1 - - var f schema.TicksSubscribe = 1 - - req := schema.Ticks{Ticks: "R50", Subscribe: &f, ReqId: &reqID} - initResp, err := sub.Start(reqID, req) + reqID := 1 - if err != nil { - t.Errorf("Expected no error, but got %v", err) - } + var f schema.TicksSubscribe = 1 - // First message - if *initResp.Tick.Quote != 186.9588 { - t.Errorf("Expected message to be %v, but got %v", 186.9588, *initResp.Tick.Quote) - } + req := schema.Ticks{Ticks: "R50", Subscribe: &f, ReqId: &reqID} - // Second message - select { - case tick, ok := <-sub.GetStream(): - if ok { - t.Errorf("Expected to get noting, but got response: %v", tick) - } - case <-time.After(time.Millisecond): + if _, err := sub.Start(reqID, req); err == nil { + t.Errorf("Expected an error, but got nil") + } + }) } } @@ -583,7 +484,8 @@ func TestStartAPIErrorInSubscription(t *testing.T) { "code": "InvalidSymbol", "message": "Invalid symbol" } - }`} + }`, + `{ "req_id": 1 }`} server := newMockWSServer( onMessageHanler(func(ws *websocket.Conn, _ websocket.MessageType, _ []byte) {