Fixed debts to use the shares.Calculated field
This commit is contained in:
@@ -1 +1,2 @@
|
||||
build/
|
||||
coverage.html
|
||||
|
||||
@@ -26,3 +26,9 @@ launch_container:
|
||||
|
||||
push_image:
|
||||
docker push $(registry)/$(repo)/expensesweb:$(tag)
|
||||
|
||||
test:
|
||||
# go test ./... -cover -bench=
|
||||
go test -coverprofile=coverage.out ./...
|
||||
go tool cover -html=coverage.out -o coverage.html
|
||||
rm ./coverage.out
|
||||
|
||||
@@ -41,7 +41,7 @@ func filterExpenseParticipants(e *mod.Expense) (UserTabs, UserTabs) {
|
||||
creditors := UserTabs{}
|
||||
|
||||
for user, share := range shares {
|
||||
debt := (share.Mul(e.Value)).Sub(payments[user])
|
||||
debt := share.Sub(payments[user])
|
||||
if debt.LessThan(dec.NewFromFloat(0.0)) {
|
||||
creditors = append(creditors, UserTab{
|
||||
User: user,
|
||||
|
||||
@@ -51,10 +51,10 @@ var (
|
||||
ExpID: 0,
|
||||
Value: dec.NewFromInt(160),
|
||||
Shares: []mod.Share{
|
||||
{ExpShareID: 0, User: user1, Share: dec.NewFromFloat(0.3)},
|
||||
{ExpShareID: 1, User: user2, Share: dec.NewFromFloat(0.3)},
|
||||
{ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.3)},
|
||||
{ExpShareID: 3, User: user4, Share: dec.NewFromFloat(0.1)},
|
||||
{ExpShareID: 0, User: user1, Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.0)},
|
||||
{ExpShareID: 1, User: user2, Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.0)},
|
||||
{ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.0)},
|
||||
{ExpShareID: 3, User: user4, Share: dec.NewFromFloat(0.1), Calculated: dec.NewFromFloat(16)},
|
||||
},
|
||||
Payments: []mod.ExpensePayment{
|
||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(40)},
|
||||
@@ -178,6 +178,24 @@ var (
|
||||
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromFloat(36.66)},
|
||||
},
|
||||
}
|
||||
|
||||
expense6 = mod.Expense{
|
||||
ExpID: 252,
|
||||
Value: dec.NewFromFloat(0.50),
|
||||
Shares: []mod.Share{
|
||||
{ExpShareID: 0, User: user1,
|
||||
Share: dec.NewFromFloat(0.34), Calculated: dec.NewFromFloat(0.18)},
|
||||
{ExpShareID: 1, User: user2,
|
||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(0.16)},
|
||||
{ExpShareID: 2, User: user3,
|
||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(0.16)},
|
||||
},
|
||||
Payments: []mod.ExpensePayment{
|
||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromFloat(0.50)},
|
||||
{ExpPaymID: 1, User: user2, PayedAmount: dec.NewFromFloat(0.0)},
|
||||
{ExpPaymID: 2, User: user3, PayedAmount: dec.NewFromFloat(0.0)},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func TestSharesAndPaymentsMapping(t *testing.T) {
|
||||
|
||||
@@ -2,32 +2,57 @@ package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
dec "github.com/shopspring/decimal"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func TestFilterExpenseParticipants(t *testing.T) {
|
||||
// Use expense2 here for the test
|
||||
expectedCreditors := UserTabs{
|
||||
{User: user1, Sum: dec.NewFromFloat(32.0)},
|
||||
{User: user3, Sum: dec.NewFromFloat(22.0)},
|
||||
testCases := []struct {
|
||||
name string
|
||||
exp *mod.Expense
|
||||
expectedDebtors UserTabs
|
||||
expectedCreditors UserTabs
|
||||
}{
|
||||
{
|
||||
name: "Expense something something",
|
||||
exp: &expense2,
|
||||
expectedCreditors: UserTabs{
|
||||
{User: user1, Sum: dec.NewFromFloat(32.0)},
|
||||
{User: user3, Sum: dec.NewFromFloat(22.0)},
|
||||
},
|
||||
expectedDebtors: UserTabs{
|
||||
{User: user2, Sum: dec.NewFromFloat(43.0)},
|
||||
{User: user4, Sum: dec.NewFromFloat(11.0)},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Expense cents",
|
||||
exp: &expense6,
|
||||
expectedCreditors: UserTabs{
|
||||
{User: user1, Sum: dec.NewFromFloat(0.32)},
|
||||
},
|
||||
expectedDebtors: UserTabs{
|
||||
{User: user2, Sum: dec.NewFromFloat(0.16)},
|
||||
{User: user3, Sum: dec.NewFromFloat(0.16)},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
expectedDebtors := UserTabs{
|
||||
{User: user2, Sum: dec.NewFromFloat(43.0)},
|
||||
{User: user4, Sum: dec.NewFromFloat(11.0)},
|
||||
}
|
||||
for _, tt := range testCases {
|
||||
fmt.Printf("Running test: %s\n", tt.name)
|
||||
debtors, creditors := filterExpenseParticipants(tt.exp)
|
||||
|
||||
debtors, creditors := filterExpenseParticipants(&expense2)
|
||||
if !tt.expectedDebtors.Equal(debtors) {
|
||||
t.Errorf("Expected debtors:\n%+v\nGotten debtors:\n%+v\n",
|
||||
tt.expectedDebtors, debtors)
|
||||
}
|
||||
|
||||
if !expectedDebtors.Equal(debtors) {
|
||||
t.Errorf("Expected debtors:\n%+v\nGotten debtors:\n%+v\n", expectedDebtors,
|
||||
debtors)
|
||||
}
|
||||
|
||||
if !expectedCreditors.Equal(creditors) {
|
||||
t.Errorf("Expected creditors:\n%+v\nGotten creditors:\n%+v\n", expectedCreditors,
|
||||
creditors)
|
||||
if !tt.expectedCreditors.Equal(creditors) {
|
||||
t.Errorf("Expected creditors:\n%+v\nGotten creditors:\n%+v\n",
|
||||
tt.expectedCreditors, creditors)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,11 @@ package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
dec "github.com/shopspring/decimal"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func TestNormalizeShares(t *testing.T) {
|
||||
@@ -42,7 +45,6 @@ func TestNormalizeShares(t *testing.T) {
|
||||
|
||||
// dummy service to call it
|
||||
app := ExpensesApp{}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := app.NormalizeShares(tc.exp)
|
||||
@@ -58,3 +60,59 @@ func TestNormalizeShares(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMapShares(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
exp *mod.Expense
|
||||
expectedShares map[mod.User]dec.Decimal
|
||||
}{
|
||||
{
|
||||
name: "Map a share",
|
||||
exp: &expense6,
|
||||
expectedShares: map[mod.User]dec.Decimal{
|
||||
user1: dec.NewFromFloat(0.18),
|
||||
user2: dec.NewFromFloat(0.16),
|
||||
user3: dec.NewFromFloat(0.16),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
fmt.Printf("Running test: %s", tc.name)
|
||||
|
||||
shares := mapShares(tc.exp)
|
||||
if !reflect.DeepEqual(shares, tc.expectedShares) {
|
||||
t.Errorf("Resulting shares: %+v\nAre not correct: %+v", shares,
|
||||
tc.expectedShares)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapPayments(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
exp *mod.Expense
|
||||
expectedPayments map[mod.User]dec.Decimal
|
||||
}{
|
||||
{
|
||||
name: "Map a share",
|
||||
exp: &expense6,
|
||||
expectedPayments: map[mod.User]dec.Decimal{
|
||||
user1: dec.NewFromFloat(0.50),
|
||||
user2: dec.NewFromFloat(0.0),
|
||||
user3: dec.NewFromFloat(0.0),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
fmt.Printf("Running test: %s", tc.name)
|
||||
|
||||
payments := mapPayments(tc.exp)
|
||||
if !reflect.DeepEqual(payments, tc.expectedPayments) {
|
||||
t.Errorf("Resulting payments: %+v\nAre not correct: %+v", payments,
|
||||
tc.expectedPayments)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ func (a *ExpensesApp) DeleteExpense(id int64) error {
|
||||
func mapShares(e *mod.Expense) map[mod.User]decimal.Decimal {
|
||||
shares := make(map[mod.User]decimal.Decimal)
|
||||
for _, share := range e.Shares {
|
||||
shares[share.User] = share.Share
|
||||
shares[share.User] = share.Calculated
|
||||
}
|
||||
|
||||
return shares
|
||||
@@ -149,7 +149,6 @@ func ExpenseIsEvenlyShared(exp *mod.Expense) bool {
|
||||
payments := mapPayments(exp)
|
||||
|
||||
for user, share := range shares {
|
||||
userShare := share.Mul(exp.Value)
|
||||
val, userHasPayment := payments[user]
|
||||
|
||||
// If a user doesn't even have a payment but has a share, its not even
|
||||
@@ -157,7 +156,7 @@ func ExpenseIsEvenlyShared(exp *mod.Expense) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
if !val.Truncate(2).Equal(userShare.Truncate(2)) {
|
||||
if !val.Truncate(2).Equal(share.Truncate(2)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
func mapRepoGetSharesRow(s repo.GetSharesRow) Share {
|
||||
share, _ := decimal.NewFromString(s.ExpensesShare.Share)
|
||||
calculated, _ := decimal.NewFromString(s.ExpensesShare.Calculated)
|
||||
|
||||
return Share{
|
||||
ExpShareID: s.ExpensesShare.ExpShareID,
|
||||
@@ -16,6 +17,7 @@ func mapRepoGetSharesRow(s repo.GetSharesRow) Share {
|
||||
UserID: s.User.UserID,
|
||||
UserName: s.User.UserName,
|
||||
},
|
||||
Calculated: calculated,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user