register and balances accept bpi

main
Bel LaPointe 2023-10-25 14:02:44 -06:00
parent f2ec1233d7
commit 6b666c7220
5 changed files with 54 additions and 26 deletions

View File

@ -9,6 +9,23 @@ type Balances map[string]Balance
type Balance map[Currency]float64
func (balances Balances) WithBPIs(bpis BPIs) Balances {
result := make(Balances)
for k, v := range balances {
if _, ok := result[k]; !ok {
result[k] = make(Balance)
}
for k2, v2 := range v {
scalar := 1.0
if k2 != USD {
scalar = bpis[k2].Lookup("9")
}
result[k][USD] += v2 * scalar
}
}
return result
}
func (balances Balances) PushAll(other Balances) {
for k, v := range other {
if _, ok := balances[k]; !ok {

View File

@ -28,15 +28,6 @@ func newDelta(d, desc, name string, v float64, c string, isSet bool) Delta {
}
}
func (delta Delta) Plus(other Delta) Delta {
return Delta{
Date: other.Date,
Name: delta.Name,
Value: delta.Value + other.Value,
Currency: other.Currency,
}
}
func (delta Delta) Debug() string {
return fmt.Sprintf("{@%s %s:\"%s\" %s%.2f %s}", delta.Date, delta.Name, delta.Description, func() string {
if !delta.isSet {

View File

@ -21,21 +21,4 @@ func TestDelta(t *testing.T) {
t.Error(delta.Currency)
}
t.Log(delta)
d2 := "2099-09-08"
delta2 := newDelta(d2, "", "name", 11.11, "$", false)
combined := delta.Plus(delta2)
if combined.Date != d2 {
t.Error(combined.Date)
}
if combined.Name != "name" {
t.Error(combined.Name)
}
if combined.Value != 45.67 {
t.Error(combined.Value)
}
if combined.Currency != USD {
t.Error(combined.Currency)
}
}

View File

@ -92,6 +92,42 @@ func TestFileAdd(t *testing.T) {
}
}
func TestFileTestdataMacroWithBPI(t *testing.T) {
paths, err := filepath.Glob("./testdata/macro.d/*")
if err != nil {
t.Fatal(err)
}
f, err := NewFiles(paths[0], paths[1:]...)
if err != nil {
t.Fatal(err)
}
deltas, err := f.Deltas()
if err != nil {
t.Fatal(err)
}
bpis, err := NewBPIs("./testdata/bpi.bpi")
if err != nil {
t.Fatal(err)
}
t.Run("bal like", func(t *testing.T) {
bal := deltas.Like(LikeName(`^AssetAccount:Bond`)).Balances().WithBPIs(bpis)
for k, v := range bal {
t.Logf("%s: %+v", k, v)
}
})
t.Run("reg like", func(t *testing.T) {
reg := deltas.Like(LikeName(`^AssetAccount:Bond`)).Register()
for k, v := range reg {
t.Logf("%s: %+v", k, v.WithBPIs(bpis))
}
})
}
func TestFileTestdata(t *testing.T) {
t.Run("macro.d", func(t *testing.T) {
paths, err := filepath.Glob("./testdata/macro.d/*")

1
ledger/testdata/bpi.bpi vendored Symbolic link
View File

@ -0,0 +1 @@
../../../../../../Sync/Core/ledger/bpi.dat