From 188bd054e5cf5abc19483d962cc932b2be892635 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Sat, 31 Dec 2022 10:19:44 -0900 Subject: [PATCH] fix test error --- .../data/repo/repo_maintenance_entry_test.go | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/backend/internal/data/repo/repo_maintenance_entry_test.go b/backend/internal/data/repo/repo_maintenance_entry_test.go index 8babefc..e3df3d0 100644 --- a/backend/internal/data/repo/repo_maintenance_entry_test.go +++ b/backend/internal/data/repo/repo_maintenance_entry_test.go @@ -8,14 +8,35 @@ import ( "github.com/stretchr/testify/assert" ) +// get the previous month from the current month, accounts for errors when run +// near the beginning or end of the month/year +func getPrevMonth(now time.Time) time.Time { + t := now.AddDate(0, -1, 0) + + // avoid infinite loop + max := 15 + for t.Month() == now.Month() { + println("month is the same") + t = t.AddDate(0, 0, -1) + println(t.String()) + + max-- + if max == 0 { + panic("max exceeded") + } + } + + return t +} + func TestMaintenanceEntryRepository_GetLog(t *testing.T) { item := useItems(t, 1)[0] // Create 10 maintenance entries for the item created := make([]MaintenanceEntryCreate, 10) - lastMonth := time.Now().AddDate(0, -1, 0) thisMonth := time.Now() + lastMonth := getPrevMonth(thisMonth) for i := 0; i < 10; i++ { dt := lastMonth