Skip to content

Commit

Permalink
Unit tests for communication/base package (thrasher-corp#312)
Browse files Browse the repository at this point in the history
* Added tests

* Added tests communications base

* Removed unnecessary field

* Review corrections: linter

* Review corrections: typo
  • Loading branch information
mshogin authored and thrasher- committed May 30, 2019
1 parent e8bc3b5 commit 416fbbd
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 4 deletions.
1 change: 1 addition & 0 deletions communications/base/base_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
147 changes: 143 additions & 4 deletions communications/base/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package base

import (
"testing"

"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)

var (
Expand Down Expand Up @@ -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 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 TestGetEnabledCommunicationMediums(t *testing.T) {
i.GetEnabledCommunicationMediums()
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")
}
}

0 comments on commit 416fbbd

Please sign in to comment.