package ledger import ( "slices" "testing" "time" ) func TestRegisterPrediction(t *testing.T) { t.Run("contribution", func(t *testing.T) { input := newTestRegister() got, err := RegisterWithContributionPrediction(input, time.Hour*24*365) if err != nil { t.Fatal(err) } t.Logf("%+v", got) if len(got) != len(input)+13 { t.Error(len(got)) } for _, date := range got.Dates() { for _, name := range got.Names() { t.Logf("%s | %s = %+v", date, name, got[date][name]) if ti, _ := dateToTime(date); ti.After(time.Now().Add(time.Hour*24*60)) && got[date][name]["XYZ"] == 0 { t.Error("predicting future contributions lost unmodified currency", got[date][name]) } } } }) t.Run("compounding interest", func(t *testing.T) { input := newTestRegister() got, err := RegisterWithCompoundingInterestPrediction(input, time.Hour*24*365, "X", .04) if err != nil { t.Fatal(err) } t.Logf("%+v", got) if len(got) <= len(input) { t.Error(len(got)) } for _, date := range got.Dates() { for name, balance := range got[date] { t.Logf("%s | %s %s", date, name, balance.Debug()) } } }) } func TestBPIPrediction(t *testing.T) { t.Run("fixed growth", func(t *testing.T) { bpis := BPIs{ USD: BPI{ "2001-01": -1000, "2001-02": 100, }, } got, err := BPIsWithFixedGrowthPrediction(bpis, time.Hour*24*365, string(USD), 0.06) if err != nil { t.Fatal(err) } dates := []string{} for d := range got[USD] { dates = append(dates, d) } slices.Sort(dates) for _, d := range dates { t.Logf("%s | %s %.2f", USD, d, got[USD][d]) } }) } func newTestRegister() map[string]Balances { s := func(t time.Time) string { return t.Format("2006-01") } day := time.Hour * 24 lastYear := time.Now().Add(-1 * day * time.Duration(time.Now().YearDay())) return map[string]Balances{ s(lastYear.Add(day * 0)): Balances{"X": Balance{USD: 1}}, s(lastYear.Add(day * 32)): Balances{"X": Balance{USD: 2}}, s(lastYear.Add(day * 64)): Balances{"X": Balance{USD: 3}}, s(lastYear.Add(day * 94)): Balances{"X": Balance{USD: 4}}, s(lastYear.Add(day * 124)): Balances{"X": Balance{USD: 5}}, s(lastYear.Add(day * 154)): Balances{"X": Balance{USD: 6}}, s(lastYear.Add(day * 184)): Balances{"X": Balance{USD: 8}}, s(lastYear.Add(day * 214)): Balances{"X": Balance{USD: 10}}, s(lastYear.Add(day * 244)): Balances{"X": Balance{USD: 12}}, s(lastYear.Add(day * 274)): Balances{"X": Balance{USD: 16, "XYZ": 1}}, } }