From 7183685261de094eea5b423abf3938109a9c9736 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Sun, 10 Nov 2024 14:23:46 +0100 Subject: [PATCH] fixing lint issues --- .github/workflows/go.yml | 2 +- .github/workflows/golangci-lint.yml | 31 ++ .golangci.yml | 6 + LICENSE | 21 - README.md | 29 +- go.mod | 8 - pkg/timeperiod/timeperiod.go | 106 ++--- pkg/timeperiod/timeperiods_test.go | 715 +++++++++++++++------------- 8 files changed, 499 insertions(+), 419 deletions(-) create mode 100644 .github/workflows/golangci-lint.yml create mode 100644 .golangci.yml delete mode 100644 LICENSE diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index a99ff70..b024b31 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -6,7 +6,7 @@ name: Go on: workflow_dispatch: push: - branches: [ "main" ] + branches: [ "main", "dev" ] pull_request: branches: [ "main" ] diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 0000000..9110d4c --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,31 @@ +name: golangci-lint +on: + workflow_dispatch: + push: + branches: + - main + - dev + pull_request: + +permissions: + contents: read + # Optional: allow read access to pull request. Use with `only-new-issues` option. + # pull-requests: read + +jobs: + golangci: + strategy: + matrix: + go: [stable] + os: [ubuntu-latest, macos-latest, windows-latest] + name: lint + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go }} + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.60 \ No newline at end of file diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..434cdda --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,6 @@ +issues: + exclude-rules: + # disable funlen for all _test.go files + - path: _test.go + linters: + - funlen \ No newline at end of file diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 839e710..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2024 Manuel Doncel Martos - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/README.md b/README.md index dc77ff6..d5d3c16 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,12 @@ [![Go](https://github.com/manuelarte/GoTime/actions/workflows/go.yml/badge.svg)](https://github.com/manuelarte/GoTime/actions/workflows/go.yml) ![coverage](https://raw.githubusercontent.com/manuelarte/GoTime/badges/.badges/main/coverage.svg) -# GoTime +# 🕐 GoTime 🕐 -## Introduction +## 📝 How to install it + +> go get github.com/manuelarte/GoTime + +## ✏️ Introduction GoTime contains the following utility struct @@ -10,5 +14,24 @@ GoTime contains the following utility struct Construct a time period based on start time and end time. -> timePeriod, err := NewTimePeriod(startTime, endTime) +> tp, err := NewTimePeriod(startTime, endTime) + +The time period is built based on the overlapping period between the two dates. + +``` +t1 ____|________ +t2 _________| +tp ____|‾‾‾‾|___ +``` + +It also provides a function `Overlaps` to check whether the two time periods overlaps, and what's the overlapping period + +e.g. + +``` +tp1 ____|‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +tp2 _________|‾‾‾‾‾‾|__ +tp ____|‾‾‾‾|_________ +``` +For more information check the [examples](./examples) diff --git a/go.mod b/go.mod index 0d25c2f..cc1e109 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,3 @@ module GoTime go 1.22 - -require github.com/stretchr/testify v1.9.0 - -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) diff --git a/pkg/timeperiod/timeperiod.go b/pkg/timeperiod/timeperiod.go index 81445f8..fb8a907 100644 --- a/pkg/timeperiod/timeperiod.go +++ b/pkg/timeperiod/timeperiod.go @@ -8,92 +8,92 @@ import ( var ErrEndTimeBeforeStartTime = errors.New("end time before start time") -// TimePeriod struct to track a Period of Time. It's composed of a StartTime and an EndTime -// If StartTime is zero then it means the beginning of time -// If EndTime is zero then it means no limit -type TimePeriod interface { - GetStartTime() time.Time - GetEndTime() time.Time - // Overlaps Returns the overlap period between the two time periods, and the boolean wheter it overlaps or not - Overlaps(other TimePeriod) (TimePeriod, bool) - // GetDuration Get the duration - GetDuration() time.Duration -} - // NewTimePeriod Creates new time period based on a start time and an end time -// Returns either the time period of an error is the end time is before the start time +// Returns either the time period of an error is the end time is before the start time. func NewTimePeriod(startTime time.Time, endTime time.Time) (TimePeriod, error) { if (!startTime.IsZero() && !endTime.IsZero()) && endTime.Before(startTime) { - return timePeriodImpl{}, ErrEndTimeBeforeStartTime + return TimePeriod{}, ErrEndTimeBeforeStartTime } - return timePeriodImpl{ - StartTime: startTime, - EndTime: endTime, + + return TimePeriod{ + startTime: startTime, + endTime: endTime, }, nil } -var _ TimePeriod = new(timePeriodImpl) - -type timePeriodImpl struct { - StartTime time.Time - EndTime time.Time +// TimePeriod struct to track a Period of Time. It's composed of a StartTime and an EndTime +// If StartTime is zero then it means the beginning of time +// If EndTime is zero then it means no limit. +type TimePeriod struct { + startTime time.Time + endTime time.Time } -func (timePeriod timePeriodImpl) GetStartTime() time.Time { - return timePeriod.StartTime +func (tp TimePeriod) GetStartTime() time.Time { + return tp.startTime } -func (timePeriod timePeriodImpl) GetEndTime() time.Time { - return timePeriod.EndTime +func (tp TimePeriod) GetEndTime() time.Time { + return tp.endTime } -func (timePeriod timePeriodImpl) GetDuration() time.Duration { - if timePeriod.StartTime.IsZero() || timePeriod.EndTime.IsZero() { +// GetDuration Get the duration. +func (tp TimePeriod) GetDuration() time.Duration { + if tp.startTime.IsZero() || tp.endTime.IsZero() { // return maxDuration return 1<<63 - 1 } - return timePeriod.EndTime.Sub(timePeriod.StartTime) + + return tp.endTime.Sub(tp.startTime) +} + +// Overlaps Returns the overlap period between the two time periods, and the boolean wheter it overlaps or not +func (tp TimePeriod) Overlaps(other TimePeriod) (TimePeriod, bool) { + if tp.doesIntersect(other) { + return tp.intersect(other), true + } + + return TimePeriod{startTime: time.Time{}, endTime: time.Time{}}, false } -func (timePeriod timePeriodImpl) doesIntersect(comparePeriod TimePeriod) bool { - if timePeriod.EndTime.IsZero() && comparePeriod.GetEndTime().IsZero() { +func (tp TimePeriod) doesIntersect(comparePeriod TimePeriod) bool { + if tp.endTime.IsZero() && comparePeriod.GetEndTime().IsZero() { return true } - if comparePeriod.GetEndTime().IsZero() && comparePeriod.GetStartTime().UTC().After(timePeriod.EndTime.UTC()) { + if comparePeriod.GetEndTime().IsZero() && comparePeriod.GetStartTime().UTC().After(tp.endTime.UTC()) { return false } - if !timePeriod.EndTime.IsZero() && (timePeriod.EndTime.UTC().Before(comparePeriod.GetStartTime().UTC()) || timePeriod.EndTime.UTC() == comparePeriod.GetStartTime().UTC()) { + if !tp.endTime.IsZero() && (tp.endTime.UTC().Before(comparePeriod.GetStartTime().UTC()) || + tp.endTime.UTC() == comparePeriod.GetStartTime().UTC()) { return false } - if !comparePeriod.GetEndTime().IsZero() && (timePeriod.StartTime.UTC().After(comparePeriod.GetEndTime().UTC()) || timePeriod.StartTime.UTC() == comparePeriod.GetEndTime().UTC()) { + if !comparePeriod.GetEndTime().IsZero() && (tp.startTime.UTC().After(comparePeriod.GetEndTime().UTC()) || + tp.startTime.UTC() == comparePeriod.GetEndTime().UTC()) { return false } + return true } -func (timePeriod timePeriodImpl) intersect(comparePeriod TimePeriod) TimePeriod { - if !timePeriod.doesIntersect(comparePeriod) { - return timePeriodImpl{ - StartTime: time.Time{}, - EndTime: time.Time{}, +func (tp TimePeriod) intersect(comparePeriod TimePeriod) TimePeriod { + if !tp.doesIntersect(comparePeriod) { + return TimePeriod{ + startTime: time.Time{}, + endTime: time.Time{}, } } - intersectPeriod := timePeriod - if timePeriod.StartTime.UTC().Before(comparePeriod.GetStartTime().UTC()) { - intersectPeriod.StartTime = comparePeriod.GetStartTime() + + intersectPeriod := tp + + if tp.startTime.UTC().Before(comparePeriod.GetStartTime().UTC()) { + intersectPeriod.startTime = comparePeriod.GetStartTime() } - if !comparePeriod.GetEndTime().IsZero() && !timePeriod.EndTime.IsZero() && timePeriod.EndTime.UTC().After(comparePeriod.GetEndTime().UTC()) { - intersectPeriod.EndTime = comparePeriod.GetEndTime() + if !comparePeriod.GetEndTime().IsZero() && !tp.endTime.IsZero() && tp.endTime.UTC().After(comparePeriod.GetEndTime().UTC()) { + intersectPeriod.endTime = comparePeriod.GetEndTime() } - if timePeriod.EndTime.IsZero() { - intersectPeriod.EndTime = comparePeriod.GetEndTime() + if tp.endTime.IsZero() { + intersectPeriod.endTime = comparePeriod.GetEndTime() } - return intersectPeriod -} -func (timePeriod timePeriodImpl) Overlaps(other TimePeriod) (TimePeriod, bool) { - if timePeriod.doesIntersect(other) { - return timePeriod.intersect(other), true - } - return timePeriodImpl{}, false + return intersectPeriod } diff --git a/pkg/timeperiod/timeperiods_test.go b/pkg/timeperiod/timeperiods_test.go index 85e8fef..01326e8 100644 --- a/pkg/timeperiod/timeperiods_test.go +++ b/pkg/timeperiod/timeperiods_test.go @@ -1,287 +1,304 @@ package timeperiod import ( + "errors" "testing" "time" - - "github.com/stretchr/testify/assert" ) func TestTimePeriod_GetDuration(t *testing.T) { - // Arrange + t.Parallel() tests := map[string]struct { - timePeriod TimePeriod - expectedHours time.Duration + timePeriod TimePeriod + expected time.Duration }{ "One hour": { - timePeriod: timePeriodImpl{ - StartTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), - EndTime: time.Date(2022, 1, 1, 13, 0, 0, 0, time.UTC), + timePeriod: TimePeriod{ + startTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), + endTime: time.Date(2022, 1, 1, 13, 0, 0, 0, time.UTC), }, - expectedHours: 1 * time.Hour, + expected: 1 * time.Hour, }, "One day": { - timePeriod: timePeriodImpl{ - StartTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), - EndTime: time.Date(2022, 1, 2, 12, 0, 0, 0, time.UTC), + timePeriod: TimePeriod{ + startTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), + endTime: time.Date(2022, 1, 2, 12, 0, 0, 0, time.UTC), }, - expectedHours: 24 * time.Hour, + expected: 24 * time.Hour, }, "No end, max duration": { - timePeriod: timePeriodImpl{ - StartTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), + timePeriod: TimePeriod{ + startTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), + endTime: time.Time{}, }, - expectedHours: 1<<63 - 1, + expected: 1<<63 - 1, }, "Less than one hour": { - timePeriod: timePeriodImpl{ - StartTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), - EndTime: time.Date(2022, 1, 1, 12, 59, 0, 0, time.UTC), + timePeriod: TimePeriod{ + startTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), + endTime: time.Date(2022, 1, 1, 12, 59, 0, 0, time.UTC), }, - expectedHours: 59 * time.Minute, + expected: 59 * time.Minute, }, } for name, test := range tests { t.Run(name, func(t *testing.T) { - // Act - actualHours := test.timePeriod.GetDuration() + t.Parallel() + actual := test.timePeriod.GetDuration() - // Assert - assert.Equal(t, test.expectedHours, actualHours) + if test.expected != actual { + t.Errorf("\nExpected: %v\nActual: %v", test.expected, actual) + } }) } } func TestTimePeriod_DoesIntersect(t *testing.T) { - // Arrange + t.Parallel() tests := map[string]struct { - basePeriod timePeriodImpl + basePeriod TimePeriod comparePeriod TimePeriod expectedResult bool }{ "Base Period is exactly the same as Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, expectedResult: true, }, "Base Period falls inside Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: yearMonthDay(2023, 4, 1), }, expectedResult: true, }, "Base Period contains Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 10, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 2, 20, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 10), + endTime: yearMonthDay(2023, 2, 20), }, expectedResult: true, }, "Base Period overlaps first part of Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 15, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 15, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 15), + endTime: yearMonthDay(2023, 3, 15), }, expectedResult: true, }, "Base Period overlaps last part of Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 15, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 2, 15, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 15), + endTime: yearMonthDay(2023, 2, 15), }, expectedResult: true, }, "Base Period is before Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 5, 1, 0, 0, 0, 0, time.UTC)), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 4, 1), + endTime: yearMonthDay(2023, 5, 1), }, expectedResult: false, }, "Base Period is after Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 1, 20, 0, 0, 0, 0, time.UTC)), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: yearMonthDay(2023, 1, 20), }, expectedResult: false, }, "Base Period ends on start of Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC)), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 3, 1), + endTime: yearMonthDay(2023, 4, 1), }, expectedResult: false, }, "Base Period starts on end of Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC)), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: yearMonthDay(2023, 2, 1), }, expectedResult: false, }, "Compare Period has no end time and starts before Base Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: time.Time{}, }, expectedResult: true, }, "Compare Period has no end time and starts on Base Period start": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, expectedResult: true, }, "Compare Period has no end time and starts inside Base Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 15, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 15), + endTime: time.Time{}, }, expectedResult: true, }, "Compare Period has no end time and starts on Base Period end": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 3, 1), + endTime: time.Time{}, }, expectedResult: false, }, "Compare Period has no end time and starts after Base Period end": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 4, 1), + endTime: time.Time{}, }, expectedResult: false, }, "Base Period has no end time and starts before Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC)), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 3, 1), + endTime: yearMonthDay(2023, 4, 1), }, expectedResult: true, }, "Base Period has no end time and starts on Compare Period start": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, expectedResult: true, }, "Base Period has no end time and starts inside Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: yearMonthDay(2023, 3, 1), }, expectedResult: true, }, "Base Period has no end time and starts on Compare Period end": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC)), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: yearMonthDay(2023, 2, 1), }, expectedResult: false, }, "Base Period has no end time and starts after Compare Period end": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 3, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC)), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: yearMonthDay(2023, 2, 1), }, expectedResult: false, }, "Base Period and Compare Period have no end times and Base starts before Compare start": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 3, 1), + endTime: time.Time{}, }, expectedResult: true, }, "Base Period and Compare Period have no end times and Base starts on Compare start": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, expectedResult: true, }, "Base Period and Compare Period have no end times and Base starts after Compare start": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: time.Time{}, }, expectedResult: true, }, @@ -289,349 +306,376 @@ func TestTimePeriod_DoesIntersect(t *testing.T) { for name, test := range tests { t.Run(name, func(t *testing.T) { - // Act + t.Parallel() actualResult := test.basePeriod.doesIntersect(test.comparePeriod) - - // Assert - assert.Equal(t, test.expectedResult, actualResult) + if test.expectedResult != actualResult { + t.Errorf("Test %v: Expected %v but got %v", name, test.expectedResult, actualResult) + } }) } } func TestTimePeriod_Intersect(t *testing.T) { - // Arrange + t.Parallel() tests := map[string]struct { - basePeriod timePeriodImpl + basePeriod TimePeriod comparePeriod TimePeriod expectedPeriod TimePeriod }{ "Base Period is exactly the same as Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, }, "Base Period falls inside Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: yearMonthDay(2023, 4, 1), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, }, "Base Period contains Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 10, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 2, 20, 0, 0, 0, 0, time.UTC)), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 10), + endTime: yearMonthDay(2023, 2, 20), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 10, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 2, 20, 0, 0, 0, 0, time.UTC), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 10), + endTime: yearMonthDay(2023, 2, 20), }, }, "Base Period overlaps first part of Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 15, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 15, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 15), + endTime: yearMonthDay(2023, 3, 15), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 15, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 15), + endTime: yearMonthDay(2023, 3, 1), }, }, "Base Period overlaps last part of Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 15, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 2, 15, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 15), + endTime: yearMonthDay(2023, 2, 15), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 2, 15, 0, 0, 0, 0, time.UTC), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 2, 15), }, }, "Base Period is before Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 5, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 4, 1), + endTime: yearMonthDay(2023, 5, 1), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Time{}, + expectedPeriod: TimePeriod{ + startTime: time.Time{}, + endTime: time.Time{}, }, }, "Base Period is after Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 1, 20, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: yearMonthDay(2023, 1, 20), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Time{}, + expectedPeriod: TimePeriod{ + startTime: time.Time{}, + endTime: time.Time{}, }, }, "Base Period ends on start of Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 3, 1), + endTime: yearMonthDay(2023, 4, 1), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Time{}, + expectedPeriod: TimePeriod{ + startTime: time.Time{}, + endTime: time.Time{}, }, }, "Base Period starts on end of Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: yearMonthDay(2023, 2, 1), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Time{}, + expectedPeriod: TimePeriod{ + startTime: time.Time{}, + endTime: time.Time{}, }, }, "Compare Period has no end time and starts before Base Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: time.Time{}, }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, }, "Compare Period has no end time and starts on Base Period start": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, }, "Compare Period has no end time and starts inside Base Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 15, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 15), + endTime: time.Time{}, }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 15, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 15), + endTime: yearMonthDay(2023, 3, 1), }, }, "Compare Period has no end time and starts on Base Period end": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 3, 1), + endTime: time.Time{}, }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Time{}, + expectedPeriod: TimePeriod{ + startTime: time.Time{}, + endTime: time.Time{}, }, }, "Compare Period has no end time and starts after Base Period end": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 4, 1), + endTime: time.Time{}, }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Time{}, + expectedPeriod: TimePeriod{ + startTime: time.Time{}, + endTime: time.Time{}, }, }, "Base Period has no end time and starts before Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 3, 1), + endTime: yearMonthDay(2023, 4, 1), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 3, 1), + endTime: yearMonthDay(2023, 4, 1), }, }, "Base Period has no end time and starts on Compare Period start": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, }, "Base Period has no end time and starts inside Compare Period": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), - EndTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: yearMonthDay(2023, 3, 1), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC)), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: yearMonthDay(2023, 3, 1), }, }, "Base Period has no end time and starts on Compare Period end": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC)), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: yearMonthDay(2023, 2, 1), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Time{}, + expectedPeriod: TimePeriod{ + startTime: time.Time{}, + endTime: time.Time{}, }, }, "Base Period has no end time and starts after Compare Period end": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 3, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), - EndTime: (time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC)), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: yearMonthDay(2023, 2, 1), }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Time{}, + expectedPeriod: TimePeriod{ + startTime: time.Time{}, + endTime: time.Time{}, }, }, "Base Period and Compare Period have no end times and Base starts before Compare start": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 3, 1), + endTime: time.Time{}, }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 3, 1), + endTime: time.Time{}, }, }, "Base Period and Compare Period have no end times and Base starts on Compare start": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, }, "Base Period and Compare Period have no end times and Base starts after Compare start": { - basePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + basePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, - comparePeriod: timePeriodImpl{ - StartTime: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), + comparePeriod: TimePeriod{ + startTime: yearMonthDay(2023, 1, 1), + endTime: time.Time{}, }, - expectedPeriod: timePeriodImpl{ - StartTime: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC), + expectedPeriod: TimePeriod{ + startTime: yearMonthDay(2023, 2, 1), + endTime: time.Time{}, }, }, } for name, test := range tests { t.Run(name, func(t *testing.T) { - // Act + t.Parallel() actualResult := test.basePeriod.intersect(test.comparePeriod) - - // Assert - assert.Equal(t, test.expectedPeriod, actualResult) + if test.expectedPeriod != actualResult { + t.Errorf("Expected: %v, Actual: %v", test.expectedPeriod, actualResult) + } }) } } func TestTimePeriod_NewTimePeriod(t *testing.T) { - // Arrange + t.Parallel() tests := map[string]struct { startTime time.Time endTime time.Time expectedErr error }{ - "EndTime after StartTime": { + "endTime after startTime": { startTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), endTime: time.Date(2022, 1, 1, 13, 0, 0, 0, time.UTC), expectedErr: nil, }, - "EndTime before StartTime": { + "endTime before startTime": { startTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), endTime: time.Date(2022, 1, 1, 10, 0, 0, 0, time.UTC), expectedErr: ErrEndTimeBeforeStartTime, }, - "EndTime equal to StartTime": { + "endTime equal to startTime": { startTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), endTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), expectedErr: nil, }, - "No EndTime": { + "No endTime": { startTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), endTime: time.Time{}, expectedErr: nil, }, - "No StartTime": { + "No startTime": { startTime: time.Time{}, endTime: time.Date(2022, 1, 1, 12, 0, 0, 0, time.UTC), expectedErr: nil, }, - "No Start nor EndTime": { + "No Start nor endTime": { startTime: time.Time{}, endTime: time.Time{}, expectedErr: nil, @@ -639,11 +683,16 @@ func TestTimePeriod_NewTimePeriod(t *testing.T) { } for name, test := range tests { t.Run(name, func(t *testing.T) { - // Act + t.Parallel() _, err := NewTimePeriod(test.startTime, test.endTime) - // Assert - assert.Equal(t, test.expectedErr, err) + if !errors.Is(test.expectedErr, err) { + t.Errorf("Expected: %v, Actual: %v", test.expectedErr, err) + } }) } } + +func yearMonthDay(year int, month time.Month, day int) time.Time { + return time.Date(year, month, day, 0, 0, 0, 0, time.UTC) +}