Internal state persistence (#27)
Badges / coveralls (push) Successful in 1m4s

Allow us to generate year over year reports without having to rerun everything from the beginning.

Co-authored-by: Natercio Moniz <[email protected]>
This commit was merged in pull request #27.
This commit is contained in:
2026-08-03 00:33:06 +01:00
committed by natercio
parent 1ce8561782
commit 9bd4230ff1
25 changed files with 1726 additions and 178 deletions
+86 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"reflect"
"testing"
"time"
@@ -43,7 +44,7 @@ func TestBuildReport(t *testing.T) {
Taxes: decimal.Decimal{},
})).Times(1)
gotErr := internal.BuildReport(t.Context(), reader, writer, internal.Any())
gotErr := internal.BuildReport(t.Context(), reader, writer)
if gotErr != nil {
t.Fatalf("got unexpected err: %v", gotErr)
}
@@ -98,3 +99,87 @@ func (m ReportItemMatcher) String() string {
}
var _ gomock.Matcher = (*ReportItemMatcher)(nil)
// recordingStore is a test fake for internal.Store that records how many
// times Load/Save were invoked and captures the maps it handed back so a
// test can verify BuildReport wires the store correctly.
type recordingStore struct {
loadCalls int
saveCalls int
loaded map[string]*internal.FillerQueue
saved map[string]*internal.FillerQueue
}
func (s *recordingStore) Load(_ context.Context) (map[string]*internal.FillerQueue, error) {
s.loadCalls++
if s.loaded == nil {
s.loaded = map[string]*internal.FillerQueue{}
}
return s.loaded, nil
}
func (s *recordingStore) Save(_ context.Context, queues map[string]*internal.FillerQueue) error {
s.saveCalls++
s.saved = queues
return nil
}
func TestBuildReport_WithStore(t *testing.T) {
now := time.Now()
ctrl := gomock.NewController(t)
reader := mocks.NewMockRecordReader(ctrl)
records := []internal.Record{
mockRecord(ctrl, 20.0, 10.0, internal.KindBuy, now),
}
reader.EXPECT().ReadRecord(gomock.Any()).DoAndReturn(func(ctx context.Context) (internal.Record, error) {
if len(records) > 0 {
r := records[0]
records = records[1:]
return r, nil
}
return nil, io.EOF
}).Times(2)
// No sells, so the writer must not be called.
writer := mocks.NewMockReportWriter(ctrl)
store := &recordingStore{}
gotErr := internal.BuildReport(t.Context(), reader, writer, internal.WithStore(store))
if gotErr != nil {
t.Fatalf("got unexpected err: %v", gotErr)
}
if store.loadCalls != 1 {
t.Errorf("Load calls: want 1 but got %d", store.loadCalls)
}
if store.saveCalls != 1 {
t.Errorf("Save calls: want 1 but got %d", store.saveCalls)
}
// Load must run before any records are read, so it has already been
// invoked by the time Save is observed.
if store.loadCalls < store.saveCalls {
t.Errorf("Load (%d) should be called at least as many times as Save (%d)", store.loadCalls, store.saveCalls)
}
if store.loaded == nil {
t.Fatalf("Load was not called or returned a nil map")
}
if store.saved == nil {
t.Fatalf("Save was not called or received a nil map")
}
// The map handed to Save must be the same map returned by Load so
// that buy-queue mutations done while processing flow into Save.
if reflect.ValueOf(store.loaded).Pointer() != reflect.ValueOf(store.saved).Pointer() {
t.Errorf("map passed to Save is not the same map returned by Load")
}
// Sanity: the buy record above should have populated an entry for the
// "TEST" symbol in the shared map.
if _, ok := store.saved["TEST"]; !ok {
t.Errorf("want saved map to contain symbol %q but it was missing", "TEST")
}
}