mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-05 23:16:53 +00:00
exchanges/fill: Add test coverage (#1223)
* Fill go test * Add return error into Update function * Update exchanges/fill/fill_test.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * GateIO: Fix websocket trade tests * GateIO: More test coverage * GateIO: Options tests * GateIO: Fix woopsies * Address nits * GateIO: Fix nitters --------- Co-authored-by: Scott <gloriousCode@users.noreply.github.com> Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
package fill
|
||||
|
||||
import "errors"
|
||||
|
||||
// ErrFeedDisabled is an error that indicates the fill feed is disabled
|
||||
var ErrFeedDisabled = errors.New("fill feed disabled")
|
||||
|
||||
// Setup sets up the fill processor
|
||||
func (f *Fills) Setup(fillsFeedEnabled bool, c chan interface{}) {
|
||||
f.dataHandler = c
|
||||
@@ -14,9 +19,11 @@ func (f *Fills) Update(data ...Data) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if f.fillsFeedEnabled {
|
||||
f.dataHandler <- data
|
||||
if !f.fillsFeedEnabled {
|
||||
return ErrFeedDisabled
|
||||
}
|
||||
|
||||
f.dataHandler <- data
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
106
exchanges/fill/fill_test.go
Normal file
106
exchanges/fill/fill_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package fill
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestSetup tests the setup function of the Fills struct
|
||||
func TestSetup(t *testing.T) {
|
||||
fill := &Fills{}
|
||||
channel := make(chan interface{})
|
||||
fill.Setup(true, channel)
|
||||
|
||||
if fill.dataHandler == nil {
|
||||
t.Error("expected dataHandler to be set")
|
||||
}
|
||||
|
||||
if !fill.fillsFeedEnabled {
|
||||
t.Error("expected fillsFeedEnabled to be true")
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateDisabledFeed tests the Update function when fillsFeedEnabled is false
|
||||
func TestUpdateDisabledFeed(t *testing.T) {
|
||||
channel := make(chan interface{}, 1)
|
||||
fill := Fills{dataHandler: channel, fillsFeedEnabled: false}
|
||||
|
||||
// Send a test data to the Update function
|
||||
testData := Data{Timestamp: time.Now(), Price: 15.2, Amount: 3.2}
|
||||
if err := fill.Update(testData); !errors.Is(err, ErrFeedDisabled) {
|
||||
t.Errorf("Expected ErrFeedDisabled, got %v", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-channel:
|
||||
t.Errorf("Expected no data on channel, got data")
|
||||
default:
|
||||
// nothing to do
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdate tests the Update function of the Fills struct.
|
||||
func TestUpdate(t *testing.T) {
|
||||
channel := make(chan interface{}, 1)
|
||||
fill := &Fills{dataHandler: channel, fillsFeedEnabled: true}
|
||||
receivedData := Data{Timestamp: time.Now(), Price: 15.2, Amount: 3.2}
|
||||
if err := fill.Update(receivedData); err != nil {
|
||||
t.Errorf("Update returned error %v", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case data := <-channel:
|
||||
dataSlice, ok := data.([]Data)
|
||||
if !ok {
|
||||
t.Errorf("expected []Data, got %T", data)
|
||||
}
|
||||
|
||||
if len(dataSlice) != 1 || dataSlice[0] != receivedData {
|
||||
t.Errorf("expected data to be sent through channel")
|
||||
}
|
||||
default:
|
||||
t.Errorf("No data sent to channel")
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateNoData tests the Update function with no Data objects
|
||||
func TestUpdateNoData(t *testing.T) {
|
||||
channel := make(chan interface{}, 1)
|
||||
fill := &Fills{dataHandler: channel, fillsFeedEnabled: true}
|
||||
if err := fill.Update(); err != nil {
|
||||
t.Errorf("Update returned error %v", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-channel:
|
||||
t.Errorf("Expected no data on channel, got data")
|
||||
default:
|
||||
// pass, nothing to do
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateMultipleData tests the Update function with multiple Data objects
|
||||
func TestUpdateMultipleData(t *testing.T) {
|
||||
channel := make(chan interface{}, 2)
|
||||
fill := &Fills{dataHandler: channel, fillsFeedEnabled: true}
|
||||
receivedData := Data{Timestamp: time.Now(), Price: 15.2, Amount: 3.2}
|
||||
receivedData2 := Data{Timestamp: time.Now(), Price: 18.2, Amount: 9.0}
|
||||
if err := fill.Update(receivedData, receivedData2); err != nil {
|
||||
t.Errorf("Update returned error %v", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case data := <-channel:
|
||||
dataSlice, ok := data.([]Data)
|
||||
if !ok {
|
||||
t.Errorf("expected []Data, got %T", data)
|
||||
}
|
||||
|
||||
if len(dataSlice) != 2 || dataSlice[0] != receivedData || dataSlice[1] != receivedData2 {
|
||||
t.Errorf("expected data to be sent through channel")
|
||||
}
|
||||
default:
|
||||
t.Errorf("No data sent to channel")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user