Skip to content

Commit

Permalink
feature: fix end of week period and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Samox committed May 11, 2023
1 parent e19ea55 commit 56a2eac
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
4 changes: 2 additions & 2 deletions charts/sort_filter_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func getFirstDateOfPeriod(periodKey string) time.Time {
case common.Day:
lastDay, _ = time.Parse("2006-01-02", periodKey)
case common.Week:
periodTime, _ := time.Parse("2006-W01", periodKey)
lastDay = periodTime.AddDate(0, 0, (7 - int(periodTime.Weekday()))).Add(time.Duration(23)*time.Hour + time.Duration(59)*time.Minute + time.Duration(59)*time.Second)
periodTime, _ := reports.ParseYearWeek(periodKey)
lastDay = periodTime.AddDate(0, 0, 6).Add(time.Duration(23)*time.Hour + time.Duration(59)*time.Minute + time.Duration(59)*time.Second)
case common.Month:
periodTime, _ := time.Parse("2006-01", periodKey)

Expand Down
56 changes: 56 additions & 0 deletions charts/sort_filter_events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package charts

import (
"testing"
"time"
)

func TestGetFirstDateOfPeriod_Day(t *testing.T) {
expected := time.Date(2023, time.May, 1, 0, 0, 0, 0, time.UTC)
result := getFirstDateOfPeriod("2023-05-01")
if !result.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestGetFirstDateOfPeriod_Week(t *testing.T) {
expected := time.Date(2023, time.April, 30, 23, 59, 59, 0, time.UTC)
result := getFirstDateOfPeriod("2023-W17")
if !result.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestGetFirstDateOfPeriod_Month(t *testing.T) {
expected := time.Date(2023, time.May, 31, 23, 59, 59, 0, time.UTC)
result := getFirstDateOfPeriod("2023-05")
if !result.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestGetFirstDateOfPeriod_Quarter(t *testing.T) {
expected := time.Date(2023, time.March, 31, 0, 0, 0, 0, time.UTC)
result := getFirstDateOfPeriod("2023-Q1")
if !result.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestGetFirstDateOfPeriod_Year(t *testing.T) {
expected := time.Date(2023, time.December, 31, 23, 59, 59, 0, time.UTC)
result := getFirstDateOfPeriod("2023")
if !result.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestGetFirstDateOfPeriod_Invalid(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected function to panic, but it didn't")
}
}()

getFirstDateOfPeriod("invalid-grain")
}
22 changes: 21 additions & 1 deletion reports/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func GetTimeGrain(periodKey string) (common.TimeGrain, error) {
if err == nil {
return common.Day, nil
}
_, err = time.Parse("2006-W01", periodKey)
_, err = ParseYearWeek(periodKey)
if err == nil {
return common.Week, nil
}
Expand All @@ -248,6 +248,26 @@ func GetTimeGrain(periodKey string) (common.TimeGrain, error) {
return "", fmt.Errorf("invalid period key: %s", periodKey)
}

func ParseYearWeek(yearWeek string) (time.Time, error) {
if len(yearWeek) != 8 {
return time.Time{}, fmt.Errorf("invalid year week format: %s", yearWeek)
}
year, err := strconv.Atoi(yearWeek[0:4])
if err != nil {
return time.Time{}, err
}

week, err := strconv.Atoi(yearWeek[6:])
if err != nil {
return time.Time{}, err
}

// Get the first day of the week (Monday)
firstDay := time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC).AddDate(0, 0, 7*(week-1)+1)

return firstDay, nil
}

func ParseQuarterDate(s string) (time.Time, error) {
parts := strings.Split(s, "-")
if len(parts) != 2 {
Expand Down
20 changes: 20 additions & 0 deletions reports/reports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package reports

import (
"testing"
"time"
)

func TestParseYearWeek(t *testing.T) {
yearWeek := "2023-W17" // Monday 24 April to Sunday 30 April 2023
expectedFirstDay := time.Date(2023, time.April, 24, 0, 0, 0, 0, time.UTC)

firstDay, err := ParseYearWeek(yearWeek)
if err != nil {
t.Errorf("Error parsing year week: %v", err)
}

if !firstDay.Equal(expectedFirstDay) {
t.Errorf("Expected first day of week for %s to be %v, but got %v", yearWeek, expectedFirstDay, firstDay)
}
}

0 comments on commit 56a2eac

Please sign in to comment.