still adding the 'Calculated' column
This commit is contained in:
@@ -1,101 +1,57 @@
|
||||
package expenses
|
||||
|
||||
// Do the test like this
|
||||
// func TestSplitByPercent(t *testing.T) {
|
||||
// tests := []struct {
|
||||
// name string
|
||||
// total string
|
||||
// shares []Share
|
||||
// ownerIdx int
|
||||
// ownerAbsorb bool
|
||||
// want []int64 // cents
|
||||
// }{
|
||||
// {
|
||||
// name: "Even split, no remainder",
|
||||
// total: "120.00",
|
||||
// shares: []Share{
|
||||
// {ID: "P1", Percent: d("50")},
|
||||
// {ID: "P2", Percent: d("50")},
|
||||
// },
|
||||
// ownerIdx: 0,
|
||||
// ownerAbsorb: false,
|
||||
// want: []int64{6000, 6000},
|
||||
// },
|
||||
// {
|
||||
// name: "Three way split with remainder (largest remainder mode)",
|
||||
// total: "111.11",
|
||||
// shares: []Share{
|
||||
// {ID: "P1", Percent: d("34")},
|
||||
// {ID: "P2", Percent: d("33")},
|
||||
// {ID: "P3", Percent: d("33")},
|
||||
// },
|
||||
// ownerIdx: 0,
|
||||
// ownerAbsorb: false,
|
||||
// // One possible: P1=37.78, P2=36.67, P3=36.66
|
||||
// want: []int64{3778, 3667, 3666},
|
||||
// },
|
||||
// {
|
||||
// name: "Three way split with remainder (owner absorbs mode)",
|
||||
// total: "111.11",
|
||||
// shares: []Share{
|
||||
// {ID: "P1", Percent: d("34")},
|
||||
// {ID: "P2", Percent: d("33")},
|
||||
// {ID: "P3", Percent: d("33")},
|
||||
// },
|
||||
// ownerIdx: 0,
|
||||
// ownerAbsorb: true,
|
||||
// // Owner takes both extra cents
|
||||
// want: []int64{3779, 3666, 3666},
|
||||
// },
|
||||
// {
|
||||
// name: "Unequal split with rounding",
|
||||
// total: "10.00",
|
||||
// shares: []Share{
|
||||
// {ID: "P1", Percent: d("70")},
|
||||
// {ID: "P2", Percent: d("30")},
|
||||
// },
|
||||
// ownerIdx: 0,
|
||||
// ownerAbsorb: false,
|
||||
// want: []int64{700, 300}, // perfect
|
||||
// },
|
||||
// {
|
||||
// name: "Edge case: all to one person",
|
||||
// total: "99.99",
|
||||
// shares: []Share{
|
||||
// {ID: "P1", Percent: d("100")},
|
||||
// },
|
||||
// ownerIdx: 0,
|
||||
// ownerAbsorb: true,
|
||||
// want: []int64{9999},
|
||||
// },
|
||||
// {
|
||||
// name: "Stuff",
|
||||
// total: "111.11",
|
||||
// shares: []Share{
|
||||
// {ID: "P1", Percent: d("0.34")},
|
||||
// {ID: "P2", Percent: d("0.33")},
|
||||
// {ID: "P3", Percent: d("0.33")},
|
||||
// },
|
||||
// ownerIdx: 0,
|
||||
// ownerAbsorb: true,
|
||||
// want: []int64{3779, 3666, 3666},
|
||||
// },
|
||||
// }
|
||||
//
|
||||
// for _, tt := range tests {
|
||||
// t.Run(tt.name, func(t *testing.T) {
|
||||
// got, err := SplitByPercent(d(tt.total), tt.shares, tt.ownerIdx, tt.ownerAbsorb)
|
||||
// if err != nil {
|
||||
// t.Fatalf("unexpected error: %v", err)
|
||||
// }
|
||||
// if len(got) != len(tt.want) {
|
||||
// t.Fatalf("length mismatch: got %d, want %d", len(got), len(tt.want))
|
||||
// }
|
||||
// for i := range got {
|
||||
// if got[i].Cents != tt.want[i] {
|
||||
// t.Errorf("person %s: got %d, want %d", tt.shares[i].ID, got[i].Cents, tt.want[i])
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
dec "github.com/shopspring/decimal"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizeShares(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
exp *mod.Expense
|
||||
expectedShares mod.Shares
|
||||
}{
|
||||
{
|
||||
name: "Split with remainder",
|
||||
exp: &expense5,
|
||||
expectedShares: mod.Shares{
|
||||
{ExpShareID: 0, User: user1,
|
||||
Share: dec.NewFromFloat(0.34), Calculated: dec.NewFromFloat(37.79)},
|
||||
{ExpShareID: 1, User: user2,
|
||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(36.66)},
|
||||
{ExpShareID: 2, User: user3,
|
||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(36.66)},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Even split",
|
||||
exp: &expense3,
|
||||
expectedShares: mod.Shares{
|
||||
{ExpShareID: 0, User: user1,
|
||||
Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.00)},
|
||||
{ExpShareID: 1, User: user2,
|
||||
Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.00)},
|
||||
{ExpShareID: 2, User: user3,
|
||||
Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.00)},
|
||||
{ExpShareID: 3, User: user4,
|
||||
Share: dec.NewFromFloat(0.1), Calculated: dec.NewFromFloat(16.00)},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := normalizeShares(tc.exp)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to normalize shares: %+v", err)
|
||||
}
|
||||
|
||||
if !tc.expectedShares.Equal(tc.exp.Shares) {
|
||||
t.Errorf("Resulting shares: %+v\nAre not correct: %+v", tc.exp.Shares,
|
||||
tc.expectedShares)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user