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