report show nature
Some checks failed
Generate check / check-changes (push) Successful in 4s
Generate check / check-generate (push) Has been skipped
Generate check / check-changes (pull_request) Successful in 4s
Quality / check-changes (pull_request) Successful in 3s
Generate check / check-generate (pull_request) Successful in 28s
Quality / tests (pull_request) Failing after 41s
Some checks failed
Generate check / check-changes (push) Successful in 4s
Generate check / check-generate (push) Has been skipped
Generate check / check-changes (pull_request) Successful in 4s
Quality / check-changes (pull_request) Successful in 3s
Generate check / check-generate (pull_request) Successful in 28s
Quality / tests (pull_request) Failing after 41s
This commit is contained in:
@@ -4,8 +4,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/nmoniz/any2anexoj/internal"
|
"github.com/nmoniz/any2anexoj/internal"
|
||||||
"github.com/nmoniz/any2anexoj/internal/trading212"
|
"github.com/nmoniz/any2anexoj/internal/trading212"
|
||||||
@@ -18,7 +20,9 @@ import (
|
|||||||
var platform = pflag.StringP("platform", "p", "trading212", "one of the supported platforms")
|
var platform = pflag.StringP("platform", "p", "trading212", "one of the supported platforms")
|
||||||
|
|
||||||
var readerFactories = map[string]func() internal.RecordReader{
|
var readerFactories = map[string]func() internal.RecordReader{
|
||||||
"trading212": func() internal.RecordReader { return trading212.NewRecordReader(os.Stdin) },
|
"trading212": func() internal.RecordReader {
|
||||||
|
return trading212.NewRecordReader(os.Stdin, internal.NewOpenFIGI(&http.Client{Timeout: 5 * time.Second}))
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -221,10 +221,10 @@ func (c *MockRecordFeesCall) DoAndReturn(f func() decimal.Decimal) *MockRecordFe
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Nature mocks base method.
|
// Nature mocks base method.
|
||||||
func (m *MockRecord) Nature() string {
|
func (m *MockRecord) Nature() internal.Nature {
|
||||||
m.ctrl.T.Helper()
|
m.ctrl.T.Helper()
|
||||||
ret := m.ctrl.Call(m, "Nature")
|
ret := m.ctrl.Call(m, "Nature")
|
||||||
ret0, _ := ret[0].(string)
|
ret0, _ := ret[0].(internal.Nature)
|
||||||
return ret0
|
return ret0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,19 +241,19 @@ type MockRecordNatureCall struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return rewrite *gomock.Call.Return
|
// Return rewrite *gomock.Call.Return
|
||||||
func (c *MockRecordNatureCall) Return(arg0 string) *MockRecordNatureCall {
|
func (c *MockRecordNatureCall) Return(arg0 internal.Nature) *MockRecordNatureCall {
|
||||||
c.Call = c.Call.Return(arg0)
|
c.Call = c.Call.Return(arg0)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do rewrite *gomock.Call.Do
|
// Do rewrite *gomock.Call.Do
|
||||||
func (c *MockRecordNatureCall) Do(f func() string) *MockRecordNatureCall {
|
func (c *MockRecordNatureCall) Do(f func() internal.Nature) *MockRecordNatureCall {
|
||||||
c.Call = c.Call.Do(f)
|
c.Call = c.Call.Do(f)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// DoAndReturn rewrite *gomock.Call.DoAndReturn
|
// DoAndReturn rewrite *gomock.Call.DoAndReturn
|
||||||
func (c *MockRecordNatureCall) DoAndReturn(f func() string) *MockRecordNatureCall {
|
func (c *MockRecordNatureCall) DoAndReturn(f func() internal.Nature) *MockRecordNatureCall {
|
||||||
c.Call = c.Call.DoAndReturn(f)
|
c.Call = c.Call.DoAndReturn(f)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ type RecordReader interface {
|
|||||||
|
|
||||||
type ReportItem struct {
|
type ReportItem struct {
|
||||||
Symbol string
|
Symbol string
|
||||||
Nature string
|
Nature Nature
|
||||||
BrokerCountry int64
|
BrokerCountry int64
|
||||||
AssetCountry int64
|
AssetCountry int64
|
||||||
BuyValue decimal.Decimal
|
BuyValue decimal.Decimal
|
||||||
@@ -118,6 +118,7 @@ func processRecord(ctx context.Context, q *FillerQueue, rec Record, writer Repor
|
|||||||
SellTimestamp: rec.Timestamp(),
|
SellTimestamp: rec.Timestamp(),
|
||||||
Fees: buy.Fees().Add(rec.Fees()),
|
Fees: buy.Fees().Add(rec.Fees()),
|
||||||
Taxes: buy.Taxes().Add(rec.Fees()),
|
Taxes: buy.Taxes().Add(rec.Fees()),
|
||||||
|
Nature: buy.Nature(),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("write report item: %w", err)
|
return fmt.Errorf("write report item: %w", err)
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func (tw *TableWriter) Write(_ context.Context, ri ReportItem) error {
|
|||||||
tw.totalFees = tw.totalFees.Add(ri.Fees)
|
tw.totalFees = tw.totalFees.Add(ri.Fees)
|
||||||
tw.totalTaxes = tw.totalTaxes.Add(ri.Taxes)
|
tw.totalTaxes = tw.totalTaxes.Add(ri.Taxes)
|
||||||
|
|
||||||
tw.table.AppendRow(table.Row{ri.AssetCountry, "G!!", ri.SellTimestamp.Year(), int(ri.SellTimestamp.Month()), ri.SellTimestamp.Day(), ri.SellValue, ri.BuyTimestamp.Year(), ri.BuyTimestamp.Month(), ri.BuyTimestamp.Day(), ri.BuyValue, ri.Fees, ri.Taxes, ri.BrokerCountry})
|
tw.table.AppendRow(table.Row{ri.AssetCountry, ri.Nature, ri.SellTimestamp.Year(), int(ri.SellTimestamp.Month()), ri.SellTimestamp.Day(), ri.SellValue, ri.BuyTimestamp.Year(), ri.BuyTimestamp.Month(), ri.BuyTimestamp.Day(), ri.BuyValue, ri.Fees, ri.Taxes, ri.BrokerCountry})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ import (
|
|||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log/slog"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/biter777/countries"
|
"github.com/biter777/countries"
|
||||||
@@ -68,11 +70,13 @@ func (r Record) Nature() internal.Nature {
|
|||||||
|
|
||||||
type RecordReader struct {
|
type RecordReader struct {
|
||||||
reader *csv.Reader
|
reader *csv.Reader
|
||||||
|
figi *internal.OpenFIGI
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRecordReader(r io.Reader) *RecordReader {
|
func NewRecordReader(r io.Reader, f *internal.OpenFIGI) *RecordReader {
|
||||||
return &RecordReader{
|
return &RecordReader{
|
||||||
reader: csv.NewReader(r),
|
reader: csv.NewReader(r),
|
||||||
|
figi: f,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +87,7 @@ const (
|
|||||||
LimitSell = "limit sell"
|
LimitSell = "limit sell"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (rr RecordReader) ReadRecord(_ context.Context) (internal.Record, error) {
|
func (rr RecordReader) ReadRecord(ctx context.Context) (internal.Record, error) {
|
||||||
for {
|
for {
|
||||||
raw, err := rr.reader.Read()
|
raw, err := rr.reader.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -140,10 +144,31 @@ func (rr RecordReader) ReadRecord(_ context.Context) (internal.Record, error) {
|
|||||||
fees: conversionFee,
|
fees: conversionFee,
|
||||||
taxes: stampDutyTax.Add(frenchTxTax),
|
taxes: stampDutyTax.Add(frenchTxTax),
|
||||||
timestamp: ts,
|
timestamp: ts,
|
||||||
|
natureGetter: figiNatureGetter(ctx, rr.figi, raw[2]),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func figiNatureGetter(ctx context.Context, of *internal.OpenFIGI, isin string) func() internal.Nature {
|
||||||
|
return sync.OnceValue(func() internal.Nature {
|
||||||
|
secType, err := of.SecurityTypeByISIN(ctx, isin)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed to get security type by ISIN", slog.Any("err", err), slog.String("isin", isin))
|
||||||
|
return internal.NatureUnknown
|
||||||
|
}
|
||||||
|
|
||||||
|
switch secType {
|
||||||
|
case "Common Stock":
|
||||||
|
return internal.NatureG01
|
||||||
|
case "ETP":
|
||||||
|
return internal.NatureG20
|
||||||
|
default:
|
||||||
|
slog.Error("got unsupported security type for ISIN", slog.String("isin", isin), slog.String("securityType", secType))
|
||||||
|
return internal.NatureUnknown
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// parseFloat attempts to parse a string using a standard precision and rounding mode.
|
// parseFloat attempts to parse a string using a standard precision and rounding mode.
|
||||||
// Using this function helps avoid issues around converting values due to sligh parameter changes.
|
// Using this function helps avoid issues around converting values due to sligh parameter changes.
|
||||||
func parseDecimal(s string) (decimal.Decimal, error) {
|
func parseDecimal(s string) (decimal.Decimal, error) {
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package trading212
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -34,6 +36,7 @@ func TestRecordReader_ReadRecord(t *testing.T) {
|
|||||||
timestamp: time.Date(2025, 7, 3, 10, 44, 29, 0, time.UTC),
|
timestamp: time.Date(2025, 7, 3, 10, 44, 29, 0, time.UTC),
|
||||||
fees: ShouldParseDecimal(t, "0.02"),
|
fees: ShouldParseDecimal(t, "0.02"),
|
||||||
taxes: ShouldParseDecimal(t, "0.25"),
|
taxes: ShouldParseDecimal(t, "0.25"),
|
||||||
|
natureGetter: func() internal.Nature { return internal.NatureG01 },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -47,6 +50,7 @@ func TestRecordReader_ReadRecord(t *testing.T) {
|
|||||||
timestamp: time.Date(2025, 8, 4, 11, 45, 30, 0, time.UTC),
|
timestamp: time.Date(2025, 8, 4, 11, 45, 30, 0, time.UTC),
|
||||||
fees: ShouldParseDecimal(t, "0.02"),
|
fees: ShouldParseDecimal(t, "0.02"),
|
||||||
taxes: ShouldParseDecimal(t, "0.1"),
|
taxes: ShouldParseDecimal(t, "0.1"),
|
||||||
|
natureGetter: func() internal.Nature { return internal.NatureG01 },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -79,6 +83,16 @@ func TestRecordReader_ReadRecord(t *testing.T) {
|
|||||||
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Broccoli",EOF987654321,2.4387014200,,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:39,IE000GA3D489,ABXY,"Aspargus Broccoli",EOF987654321,2.4387014200,,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "malformed fees",
|
||||||
|
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:30,IE000GA3D489,ABXY,"Aspargus Broccoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,BAD,"EUR",0.1,"EUR"`),
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "malformed taxes",
|
||||||
|
r: bytes.NewBufferString(`Market sell,2025-08-04 11:45:30,IE000GA3D489,ABXY,"Aspargus Broccoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",BAD,"EUR"`),
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "malformed timestamp",
|
name: "malformed timestamp",
|
||||||
r: bytes.NewBufferString(`Market sell,2006-01-02T15:04:05Z07:00,IE000GA3D489,ABXY,"Aspargus Broccoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
r: bytes.NewBufferString(`Market sell,2006-01-02T15:04:05Z07:00,IE000GA3D489,ABXY,"Aspargus Broccoli",EOF987654321,2.4387014200,7.9999999999,USD,1.17995999,,"EUR",15.25,"EUR",,,0.02,"EUR",,`),
|
||||||
@@ -92,7 +106,7 @@ func TestRecordReader_ReadRecord(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
rr := NewRecordReader(tt.r)
|
rr := NewRecordReader(tt.r, NewFigiClientSecurityTypeStub(t, "Common Stock"))
|
||||||
got, gotErr := rr.ReadRecord(t.Context())
|
got, gotErr := rr.ReadRecord(t.Context())
|
||||||
if gotErr != nil {
|
if gotErr != nil {
|
||||||
if !tt.wantErr {
|
if !tt.wantErr {
|
||||||
@@ -132,6 +146,48 @@ func TestRecordReader_ReadRecord(t *testing.T) {
|
|||||||
if got.Taxes().Cmp(tt.want.taxes) != 0 {
|
if got.Taxes().Cmp(tt.want.taxes) != 0 {
|
||||||
t.Fatalf("want taxes %v but got %v", tt.want.taxes, got.Taxes())
|
t.Fatalf("want taxes %v but got %v", tt.want.taxes, got.Taxes())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tt.want.natureGetter != nil && tt.want.Nature() != got.Nature() {
|
||||||
|
t.Fatalf("want nature %v but got %v", tt.want.Nature(), got.Nature())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_figiNatureGetter(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string // description of this test case
|
||||||
|
of *internal.OpenFIGI
|
||||||
|
want internal.Nature
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Common Stock translates to G01",
|
||||||
|
of: NewFigiClientSecurityTypeStub(t, "Common Stock"),
|
||||||
|
want: internal.NatureG01,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ETP translates to G20",
|
||||||
|
of: NewFigiClientSecurityTypeStub(t, "ETP"),
|
||||||
|
want: internal.NatureG20,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Other translates to Unknown",
|
||||||
|
of: NewFigiClientSecurityTypeStub(t, "Other"),
|
||||||
|
want: internal.NatureUnknown,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Request fails",
|
||||||
|
of: NewFigiClientErrorStub(t, fmt.Errorf("boom")),
|
||||||
|
want: internal.NatureUnknown,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
getter := figiNatureGetter(t.Context(), tt.of, "IR123456789")
|
||||||
|
got := getter()
|
||||||
|
if tt.want != got {
|
||||||
|
t.Errorf("want %v but got %v", tt.want, got)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -145,3 +201,40 @@ func ShouldParseDecimal(t testing.TB, sf string) decimal.Decimal {
|
|||||||
}
|
}
|
||||||
return bf
|
return bf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RoundTripFunc func(req *http.Request) (*http.Response, error)
|
||||||
|
|
||||||
|
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
return f(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFigiClientSecurityTypeStub(t testing.TB, securityType string) *internal.OpenFIGI {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
c := &http.Client{
|
||||||
|
Timeout: time.Second,
|
||||||
|
Transport: RoundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||||
|
return &http.Response{
|
||||||
|
Status: http.StatusText(http.StatusOK),
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
Body: io.NopCloser(bytes.NewBufferString(fmt.Sprintf(`[{"data":[{"securityType":%q}]}]`, securityType))),
|
||||||
|
Request: req,
|
||||||
|
}, nil
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
return internal.NewOpenFIGI(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFigiClientErrorStub(t testing.TB, err error) *internal.OpenFIGI {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
c := &http.Client{
|
||||||
|
Timeout: time.Second,
|
||||||
|
Transport: RoundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||||
|
return nil, err
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
return internal.NewOpenFIGI(c)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user