mirror of
https://github.com/taigrr/most-specific-period.git
synced 2026-04-01 19:28:41 -07:00
Use a struct instead of string slices
This commit is contained in:
@@ -26,81 +26,137 @@ func TestGetChangeOvers(t *testing.T) {
|
||||
testID string
|
||||
result []time.Time
|
||||
periods []Period
|
||||
}{{testID: "No choices",
|
||||
ts: now,
|
||||
result: []time.Time{},
|
||||
periods: []Period{}},
|
||||
{testID: "Two Choices, shorter is second",
|
||||
}{
|
||||
{
|
||||
testID: "No choices",
|
||||
ts: now,
|
||||
result: []time.Time{},
|
||||
periods: []Period{},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, shorter is second",
|
||||
ts: now,
|
||||
result: []time.Time{now.Add(-5 * time.Minute), now.Add(-2 * time.Minute), now.Add(time.Minute)},
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, one is a year, other a minute",
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one is a year, other a minute",
|
||||
ts: now,
|
||||
result: []time.Time{now.Add(-1 * time.Hour * 24 * 365), now.Add(-5 * time.Minute), now.Add(time.Minute)},
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-1 * time.Hour * 24 * 365),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-1 * time.Hour * 24 * 365),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
{testID: "Two Choices, shorter is first",
|
||||
{
|
||||
testID: "Two Choices, shorter is first",
|
||||
ts: now,
|
||||
result: []time.Time{now.Add(-5 * time.Minute), now.Add(-2 * time.Minute), now.Add(time.Minute)},
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, one in the past",
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one in the past",
|
||||
ts: now,
|
||||
result: []time.Time{now.Add(-2 * time.Minute), now.Add(-time.Minute), now.Add(time.Minute)},
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, one invalid",
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one invalid",
|
||||
ts: now,
|
||||
result: []time.Time{now.Add(-2 * time.Minute), now.Add(time.Minute)},
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(time.Minute),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(time.Minute),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, Identical periods",
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, Identical periods",
|
||||
ts: now,
|
||||
result: []time.Time{now.Add(-time.Minute), now.Add(time.Minute)},
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "One choice",
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "One choice",
|
||||
ts: now,
|
||||
result: []time.Time{now.Add(-time.Minute), now.Add(time.Minute)},
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
periods: []Period{TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"}}}}
|
||||
Identifier: "A",
|
||||
}},
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%s", tc.testID), func(t *testing.T) {
|
||||
changeovers := GetChangeOvers(tc.periods...)
|
||||
if !slicesEqual(changeovers, tc.result) {
|
||||
t.Errorf("Expected %v but got %v", tc.result, changeovers)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlattenPeriods(t *testing.T) {
|
||||
// use a static timestamp to make sure tests don't fail on slower systems or during a process pause
|
||||
now := time.Now()
|
||||
@@ -110,92 +166,158 @@ func TestFlattenPeriods(t *testing.T) {
|
||||
result []string
|
||||
err error
|
||||
periods []Period
|
||||
}{{testID: "No choices",
|
||||
ts: now,
|
||||
result: []string{},
|
||||
err: ErrNoValidPeriods,
|
||||
periods: []Period{}},
|
||||
{testID: "Two Choices, shorter is second",
|
||||
}{
|
||||
{
|
||||
testID: "No choices",
|
||||
ts: now,
|
||||
result: []string{},
|
||||
err: ErrNoValidPeriods,
|
||||
periods: []Period{},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, shorter is second",
|
||||
ts: now,
|
||||
result: []string{"A", "B"},
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, one is a year, other a minute",
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one is a year, other a minute",
|
||||
ts: now,
|
||||
result: []string{"A", "B"},
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-1 * time.Hour * 24 * 365),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-1 * time.Hour * 24 * 365),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
{testID: "Two Choices, shorter is first",
|
||||
{
|
||||
testID: "Two Choices, shorter is first",
|
||||
ts: now,
|
||||
result: []string{"B", "A"},
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, one in the past",
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one in the past",
|
||||
ts: now,
|
||||
result: []string{"B", "A"},
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, one invalid",
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one invalid",
|
||||
ts: now,
|
||||
result: []string{"B"},
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(time.Minute),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(time.Minute),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, Identical periods",
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, Identical periods",
|
||||
ts: now,
|
||||
result: []string{"B"},
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Triple Nested Periods",
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Triple Nested Periods",
|
||||
ts: now,
|
||||
result: []string{"A", "B", "C", "B", "A"},
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-15 * time.Minute),
|
||||
EndTime: now.Add(15 * time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-15 * time.Minute),
|
||||
EndTime: now.Add(15 * time.Minute),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(5 * time.Minute),
|
||||
Identifier: "C"},
|
||||
TimeWindow{StartTime: now.Add(-10 * time.Minute),
|
||||
Identifier: "C",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-10 * time.Minute),
|
||||
EndTime: now.Add(10 * time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "One choice",
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "One choice",
|
||||
ts: now,
|
||||
result: []string{"A"},
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
periods: []Period{TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"}}}}
|
||||
Identifier: "A",
|
||||
}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%s", tc.testID), func(t *testing.T) {
|
||||
@@ -203,10 +325,10 @@ func TestFlattenPeriods(t *testing.T) {
|
||||
if !slicesEqual(changeovers, tc.result) {
|
||||
t.Errorf("Expected %v but got %v", tc.result, changeovers)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetNextChangeOver(t *testing.T) {
|
||||
// use a static timestamp to make sure tests don't fail on slower systems or during a process pause
|
||||
now := time.Now()
|
||||
@@ -216,79 +338,135 @@ func TestGetNextChangeOver(t *testing.T) {
|
||||
result time.Time
|
||||
err error
|
||||
periods []Period
|
||||
}{{testID: "No choices",
|
||||
ts: now,
|
||||
result: time.Time{},
|
||||
err: ErrNoNextChangeover,
|
||||
periods: []Period{}},
|
||||
{testID: "Two Choices, shorter is second",
|
||||
}{
|
||||
{
|
||||
testID: "No choices",
|
||||
ts: now,
|
||||
result: time.Time{},
|
||||
err: ErrNoNextChangeover,
|
||||
periods: []Period{},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, shorter is second",
|
||||
ts: now,
|
||||
result: now.Add(time.Minute),
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, one is a year, other a minute",
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one is a year, other a minute",
|
||||
ts: now,
|
||||
result: now.Add(time.Minute),
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-1 * time.Hour * 24 * 365),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-1 * time.Hour * 24 * 365),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
{testID: "Two Choices, shorter is first",
|
||||
{
|
||||
testID: "Two Choices, shorter is first",
|
||||
ts: now,
|
||||
result: now.Add(time.Minute),
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, one in the past",
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one in the past",
|
||||
ts: now,
|
||||
result: now.Add(time.Minute),
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, one invalid",
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one invalid",
|
||||
ts: now,
|
||||
result: now.Add(time.Minute),
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(time.Minute),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(time.Minute),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, Identical periods",
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, Identical periods",
|
||||
ts: now,
|
||||
result: now.Add(time.Minute),
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "One choice",
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "One choice",
|
||||
ts: now,
|
||||
result: now.Add(time.Minute),
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
periods: []Period{TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"}}}}
|
||||
Identifier: "A",
|
||||
}},
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%s", tc.testID), func(t *testing.T) {
|
||||
ts, err := GetNextChangeOver(now, tc.periods...)
|
||||
@@ -298,7 +476,6 @@ func TestGetNextChangeOver(t *testing.T) {
|
||||
if ts != tc.result {
|
||||
t.Errorf("Got %v but expected %v", ts, tc.result)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
163
msp/msp_test.go
163
msp/msp_test.go
@@ -6,23 +6,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type TimeWindow struct {
|
||||
StartTime time.Time
|
||||
EndTime time.Time
|
||||
Identifier string
|
||||
}
|
||||
|
||||
func (p TimeWindow) GetIdentifier() string {
|
||||
return p.Identifier
|
||||
}
|
||||
func (p TimeWindow) GetEndTime() time.Time {
|
||||
return p.EndTime
|
||||
}
|
||||
func (p TimeWindow) GetStartTime() time.Time {
|
||||
return p.StartTime
|
||||
}
|
||||
|
||||
//(periods ...Period) (id string, err error) {
|
||||
// (periods ...Period) (id string, err error) {
|
||||
func TestMostSpecificPeriod(t *testing.T) {
|
||||
// use a static timestamp to make sure tests don't fail on slower systems or during a process pause
|
||||
now := time.Now()
|
||||
@@ -32,79 +16,135 @@ func TestMostSpecificPeriod(t *testing.T) {
|
||||
result string
|
||||
err error
|
||||
periods []Period
|
||||
}{{testID: "No choices",
|
||||
ts: now,
|
||||
result: "",
|
||||
err: ErrNoValidPeriods,
|
||||
periods: []Period{}},
|
||||
{testID: "Two Choices, shorter is second",
|
||||
}{
|
||||
{
|
||||
testID: "No choices",
|
||||
ts: now,
|
||||
result: "",
|
||||
err: ErrNoValidPeriods,
|
||||
periods: []Period{},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, shorter is second",
|
||||
ts: now,
|
||||
result: "B",
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, one is a year, other a minute",
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one is a year, other a minute",
|
||||
ts: now,
|
||||
result: "B",
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-1 * time.Hour * 24 * 365),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-1 * time.Hour * 24 * 365),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
{testID: "Two Choices, shorter is first",
|
||||
{
|
||||
testID: "Two Choices, shorter is first",
|
||||
ts: now,
|
||||
result: "A",
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-5 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, one in the past",
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-5 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one in the past",
|
||||
ts: now,
|
||||
result: "A",
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, one invalid",
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one invalid",
|
||||
ts: now,
|
||||
result: "B",
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(time.Minute),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-2 * time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(time.Minute),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-2 * time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "Two Choices, Identical periods",
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, Identical periods",
|
||||
ts: now,
|
||||
result: "B",
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"},
|
||||
TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B"}}},
|
||||
{testID: "One choice",
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "One choice",
|
||||
ts: now,
|
||||
result: "A",
|
||||
err: nil,
|
||||
periods: []Period{TimeWindow{StartTime: now.Add(-time.Minute),
|
||||
periods: []Period{TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A"}}}}
|
||||
Identifier: "A",
|
||||
}},
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%s", tc.testID), func(t *testing.T) {
|
||||
id, err := MostSpecificPeriod(tc.ts, tc.periods...)
|
||||
@@ -114,7 +154,6 @@ func TestMostSpecificPeriod(t *testing.T) {
|
||||
if err != tc.err {
|
||||
t.Errorf("Error '%v' does not match expected '%v'", err, tc.err)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,38 @@
|
||||
package msp
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TimeWindow struct {
|
||||
StartTime time.Time
|
||||
EndTime time.Time
|
||||
Identifier string
|
||||
}
|
||||
|
||||
func (p TimeWindow) GetIdentifier() string {
|
||||
return p.Identifier
|
||||
}
|
||||
|
||||
func (p TimeWindow) GetEndTime() time.Time {
|
||||
return p.EndTime
|
||||
}
|
||||
|
||||
func (p TimeWindow) GetStartTime() time.Time {
|
||||
return p.StartTime
|
||||
}
|
||||
|
||||
func (t TimeWindow) String() string {
|
||||
return fmt.Sprintf("%s\t%s\t%s",
|
||||
t.GetIdentifier(),
|
||||
t.GetStartTime(),
|
||||
t.GetEndTime())
|
||||
}
|
||||
|
||||
// Outputs a formatted timeline of periods
|
||||
func GenerateTimeline(periods ...Period) (out []string) {
|
||||
func GenerateTimeline(periods ...Period) (out []Period) {
|
||||
if len(periods) == 0 {
|
||||
out = []string{}
|
||||
return out
|
||||
}
|
||||
periodsByID := make(map[string]Period)
|
||||
@@ -22,8 +49,7 @@ func GenerateTimeline(periods ...Period) (out []string) {
|
||||
start = periodsByID[val].GetStartTime()
|
||||
next = periodsByID[val].GetEndTime()
|
||||
}
|
||||
frame := fmt.Sprintf("%s\t%s\t%s\n", val, start, next)
|
||||
out = append(out, frame)
|
||||
out = append(out, TimeWindow{StartTime: start, EndTime: next, Identifier: val})
|
||||
start = next
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,7 +269,7 @@ func TestGenerateTime(t *testing.T) {
|
||||
t.Fatalf("Time line had %d results, expected %d", len(timeline), len(tc.result))
|
||||
}
|
||||
for idx, period := range timeline {
|
||||
if period != tc.result[idx] {
|
||||
if period.(TimeWindow).String()+"\n" != tc.result[idx] {
|
||||
t.Errorf("Expected:\t%s\nHad:\t%s", period, tc.result[idx])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user