Implement HTML writer (#33)
Badges / coveralls (push) Successful in 1m1s

Reviewed-on: #33
Co-authored-by: Natercio Moniz <[email protected]>
This commit was merged in pull request #33.
This commit is contained in:
natercio authored and Natercio Moniz committed 2026-10-05 16:25:21 +01:00
1 parent 6c989c338c
commit 47f38fce2b
20 files changed
+782 -315

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
package csv_test
import (
"bytes"
"strings"
"testing"
"time"
"github.com/nmoniz/any2anexoj/internal"
"github.com/nmoniz/any2anexoj/internal/writing/csv"
"github.com/shopspring/decimal"
)
func TestWriter_Render(t *testing.T) {
aw := internal.NewAggregatorWriter()
ctx := t.Context()
items := []internal.ReportItem{
{
Symbol: "AAPL",
Nature: internal.NatureG01,
BrokerCountry: 826, // United Kingdom
AssetCountry: 840, // United States
BuyValue: decimal.NewFromFloat(100.50),
BuyTimestamp: time.Date(2023, 1, 15, 0, 0, 0, 0, time.UTC),
SellValue: decimal.NewFromFloat(150.75),
SellTimestamp: time.Date(2023, 6, 20, 0, 0, 0, 0, time.UTC),
Fees: decimal.NewFromFloat(2.50),
Taxes: decimal.NewFromFloat(5.00),
},
{
Symbol: "VWRL",
Nature: internal.NatureG20,
BrokerCountry: 196, // Portugal equivalent demo
AssetCountry: 372, // Ireland
BuyValue: decimal.NewFromFloat(200.00),
BuyTimestamp: time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC),
SellValue: decimal.NewFromFloat(250.25),
SellTimestamp: time.Date(2024, 7, 10, 0, 0, 0, 0, time.UTC),
Fees: decimal.NewFromFloat(1.00),
Taxes: decimal.NewFromFloat(0.00),
},
}
for _, ri := range items {
if err := aw.Write(ctx, ri); err != nil {
t.Fatalf("write report item: %v", err)
}
}
var buf bytes.Buffer
if err := csv.NewWriter(&buf).Render(aw); err != nil {
t.Fatalf("render: %v", err)
}
out := buf.String()
wantHeader := "source_country,code,realization_year,realization_month,realization_day,realization_value,acquisition_year,acquisition_month,acquisition_day,acquisition_value,expenses,foreign_tax_paid,counter_country"
if !strings.HasPrefix(out, wantHeader+"\n") {
t.Errorf("expected header row %q, got output:\n%s", wantHeader, out)
}
wantRow1 := "840,G01,2023,6,20,150.75,2023,1,15,100.50,2.50,5.00,826"
if !strings.Contains(out, wantRow1+"\n") {
t.Errorf("expected row 1 %q in output:\n%s", wantRow1, out)
}
wantRow2 := "372,G20,2024,7,10,250.25,2024,2,1,200.00,1.00,0.00,196"
if !strings.Contains(out, wantRow2+"\n") {
t.Errorf("expected row 2 %q in output:\n%s", wantRow2, out)
}
// Make sure the header only appears once and we have exactly 1 header + 2 data rows.
lines := strings.Split(strings.TrimRight(out, "\n"), "\n")
if got, want := len(lines), 3; got != want {
t.Errorf("expected %d lines (1 header + 2 rows), got %d:\n%s", want, got, out)
}
}