From 416fbbd5ae3c4365b60d7bf15c4a0a75953917cd Mon Sep 17 00:00:00 2001 From: Shogin Michael Date: Thu, 30 May 2019 05:35:22 +0200 Subject: [PATCH] Unit tests for communication/base package (#312) * Added tests * Added tests communications base * Removed unnecessary field * Review corrections: linter * Review corrections: typo --- communications/base/base_interface.go | 1 + communications/base/base_test.go | 147 +++++++++++++++++++++++++- 2 files changed, 144 insertions(+), 4 deletions(-) diff --git a/communications/base/base_interface.go b/communications/base/base_interface.go index ae8f0ab1..1ed3633d 100644 --- a/communications/base/base_interface.go +++ b/communications/base/base_interface.go @@ -54,6 +54,7 @@ func (c IComm) PushEvent(event Event) { // GetEnabledCommunicationMediums prints out enabled and connected communication // packages +// (#debug output only) func (c IComm) GetEnabledCommunicationMediums() { var count int for i := range c { diff --git a/communications/base/base_test.go b/communications/base/base_test.go index e8133f47..3dc5d0e7 100644 --- a/communications/base/base_test.go +++ b/communications/base/base_test.go @@ -2,6 +2,9 @@ package base import ( "testing" + + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" + "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) var ( @@ -71,14 +74,150 @@ func TestGetStatus(t *testing.T) { } } +type CommunicationProvider struct { + ICommunicate + + isEnabled bool + isConnected bool + ConnectCalled bool + PushEventCalled bool +} + +func (p *CommunicationProvider) IsEnabled() bool { + return p.isEnabled +} + +func (p *CommunicationProvider) IsConnected() bool { + return p.isConnected +} + +func (p *CommunicationProvider) Connect() error { + p.ConnectCalled = true + return nil +} + +func (p *CommunicationProvider) PushEvent(e Event) error { + p.PushEventCalled = true + return nil +} + +func (p *CommunicationProvider) GetName() string { + return "someTestProvider" +} + func TestSetup(t *testing.T) { - i.Setup() + var ic IComm + testConfigs := []struct { + isEnabled bool + isConnected bool + shouldConnectCalled bool + provider ICommunicate + }{ + {false, true, false, nil}, + {false, false, false, nil}, + {true, true, false, nil}, + {true, false, true, nil}, + } + for _, config := range testConfigs { + config.provider = &CommunicationProvider{ + isEnabled: config.isEnabled, + isConnected: config.isConnected} + ic = append(ic, config.provider) + } + + ic.Setup() + + for idx, provider := range ic { + exp := testConfigs[idx].shouldConnectCalled + act := provider.(*CommunicationProvider).ConnectCalled + if exp != act { + t.Fatalf("provider should be enabled and not be connected: exp=%v, act=%v", exp, act) + } + } } func TestPushEvent(t *testing.T) { - i.PushEvent(Event{}) + var ic IComm + testConfigs := []struct { + Enabled bool + Connected bool + PushEventCalled bool + provider ICommunicate + }{ + {false, true, false, nil}, + {false, false, false, nil}, + {true, false, false, nil}, + {true, true, true, nil}, + } + for _, config := range testConfigs { + config.provider = &CommunicationProvider{ + isEnabled: config.Enabled, + isConnected: config.Connected} + ic = append(ic, config.provider) + } + + ic.PushEvent(Event{}) + + for idx, provider := range ic { + exp := testConfigs[idx].PushEventCalled + act := provider.(*CommunicationProvider).PushEventCalled + if exp != act { + t.Fatalf("provider should be enabled and connected: exp=%v, act=%v", exp, act) + } + } } -func TestGetEnabledCommunicationMediums(t *testing.T) { - i.GetEnabledCommunicationMediums() +func TestStageTickerData(t *testing.T) { + _, ok := TickerStaged["bitstamp"]["someAsset"]["BTCUSD"] + if ok { + t.Fatalf("key should not exists") + } + + price := ticker.Price{} + var i IComm + i.Setup() + + i.StageTickerData("bitstamp", "someAsset", &price) + + _, ok = TickerStaged["bitstamp"]["someAsset"][price.Pair.String()] + if !ok { + t.Fatalf("key should exists") + } +} + +func TestOrderbookData(t *testing.T) { + _, ok := OrderbookStaged["bitstamp"]["someAsset"]["someOrderbook"] + if ok { + t.Fatal("key should not exists") + } + + ob := orderbook.Base{ + Asks: []orderbook.Item{ + {Amount: 1, Price: 2, ID: 3}, + {Amount: 4, Price: 5, ID: 6}, + }, + } + var i IComm + i.Setup() + + i.StageOrderbookData("bitstamp", "someAsset", &ob) + + orderbook, ok := OrderbookStaged["bitstamp"]["someAsset"][ob.Pair.String()] + if !ok { + t.Fatal("key should exists") + } + + if ob.Pair.String() != orderbook.CurrencyPair { + t.Fatal("currency missmatched") + } + + _, totalAsks := ob.TotalAsksAmount() + if totalAsks != orderbook.TotalAsks { + t.Fatal("total asks missmatched") + } + + _, totalBids := ob.TotalBidsAmount() + if totalBids != orderbook.TotalBids { + t.Fatal("total bids missmatched") + } }