113 lines
2.0 KiB
Go
113 lines
2.0 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"spend-sparrow/types"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMoneyCalculation(t *testing.T) {
|
|
t.Parallel()
|
|
t.Run("should calculate correct oink balance", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
underTest := NewMoneyImpl()
|
|
|
|
// GIVEN
|
|
timestamp := time.Date(2020, 01, 01, 0, 0, 0, 0, time.UTC)
|
|
|
|
groupId := uuid.New()
|
|
|
|
account := types.Account{
|
|
Id: uuid.New(),
|
|
GroupId: groupId,
|
|
|
|
Type: "Bank",
|
|
Name: "Bank",
|
|
|
|
CurrentBalance: 0,
|
|
LastTransaction: time.Time{},
|
|
OinkBalance: 0,
|
|
}
|
|
|
|
// The PiggyBank is a fictional account. The money it "holds" is actually in the Account
|
|
piggyBank := types.PiggyBank{
|
|
Id: uuid.New(),
|
|
GroupId: groupId,
|
|
|
|
AccountId: account.Id,
|
|
Name: "Car",
|
|
|
|
CurrentBalance: 0,
|
|
}
|
|
|
|
savingsPlan := types.SavingsPlan{
|
|
Id: uuid.New(),
|
|
GroupId: groupId,
|
|
PiggyBankId: piggyBank.Id,
|
|
|
|
MonthlySaving: 10,
|
|
|
|
ValidFrom: timestamp,
|
|
}
|
|
|
|
transaction1 := types.Transaction{
|
|
Id: uuid.New(),
|
|
GroupId: groupId,
|
|
|
|
AccountId: account.Id,
|
|
|
|
Amount: 20,
|
|
Timestamp: timestamp,
|
|
}
|
|
|
|
transaction2 := types.Transaction{
|
|
Id: uuid.New(),
|
|
GroupId: groupId,
|
|
|
|
AccountId: account.Id,
|
|
PiggyBankId: &piggyBank.Id,
|
|
|
|
Amount: -1,
|
|
Timestamp: timestamp.Add(1 * time.Hour),
|
|
}
|
|
|
|
expected := []types.BalanceInTime{
|
|
{
|
|
Id: uuid.New(),
|
|
GroupId: groupId,
|
|
|
|
TranactionId: transaction1.Id,
|
|
AccountId: account.Id,
|
|
|
|
ValidFrom: timestamp,
|
|
Balance: 20,
|
|
OinkBalance: 10,
|
|
},
|
|
{
|
|
Id: uuid.New(),
|
|
GroupId: groupId,
|
|
|
|
TranactionId: transaction2.Id,
|
|
AccountId: account.Id,
|
|
PiggyBankId: piggyBank.Id,
|
|
|
|
ValidFrom: timestamp.Add(1 * time.Hour),
|
|
Balance: 19,
|
|
OinkBalance: 9,
|
|
},
|
|
}
|
|
|
|
// WHEN
|
|
actual, err := underTest.CalculateAllBalancesInTime(account, piggyBank, savingsPlan, []types.Transaction{transaction1, transaction2})
|
|
|
|
// THEN
|
|
assert.Nil(t, err)
|
|
assert.ElementsMatch(t, expected, actual)
|
|
})
|
|
}
|