~whereswaldon/ledger

8f3af36d34e234164de5d454e4f3a3cf3e37d086 — Chris Howey 1 year, 9 months ago f5c418b
test: date range coverage
3 files changed, 150 insertions(+), 6 deletions(-)

M balances_test.go
M date.go
M date_test.go
M balances_test.go => balances_test.go +48 -0
@@ 55,3 55,51 @@ func TestBalanceLedger(t *testing.T) {
		}
	}
}

func TestBalancesByPeriod(t *testing.T) {
	b := bytes.NewBufferString(`
2022/02/02 Payee
	Assets     50
	Income

2022/01/02 Payee
	Assets     50
	Income

2022/03/02 Payee
	Assets     50
	Income

2022/04/02 Payee
	Assets     50
	Income

2022/05/02 Payee
	Assets     50
	Income

`)

	trans, _ := ParseLedger(b)
	partitionRb := BalancesByPeriod(trans, PeriodQuarter, RangePartition)
	snapshotRb := BalancesByPeriod(trans, PeriodQuarter, RangeSnapshot)

	if partitionRb[len(partitionRb)-1].Balances[0].Balance.Abs().Cmp(decimal.NewFromInt(100)) != 0 {
		t.Error("range balance by partition not accurate")
	}
	if snapshotRb[len(snapshotRb)-1].Balances[0].Balance.Abs().Cmp(decimal.NewFromInt(250)) != 0 {
		t.Error("range balance by snapshot not accurate")
	}

	transPeriod := TransactionsByPeriod(trans, PeriodQuarter)
	lastBals := GetBalances(transPeriod[len(transPeriod)-1].Transactions, []string{})
	if partitionRb[len(partitionRb)-1].Balances[0].Balance.Abs().Cmp(lastBals[0].Balance.Abs()) != 0 {
		t.Error("range balance by partition not equal to trans by period balance")
	}

	var blanktrans []*Transaction
	rb := BalancesByPeriod(blanktrans, PeriodDay, RangeSnapshot)
	if len(rb) > 1 {
		t.Error("range balances for non-existent transactions")
	}
}

M date.go => date.go +0 -6
@@ 135,9 135,6 @@ func startEndTime(trans []*Transaction) (start, end time.Time) {
// TransactionsByPeriod will return the transactions for each period.
func TransactionsByPeriod(trans []*Transaction, per Period) []*RangeTransactions {
	var results []*RangeTransactions
	if len(trans) < 1 {
		return results
	}

	tStart, tEnd := startEndTime(trans)



@@ 166,9 163,6 @@ type RangeBalance struct {
// BalancesByPeriod will return the account balances for each period.
func BalancesByPeriod(trans []*Transaction, per Period, rType RangeType) []*RangeBalance {
	var results []*RangeBalance
	if len(trans) < 1 {
		return results
	}

	tStart, tEnd := startEndTime(trans)


M date_test.go => date_test.go +102 -0
@@ 24,6 24,72 @@ var boundCases = []boundCase{
		},
	},
	{
		PeriodSemiYear,
		time.Date(2019, time.March, 23, 0, 0, 0, 0, time.UTC),
		time.Date(2021, time.September, 23, 0, 0, 0, 0, time.UTC),
		[]time.Time{
			time.Date(2019, time.January, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.July, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2020, time.January, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2020, time.July, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2021, time.January, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2021, time.July, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2022, time.January, 01, 0, 0, 0, 0, time.UTC),
		},
	},
	{
		PeriodSemiYear,
		time.Date(2019, time.September, 23, 0, 0, 0, 0, time.UTC),
		time.Date(2021, time.September, 23, 0, 0, 0, 0, time.UTC),
		[]time.Time{
			time.Date(2019, time.July, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2020, time.January, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2020, time.July, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2021, time.January, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2021, time.July, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2022, time.January, 01, 0, 0, 0, 0, time.UTC),
		},
	},
	{
		PeriodDay,
		time.Date(2019, time.March, 23, 0, 0, 0, 0, time.UTC),
		time.Date(2019, time.March, 28, 0, 0, 0, 0, time.UTC),
		[]time.Time{
			time.Date(2019, time.March, 23, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.March, 24, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.March, 25, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.March, 26, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.March, 27, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.March, 28, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.March, 29, 0, 0, 0, 0, time.UTC),
		},
	},
	{
		PeriodWeek,
		time.Date(2019, time.March, 23, 0, 0, 0, 0, time.UTC),
		time.Date(2019, time.April, 23, 0, 0, 0, 0, time.UTC),
		[]time.Time{
			time.Date(2019, time.March, 17, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.March, 24, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.March, 31, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.April, 07, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.April, 14, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.April, 21, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.April, 28, 0, 0, 0, 0, time.UTC),
		},
	},
	{
		Period2Week,
		time.Date(2019, time.March, 23, 0, 0, 0, 0, time.UTC),
		time.Date(2019, time.April, 23, 0, 0, 0, 0, time.UTC),
		[]time.Time{
			time.Date(2019, time.March, 17, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.March, 31, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.April, 14, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.April, 28, 0, 0, 0, 0, time.UTC),
		},
	},
	{
		PeriodMonth,
		time.Date(2019, time.March, 23, 0, 0, 0, 0, time.UTC),
		time.Date(2019, time.April, 23, 0, 0, 0, 0, time.UTC),


@@ 34,6 100,42 @@ var boundCases = []boundCase{
		},
	},
	{
		Period2Month,
		time.Date(2019, time.March, 23, 0, 0, 0, 0, time.UTC),
		time.Date(2019, time.April, 23, 0, 0, 0, 0, time.UTC),
		[]time.Time{
			time.Date(2019, time.March, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.May, 01, 0, 0, 0, 0, time.UTC),
		},
	},
	{
		PeriodQuarter,
		time.Date(2019, time.April, 23, 0, 0, 0, 0, time.UTC),
		time.Date(2019, time.May, 23, 0, 0, 0, 0, time.UTC),
		[]time.Time{
			time.Date(2019, time.April, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.July, 01, 0, 0, 0, 0, time.UTC),
		},
	},
	{
		PeriodQuarter,
		time.Date(2019, time.July, 23, 0, 0, 0, 0, time.UTC),
		time.Date(2019, time.August, 23, 0, 0, 0, 0, time.UTC),
		[]time.Time{
			time.Date(2019, time.July, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2019, time.October, 01, 0, 0, 0, 0, time.UTC),
		},
	},
	{
		PeriodQuarter,
		time.Date(2019, time.October, 23, 0, 0, 0, 0, time.UTC),
		time.Date(2019, time.November, 23, 0, 0, 0, 0, time.UTC),
		[]time.Time{
			time.Date(2019, time.October, 01, 0, 0, 0, 0, time.UTC),
			time.Date(2020, time.January, 01, 0, 0, 0, 0, time.UTC),
		},
	},
	{
		Period("Unknown"),
		time.Date(2019, time.March, 23, 0, 0, 0, 0, time.UTC),
		time.Date(2019, time.April, 23, 0, 0, 0, 0, time.UTC),