-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: fix end of week period and add tests
- Loading branch information
Showing
4 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |