Compare commits
2 Commits
8e2163cce6
...
077b723536
| Author | SHA1 | Date | |
|---|---|---|---|
| 077b723536 | |||
| 143bd844a7 |
@@ -1,3 +1,3 @@
|
||||
package internal
|
||||
|
||||
//go:generate mockgen -destination=mocks/mocks_gen.go -package=mocks -typed . RecordReader,Record
|
||||
//go:generate go tool mockgen -destination=mocks/mocks_gen.go -package=mocks -typed . RecordReader,Record,ReportWriter
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: git.naterciomoniz.net/applications/broker2anexoj/internal (interfaces: RecordReader,Record)
|
||||
// Source: git.naterciomoniz.net/applications/broker2anexoj/internal (interfaces: RecordReader,Record,ReportWriter)
|
||||
//
|
||||
// Generated by this command:
|
||||
//
|
||||
// mockgen -destination=mocks/mocks_gen.go -package=mocks -typed . RecordReader,Record
|
||||
// mockgen -destination=mocks/mocks_gen.go -package=mocks -typed . RecordReader,Record,ReportWriter
|
||||
//
|
||||
|
||||
// Package mocks is a generated GoMock package.
|
||||
@@ -105,6 +105,44 @@ func (m *MockRecord) EXPECT() *MockRecordMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Fees mocks base method.
|
||||
func (m *MockRecord) Fees() *big.Float {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Fees")
|
||||
ret0, _ := ret[0].(*big.Float)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Fees indicates an expected call of Fees.
|
||||
func (mr *MockRecordMockRecorder) Fees() *MockRecordFeesCall {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Fees", reflect.TypeOf((*MockRecord)(nil).Fees))
|
||||
return &MockRecordFeesCall{Call: call}
|
||||
}
|
||||
|
||||
// MockRecordFeesCall wrap *gomock.Call
|
||||
type MockRecordFeesCall struct {
|
||||
*gomock.Call
|
||||
}
|
||||
|
||||
// Return rewrite *gomock.Call.Return
|
||||
func (c *MockRecordFeesCall) Return(arg0 *big.Float) *MockRecordFeesCall {
|
||||
c.Call = c.Call.Return(arg0)
|
||||
return c
|
||||
}
|
||||
|
||||
// Do rewrite *gomock.Call.Do
|
||||
func (c *MockRecordFeesCall) Do(f func() *big.Float) *MockRecordFeesCall {
|
||||
c.Call = c.Call.Do(f)
|
||||
return c
|
||||
}
|
||||
|
||||
// DoAndReturn rewrite *gomock.Call.DoAndReturn
|
||||
func (c *MockRecordFeesCall) DoAndReturn(f func() *big.Float) *MockRecordFeesCall {
|
||||
c.Call = c.Call.DoAndReturn(f)
|
||||
return c
|
||||
}
|
||||
|
||||
// Price mocks base method.
|
||||
func (m *MockRecord) Price() *big.Float {
|
||||
m.ctrl.T.Helper()
|
||||
@@ -257,6 +295,44 @@ func (c *MockRecordSymbolCall) DoAndReturn(f func() string) *MockRecordSymbolCal
|
||||
return c
|
||||
}
|
||||
|
||||
// Taxes mocks base method.
|
||||
func (m *MockRecord) Taxes() *big.Float {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Taxes")
|
||||
ret0, _ := ret[0].(*big.Float)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Taxes indicates an expected call of Taxes.
|
||||
func (mr *MockRecordMockRecorder) Taxes() *MockRecordTaxesCall {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Taxes", reflect.TypeOf((*MockRecord)(nil).Taxes))
|
||||
return &MockRecordTaxesCall{Call: call}
|
||||
}
|
||||
|
||||
// MockRecordTaxesCall wrap *gomock.Call
|
||||
type MockRecordTaxesCall struct {
|
||||
*gomock.Call
|
||||
}
|
||||
|
||||
// Return rewrite *gomock.Call.Return
|
||||
func (c *MockRecordTaxesCall) Return(arg0 *big.Float) *MockRecordTaxesCall {
|
||||
c.Call = c.Call.Return(arg0)
|
||||
return c
|
||||
}
|
||||
|
||||
// Do rewrite *gomock.Call.Do
|
||||
func (c *MockRecordTaxesCall) Do(f func() *big.Float) *MockRecordTaxesCall {
|
||||
c.Call = c.Call.Do(f)
|
||||
return c
|
||||
}
|
||||
|
||||
// DoAndReturn rewrite *gomock.Call.DoAndReturn
|
||||
func (c *MockRecordTaxesCall) DoAndReturn(f func() *big.Float) *MockRecordTaxesCall {
|
||||
c.Call = c.Call.DoAndReturn(f)
|
||||
return c
|
||||
}
|
||||
|
||||
// Timestamp mocks base method.
|
||||
func (m *MockRecord) Timestamp() time.Time {
|
||||
m.ctrl.T.Helper()
|
||||
@@ -294,3 +370,65 @@ func (c *MockRecordTimestampCall) DoAndReturn(f func() time.Time) *MockRecordTim
|
||||
c.Call = c.Call.DoAndReturn(f)
|
||||
return c
|
||||
}
|
||||
|
||||
// MockReportWriter is a mock of ReportWriter interface.
|
||||
type MockReportWriter struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockReportWriterMockRecorder
|
||||
isgomock struct{}
|
||||
}
|
||||
|
||||
// MockReportWriterMockRecorder is the mock recorder for MockReportWriter.
|
||||
type MockReportWriterMockRecorder struct {
|
||||
mock *MockReportWriter
|
||||
}
|
||||
|
||||
// NewMockReportWriter creates a new mock instance.
|
||||
func NewMockReportWriter(ctrl *gomock.Controller) *MockReportWriter {
|
||||
mock := &MockReportWriter{ctrl: ctrl}
|
||||
mock.recorder = &MockReportWriterMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockReportWriter) EXPECT() *MockReportWriterMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Write mocks base method.
|
||||
func (m *MockReportWriter) Write(arg0 internal.ReportItem) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Write", arg0)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Write indicates an expected call of Write.
|
||||
func (mr *MockReportWriterMockRecorder) Write(arg0 any) *MockReportWriterWriteCall {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockReportWriter)(nil).Write), arg0)
|
||||
return &MockReportWriterWriteCall{Call: call}
|
||||
}
|
||||
|
||||
// MockReportWriterWriteCall wrap *gomock.Call
|
||||
type MockReportWriterWriteCall struct {
|
||||
*gomock.Call
|
||||
}
|
||||
|
||||
// Return rewrite *gomock.Call.Return
|
||||
func (c *MockReportWriterWriteCall) Return(arg0 error) *MockReportWriterWriteCall {
|
||||
c.Call = c.Call.Return(arg0)
|
||||
return c
|
||||
}
|
||||
|
||||
// Do rewrite *gomock.Call.Do
|
||||
func (c *MockReportWriterWriteCall) Do(f func(internal.ReportItem) error) *MockReportWriterWriteCall {
|
||||
c.Call = c.Call.Do(f)
|
||||
return c
|
||||
}
|
||||
|
||||
// DoAndReturn rewrite *gomock.Call.DoAndReturn
|
||||
func (c *MockReportWriterWriteCall) DoAndReturn(f func(internal.ReportItem) error) *MockReportWriterWriteCall {
|
||||
c.Call = c.Call.DoAndReturn(f)
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ type Record interface {
|
||||
Price() *big.Float
|
||||
Quantity() *big.Float
|
||||
Timestamp() time.Time
|
||||
Fees() *big.Float
|
||||
Taxes() *big.Float
|
||||
}
|
||||
|
||||
type RecordQueue struct {
|
||||
|
||||
15
internal/report.go
Normal file
15
internal/report.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
type RecordReader interface {
|
||||
// ReadRecord should return Records until an error is found.
|
||||
ReadRecord() (Record, error)
|
||||
}
|
||||
|
||||
type ReportWriter interface {
|
||||
// ReportWriter writes report items
|
||||
Write(ReportItem) error
|
||||
}
|
||||
@@ -9,11 +9,6 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type RecordReader interface {
|
||||
// ReadRecord should return Records until an error is found.
|
||||
ReadRecord() (Record, error)
|
||||
}
|
||||
|
||||
// Reporter consumes each record to produce ReportItem.
|
||||
type Reporter struct {
|
||||
reader RecordReader
|
||||
|
||||
@@ -17,6 +17,8 @@ type Record struct {
|
||||
quantity *big.Float
|
||||
price *big.Float
|
||||
timestamp time.Time
|
||||
fees *big.Float
|
||||
taxes *big.Float
|
||||
}
|
||||
|
||||
func (r Record) Symbol() string {
|
||||
@@ -39,6 +41,14 @@ func (r Record) Timestamp() time.Time {
|
||||
return r.timestamp
|
||||
}
|
||||
|
||||
func (r Record) Fees() *big.Float {
|
||||
return r.fees
|
||||
}
|
||||
|
||||
func (r Record) Taxes() *big.Float {
|
||||
return r.taxes
|
||||
}
|
||||
|
||||
type RecordReader struct {
|
||||
reader *csv.Reader
|
||||
}
|
||||
@@ -90,12 +100,29 @@ func (rr RecordReader) ReadRecord() (internal.Record, error) {
|
||||
return Record{}, fmt.Errorf("parse record timestamp: %w", err)
|
||||
}
|
||||
|
||||
convertionFee, err := parseOptinalDecimal(raw[16])
|
||||
if err != nil {
|
||||
return Record{}, fmt.Errorf("parse record convertion fee: %w", err)
|
||||
}
|
||||
|
||||
stampDutyTax, err := parseOptinalDecimal(raw[14])
|
||||
if err != nil {
|
||||
return Record{}, fmt.Errorf("parse record stamp duty tax: %w", err)
|
||||
}
|
||||
|
||||
frenchTxTax, err := parseOptinalDecimal(raw[18])
|
||||
if err != nil {
|
||||
return Record{}, fmt.Errorf("parse record french transaction tax: %w", err)
|
||||
}
|
||||
|
||||
return Record{
|
||||
symbol: raw[2],
|
||||
side: side,
|
||||
quantity: qant,
|
||||
price: price,
|
||||
timestamp: ts,
|
||||
fees: convertionFee,
|
||||
taxes: new(big.Float).Add(stampDutyTax, frenchTxTax),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
@@ -106,3 +133,14 @@ func parseDecimal(s string) (*big.Float, error) {
|
||||
f, _, err := big.ParseFloat(s, 10, 128, big.ToZero)
|
||||
return f, err
|
||||
}
|
||||
|
||||
// parseOptinalDecimal behaves the same as parseDecimal but returns 0 when len(s) is 0 instead of
|
||||
// error.
|
||||
// Using this function helps avoid issues around converting values due to sligh parameter changes.
|
||||
func parseOptinalDecimal(s string) (*big.Float, error) {
|
||||
if len(s) == 0 {
|
||||
return new(big.Float), nil
|
||||
}
|
||||
|
||||
return parseDecimal(s)
|
||||
}
|
||||
|
||||
@@ -24,75 +24,69 @@ func TestRecordReader_ReadRecord(t *testing.T) {
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "well formed buy",
|
||||
r: bytes.NewBufferString(`Market buy,2025-07-03 10:44:29,SYM123456ABXY,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,7.3690000000,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
||||
name: "well-formed buy",
|
||||
r: bytes.NewBufferString(`Market buy,2025-07-03 10:44:29,SYM123456ABXY,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,7.3690000000,USD,1.17995999,,"EUR",15.25,"EUR",0.25,"EUR",0.02,"EUR",,`),
|
||||
want: Record{
|
||||
symbol: "SYM123456ABXY",
|
||||
side: internal.SideBuy,
|
||||
quantity: ShouldParseDecimal(t, "2.4387014200"),
|
||||
price: ShouldParseDecimal(t, "7.3690000000"),
|
||||
timestamp: time.Date(2025, 7, 3, 10, 44, 29, 0, time.UTC),
|
||||
fees: ShouldParseDecimal(t, "0.02"),
|
||||
taxes: ShouldParseDecimal(t, "0.25"),
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "well formed sell",
|
||||
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:30,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
||||
name: "well-formed sell",
|
||||
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:30,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",0.1,"EUR"`),
|
||||
want: Record{
|
||||
symbol: "IE000GA3D489",
|
||||
side: internal.SideSell,
|
||||
quantity: ShouldParseDecimal(t, "2.4387014200"),
|
||||
price: ShouldParseDecimal(t, "7.9999999999"),
|
||||
timestamp: time.Date(2025, 8, 4, 11, 45, 30, 0, time.UTC),
|
||||
fees: ShouldParseDecimal(t, "0.02"),
|
||||
taxes: ShouldParseDecimal(t, "0.1"),
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "malformed side",
|
||||
r: bytes.NewBufferString(`Aljksdaf Balsjdkf,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
||||
want: Record{},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty side",
|
||||
r: bytes.NewBufferString(`,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,0x1234,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
||||
want: Record{},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "malformed qantity",
|
||||
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,0x1234,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
||||
want: Record{},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty qantity",
|
||||
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
||||
want: Record{},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "malformed price",
|
||||
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,0b101010,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
||||
want: Record{},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty price",
|
||||
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
||||
want: Record{},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "malformed timestamp",
|
||||
r: bytes.NewBufferString(`Market sell,2006-01-02T15:04:05Z07:00,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
||||
want: Record{},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty timestamp",
|
||||
r: bytes.NewBufferString(`Market sell,,IE000GA3D489,ABXY,"Aspargus Brocoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
||||
want: Record{},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
@@ -130,6 +124,14 @@ func TestRecordReader_ReadRecord(t *testing.T) {
|
||||
if !got.Timestamp().Equal(tt.want.timestamp) {
|
||||
t.Fatalf("want timestamp %v but got %v", tt.want.timestamp, got.Timestamp())
|
||||
}
|
||||
|
||||
if got.Fees().Cmp(tt.want.fees) != 0 {
|
||||
t.Fatalf("want fees %v but got %v", tt.want.fees, got.Fees())
|
||||
}
|
||||
|
||||
if got.Taxes().Cmp(tt.want.taxes) != 0 {
|
||||
t.Fatalf("want taxes %v but got %v", tt.want.taxes, got.Taxes())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user