use shopspring/decimal library everywhere
All checks were successful
Generate check / check-generate (push) Has been skipped
Generate check / check-generate (pull_request) Has been skipped
Tests / tests (pull_request) Has been skipped

This commit is contained in:
2025-11-16 23:03:47 +00:00
parent edc1628674
commit a6d56d7441
14 changed files with 421 additions and 282 deletions

View File

@@ -2,17 +2,18 @@ package internal_test
import (
"context"
"fmt"
"io"
"math/big"
"testing"
"time"
"github.com/nmoniz/any2anexoj/internal"
"github.com/nmoniz/any2anexoj/internal/mocks"
"github.com/shopspring/decimal"
"go.uber.org/mock/gomock"
)
func TestReporter_Run(t *testing.T) {
func TestBuildReport(t *testing.T) {
now := time.Now()
ctrl := gomock.NewController(t)
@@ -32,13 +33,13 @@ func TestReporter_Run(t *testing.T) {
}).Times(3)
writer := mocks.NewMockReportWriter(ctrl)
writer.EXPECT().Write(gomock.Any(), gomock.Eq(internal.ReportItem{
BuyValue: new(big.Float).SetFloat64(200.0),
writer.EXPECT().Write(gomock.Any(), eqReportItem(internal.ReportItem{
BuyValue: decimal.NewFromFloat(200.0),
BuyTimestamp: now,
SellValue: new(big.Float).SetFloat64(250.0),
SellValue: decimal.NewFromFloat(250.0),
SellTimestamp: now.Add(1),
Fees: new(big.Float),
Taxes: new(big.Float),
Fees: decimal.Decimal{},
Taxes: decimal.Decimal{},
})).Times(1)
gotErr := internal.BuildReport(t.Context(), reader, writer)
@@ -49,12 +50,47 @@ func TestReporter_Run(t *testing.T) {
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().Price().Return(decimal.NewFromFloat(price)).AnyTimes()
rec.EXPECT().Quantity().Return(decimal.NewFromFloat(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()
rec.EXPECT().Fees().Return(decimal.Decimal{}).AnyTimes()
rec.EXPECT().Taxes().Return(decimal.Decimal{}).AnyTimes()
return rec
}
func eqReportItem(ri internal.ReportItem) ReportItemMatcher {
return ReportItemMatcher{
ReportItem: ri,
}
}
type ReportItemMatcher struct {
internal.ReportItem
}
// Matches implements gomock.Matcher.
func (m ReportItemMatcher) Matches(x any) bool {
if x == nil {
return false
}
switch other := x.(type) {
case internal.ReportItem:
return m.BuyValue.Equal(other.BuyValue) &&
m.BuyTimestamp.Equal(other.BuyTimestamp) &&
m.SellValue.Equal(other.SellValue) &&
m.SellTimestamp.Equal(other.SellTimestamp) &&
m.Fees.Equal(other.Fees) &&
m.Taxes.Equal(other.Taxes)
default:
return false
}
}
func (m ReportItemMatcher) String() string {
return fmt.Sprintf("is equivalent to %v", m.ReportItem)
}
var _ gomock.Matcher = (*ReportItemMatcher)(nil)