isolate record reading and writing from processing
All checks were successful
Tests / tests (pull_request) Successful in 17s
All checks were successful
Tests / tests (pull_request) Successful in 17s
This commit is contained in:
59
internal/report_test.go
Normal file
59
internal/report_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package internal_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
"math/big"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.naterciomoniz.net/applications/broker2anexoj/internal"
|
||||
"git.naterciomoniz.net/applications/broker2anexoj/internal/mocks"
|
||||
"go.uber.org/mock/gomock"
|
||||
)
|
||||
|
||||
func TestReporter_Run(t *testing.T) {
|
||||
now := time.Now()
|
||||
ctrl := gomock.NewController(t)
|
||||
|
||||
reader := mocks.NewMockRecordReader(ctrl)
|
||||
records := []internal.Record{
|
||||
mockRecord(ctrl, 20.0, 10.0, internal.SideBuy, now),
|
||||
mockRecord(ctrl, 25.0, 10.0, internal.SideSell, now.Add(1)),
|
||||
}
|
||||
reader.EXPECT().ReadRecord().DoAndReturn(func() (internal.Record, error) {
|
||||
if len(records) > 0 {
|
||||
r := records[0]
|
||||
records = records[1:]
|
||||
return r, nil
|
||||
} else {
|
||||
return nil, io.EOF
|
||||
}
|
||||
}).Times(3)
|
||||
|
||||
writer := mocks.NewMockReportWriter(ctrl)
|
||||
writer.EXPECT().Write(gomock.Eq(internal.ReportItem{
|
||||
BuyValue: new(big.Float).SetFloat64(200.0),
|
||||
BuyTimestamp: now,
|
||||
SellValue: new(big.Float).SetFloat64(250.0),
|
||||
SellTimestamp: now.Add(1),
|
||||
Fees: new(big.Float),
|
||||
Taxes: new(big.Float),
|
||||
})).Times(1)
|
||||
|
||||
gotErr := internal.BuildReport(reader, writer)
|
||||
if gotErr != nil {
|
||||
t.Fatalf("got unexpected err: %v", gotErr)
|
||||
}
|
||||
}
|
||||
|
||||
func mockRecord(ctrl *gomock.Controller, price, quantity float64, side internal.Side, ts time.Time) *mocks.MockRecord {
|
||||
rec := mocks.NewMockRecord(ctrl)
|
||||
rec.EXPECT().Price().Return(big.NewFloat(price)).AnyTimes()
|
||||
rec.EXPECT().Quantity().Return(big.NewFloat(quantity)).AnyTimes()
|
||||
rec.EXPECT().Side().Return(side).AnyTimes()
|
||||
rec.EXPECT().Symbol().Return("TEST").AnyTimes()
|
||||
rec.EXPECT().Timestamp().Return(ts).AnyTimes()
|
||||
rec.EXPECT().Fees().Return(new(big.Float)).AnyTimes()
|
||||
rec.EXPECT().Taxes().Return(new(big.Float)).AnyTimes()
|
||||
return rec
|
||||
}
|
||||
Reference in New Issue
Block a user