From 8fe91293e1841a43e37a3e73cf61c6ebcd616b68 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Wed, 6 Apr 2022 22:16:50 +0000 Subject: [PATCH] Add unittests for month --- server/budgeting.go | 2 +- server/main_test.go | 9 ++++++ server/month.go | 55 +++++++++++++++++++++++++++++++++++ server/month_test.go | 46 ++++++++++++++++++++++++++++++ server/util.go | 68 -------------------------------------------- 5 files changed, 111 insertions(+), 69 deletions(-) create mode 100644 server/month.go create mode 100644 server/month_test.go diff --git a/server/budgeting.go b/server/budgeting.go index 1424f8d..5960a87 100644 --- a/server/budgeting.go +++ b/server/budgeting.go @@ -88,7 +88,7 @@ func (*Handler) getAvailableBalance(budget postgres.Budget, month Month, continue } - if !month.InPast(bal.Date) { + if month.InFuture(bal.Date) { continue } diff --git a/server/main_test.go b/server/main_test.go index 99fa6c6..00b31fc 100644 --- a/server/main_test.go +++ b/server/main_test.go @@ -245,6 +245,15 @@ func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, testCa }) } +func AssertEqualBool(t *testing.T, expected, actual bool, message string) { + t.Helper() + if expected == actual { + return + } + + t.Errorf("%s: expected %v, got %v", message, expected, actual) +} + func assertEqual(t *testing.T, expected, actual float64, message string) { t.Helper() if expected == actual { diff --git a/server/month.go b/server/month.go new file mode 100644 index 0000000..4d46f3e --- /dev/null +++ b/server/month.go @@ -0,0 +1,55 @@ +package server + +import ( + "fmt" + "time" +) + +type Month struct { + Year int + Month int +} + +func NewFromTime(date time.Time) Month { + return Month{date.Year(), int(date.Month())} +} + +func (m Month) String() string { + return fmt.Sprintf("%d-%d", m.Year, m.Month) +} + +func (m Month) FirstOfMonth() time.Time { + return time.Date(m.Year, time.Month(m.Month), 1, 0, 0, 0, 0, time.Now().Location()) +} + +func (m Month) InFuture(date time.Time) bool { + if m.Year < date.Year() { + return true + } + + if m.Year > date.Year() { + return false + } + + return time.Month(m.Month) < date.Month() +} + +func (m Month) InPast(date time.Time) bool { + if m.Year > date.Year() { + return true + } + + if m.Year < date.Year() { + return false + } + + return time.Month(m.Month) > date.Month() +} + +func (m Month) InPresent(date time.Time) bool { + if date.Year() != m.Year { + return false + } + + return date.Month() == time.Month(m.Month) +} diff --git a/server/month_test.go b/server/month_test.go new file mode 100644 index 0000000..1c56845 --- /dev/null +++ b/server/month_test.go @@ -0,0 +1,46 @@ +package server_test + +import ( + "testing" + "time" + + "git.javil.eu/jacob1123/budgeteer/server" +) + +type TestCaseCompare struct { + Value server.Month + Date time.Time + InPast bool + InPresent bool + InFuture bool +} + +func TestComparisons(t *testing.T) { + t.Parallel() + + loc := time.Now().Location() + tests := []TestCaseCompare{ + {server.Month{2022, 2}, time.Date(2022, 3, 1, 0, 0, 0, 0, loc), false, false, true}, + {server.Month{2022, 3}, time.Date(2022, 3, 1, 0, 0, 0, 0, loc), false, true, false}, + {server.Month{2022, 4}, time.Date(2022, 3, 1, 0, 0, 0, 0, loc), true, false, false}, + {server.Month{2022, 2}, time.Date(2022, 3, 31, 0, 0, 0, 0, loc), false, false, true}, + {server.Month{2022, 3}, time.Date(2022, 3, 31, 0, 0, 0, 0, loc), false, true, false}, + {server.Month{2022, 4}, time.Date(2022, 3, 31, 0, 0, 0, 0, loc), true, false, false}, + {server.Month{2021, 2}, time.Date(2022, 3, 31, 0, 0, 0, 0, loc), false, false, true}, + {server.Month{2021, 3}, time.Date(2022, 3, 31, 0, 0, 0, 0, loc), false, false, true}, + {server.Month{2021, 4}, time.Date(2022, 3, 31, 0, 0, 0, 0, loc), false, false, true}, + {server.Month{2023, 2}, time.Date(2022, 3, 31, 0, 0, 0, 0, loc), true, false, false}, + {server.Month{2023, 3}, time.Date(2022, 3, 31, 0, 0, 0, 0, loc), true, false, false}, + {server.Month{2023, 4}, time.Date(2022, 3, 31, 0, 0, 0, 0, loc), true, false, false}, + {server.Month{2021, 11}, time.Date(2021, 12, 1, 0, 0, 0, 0, loc), false, false, true}, + } + for i := range tests { //nolint:paralleltest + test := tests[i] + t.Run(test.Date.Format("2006-01-02")+" is in of "+test.Value.String(), func(t *testing.T) { + t.Parallel() + server.AssertEqualBool(t, test.InPast, test.Value.InPast(test.Date), "in past") + server.AssertEqualBool(t, test.InPresent, test.Value.InPresent(test.Date), "in present") + server.AssertEqualBool(t, test.InFuture, test.Value.InFuture(test.Date), "in future") + }) + } +} diff --git a/server/util.go b/server/util.go index 531a88e..34955b8 100644 --- a/server/util.go +++ b/server/util.go @@ -8,74 +8,6 @@ import ( "github.com/gin-gonic/gin" ) -type Month struct { - Year int - Month int -} - -func NewFromTime(date time.Time) Month { - return Month{date.Year(), int(date.Month())} -} - -func (m Month) String() string { - return fmt.Sprintf("%d-%d", m.Year, m.Month) -} - -func (m Month) FirstOfMonth() time.Time { - return time.Date(m.Year, time.Month(m.Month), 1, 0, 0, 0, 0, time.Now().Location()) -} - -func (m Month) nextMonth() Month { - if m.Month == 12 { - return Month{m.Year + 1, 1} - } - return Month{m.Year, m.Month} -} - -func (m Month) InFuture(date time.Time) bool { - if m.Year < date.Year() { - return true - } - - if m.Year > date.Year() { - return false - } - - return time.Month(m.Month) < date.Month() -} - -func (m Month) isBeforeOrContains(date time.Time) bool { - if m.Year < date.Year() { - return true - } - - if m.Year > date.Year() { - return false - } - - return time.Month(m.Month) <= date.Month() -} - -func (m Month) InPast(date time.Time) bool { - if m.Year > date.Year() { - return false - } - - if m.Year < date.Year() { - return true - } - - return time.Month(m.Month) > date.Month() -} - -func (m Month) InPresent(date time.Time) bool { - if date.Year() != m.Year { - return false - } - - return date.Month() == time.Month(m.Month) -} - func getDate(c *gin.Context) (Month, error) { var year, month int yearString := c.Param("year")