mirror of
https://github.com/taigrr/most-specific-period.git
synced 2026-04-02 03:38:41 -07:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
2e68bb06af
|
|||
|
f989049717
|
|||
|
|
6adb6be6b4 | ||
|
|
dd79d7a54b | ||
| 9317c4b137 | |||
|
|
a2c1864a77 | ||
|
|
8612f90d46 | ||
|
|
9698d90308 |
33
main.go
33
main.go
@@ -20,9 +20,11 @@ type Period struct {
|
||||
func (p Period) GetEndTime() time.Time {
|
||||
return p.EndTime
|
||||
}
|
||||
|
||||
func (p Period) GetStartTime() time.Time {
|
||||
return p.StartTime
|
||||
}
|
||||
|
||||
func (p Period) GetIdentifier() string {
|
||||
return p.Identifier
|
||||
}
|
||||
@@ -33,13 +35,35 @@ func init() {
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
}
|
||||
|
||||
func warnMessage() {
|
||||
fmt.Print("Please type your date formats as follows, hit return between each field (RFC 3339), and hit Control+D to signal you are complete: \nIdentifier: id\nStartTime: 2019-10-12T07:20:50.52Z\nEndTime: 2019-10-12T07:20:50.52Z\n")
|
||||
}
|
||||
|
||||
func helpMessage() {
|
||||
fmt.Print("\nmost-significant-period [-h][-d]\n\nGenerates a timeline of periods and will provide a most significant period if available.\n\n-h\tShows this help menut\n-d\tProvide a RFC 3339 time to provide an alternate point for calculating MSP.")
|
||||
}
|
||||
|
||||
func main() {
|
||||
var start time.Time
|
||||
help := flag.Bool("h", false, "displays help command")
|
||||
userDate := flag.String("d", "", "use a custom date to calculate MSP")
|
||||
flag.Parse()
|
||||
if *help {
|
||||
helpMessage()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if userDate != nil && *userDate != "" {
|
||||
t, err := time.Parse(time.RFC3339, *userDate)
|
||||
if err != nil {
|
||||
fmt.Println("Please enter the date using the YYYY-MM-DDT00:00:00.00Z")
|
||||
os.Exit(1)
|
||||
}
|
||||
start = t
|
||||
} else {
|
||||
start = time.Now()
|
||||
}
|
||||
terminal := false
|
||||
fi, _ := os.Stdin.Stat()
|
||||
if (fi.Mode() & os.ModeCharDevice) == 0 {
|
||||
@@ -98,9 +122,14 @@ func main() {
|
||||
count++
|
||||
}
|
||||
|
||||
m, err := msp.MostSpecificPeriod(time.Now(), periods...)
|
||||
vals := msp.GenerateTimeline(periods...)
|
||||
fmt.Print("\nTimeline of changeovers:\n")
|
||||
for _, val := range vals {
|
||||
fmt.Println(val)
|
||||
}
|
||||
m, err := msp.MostSpecificPeriod(start, periods...)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
fmt.Printf("No significant period found\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
if terminal {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
57
msp/timeline.go
Normal file
57
msp/timeline.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package msp
|
||||
|
||||
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 []Period) {
|
||||
if len(periods) == 0 {
|
||||
return out
|
||||
}
|
||||
periodsByID := make(map[string]Period)
|
||||
ids := FlattenPeriods(periods...)
|
||||
for _, val := range periods {
|
||||
id := val.GetIdentifier()
|
||||
periodsByID[id] = val
|
||||
}
|
||||
start := periodsByID[ids[0]].GetStartTime()
|
||||
for _, val := range ids {
|
||||
next, err := GetNextChangeOver(start, periods...)
|
||||
if err == nil {
|
||||
if next.Equal(periodsByID[val].GetStartTime()) {
|
||||
start = periodsByID[val].GetStartTime()
|
||||
next = periodsByID[val].GetEndTime()
|
||||
}
|
||||
out = append(out, TimeWindow{StartTime: start, EndTime: next, Identifier: val})
|
||||
start = next
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
278
msp/timeline_test.go
Normal file
278
msp/timeline_test.go
Normal file
@@ -0,0 +1,278 @@
|
||||
package msp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// (periods ...Period) (id string, err error) {
|
||||
func TestGenerateTime(t *testing.T) {
|
||||
now := time.Now()
|
||||
testCases := []struct {
|
||||
ts time.Time
|
||||
testID string
|
||||
result []string
|
||||
periods []Period
|
||||
}{
|
||||
{
|
||||
testID: "No choices",
|
||||
ts: now,
|
||||
result: []string{},
|
||||
periods: []Period{},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, shorter is second",
|
||||
ts: now,
|
||||
result: []string{
|
||||
fmt.Sprintf("A\t%s\t%s\n", now.Add(-5*time.Minute), now.Add(-2*time.Minute)),
|
||||
fmt.Sprintf("B\t%s\t%s\n", 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),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one is a year, other a minute",
|
||||
ts: now,
|
||||
result: []string{
|
||||
fmt.Sprintf("A\t%s\t%s\n", now.Add(-1*time.Hour*24*365), now.Add(-5*time.Minute)),
|
||||
fmt.Sprintf("B\t%s\t%s\n", 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),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, shorter is first",
|
||||
ts: now,
|
||||
result: []string{
|
||||
fmt.Sprintf("B\t%s\t%s\n", now.Add(-5*time.Minute), now.Add(-2*time.Minute)),
|
||||
fmt.Sprintf("A\t%s\t%s\n", 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),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one in the past",
|
||||
ts: now,
|
||||
result: []string{
|
||||
fmt.Sprintf("B\t%s\t%s\n", now.Add(-2*time.Minute), now.Add(-time.Minute)),
|
||||
fmt.Sprintf("A\t%s\t%s\n", 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),
|
||||
EndTime: now.Add(-time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, one invalid",
|
||||
ts: now,
|
||||
result: []string{
|
||||
fmt.Sprintf("B\t%s\t%s\n", 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),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "Two Choices, Identical periods",
|
||||
ts: now,
|
||||
result: []string{
|
||||
fmt.Sprintf("B\t%s\t%s\n", 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),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "One choice",
|
||||
ts: now,
|
||||
result: []string{
|
||||
fmt.Sprintf("A\t%s\t%s\n", now.Add(-time.Minute), now.Add(time.Minute)),
|
||||
},
|
||||
periods: []Period{TimeWindow{
|
||||
StartTime: now.Add(-time.Minute),
|
||||
EndTime: now.Add(time.Minute),
|
||||
Identifier: "A",
|
||||
}},
|
||||
},
|
||||
{
|
||||
testID: "not in current point in time",
|
||||
ts: now,
|
||||
result: []string{
|
||||
fmt.Sprintf("A\t%s\t%s\n", now.Add(-time.Hour*24*30), now.Add(-time.Hour*24*29)),
|
||||
fmt.Sprintf("B\t%s\t%s\n", now.Add(time.Hour*24*90), now.Add(time.Hour*24*120)),
|
||||
},
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Hour * 24 * 30),
|
||||
EndTime: now.Add(-time.Hour * 24 * 29),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(time.Hour * 24 * 90),
|
||||
EndTime: now.Add(time.Hour * 24 * 120),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "three overlapping periods",
|
||||
ts: now,
|
||||
result: []string{
|
||||
fmt.Sprintf("C\t%s\t%s\n", now.Add(-time.Hour*24*31), now.Add(-time.Hour*24*30)),
|
||||
fmt.Sprintf("A\t%s\t%s\n", now.Add(-time.Hour*24*30), now.Add(-time.Hour*24*29)),
|
||||
fmt.Sprintf("C\t%s\t%s\n", now.Add(-time.Hour*24*29), now.Add(time.Hour*24*90)),
|
||||
fmt.Sprintf("B\t%s\t%s\n", now.Add(time.Hour*24*90), now.Add(time.Hour*24*120)),
|
||||
fmt.Sprintf("C\t%s\t%s\n", now.Add(time.Hour*24*120), now.Add(time.Hour*24*140)),
|
||||
},
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Hour * 24 * 30),
|
||||
EndTime: now.Add(-time.Hour * 24 * 29),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(time.Hour * 24 * 90),
|
||||
EndTime: now.Add(time.Hour * 24 * 120),
|
||||
Identifier: "B",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Hour * 24 * 31),
|
||||
EndTime: now.Add(time.Hour * 24 * 140),
|
||||
Identifier: "C",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "multiple overlapping periods",
|
||||
ts: now,
|
||||
result: []string{
|
||||
fmt.Sprintf("D\t%s\t%s\n", now.Add(-time.Hour*24*150), now.Add(-time.Hour*24*65)),
|
||||
fmt.Sprintf("E\t%s\t%s\n", now.Add(-time.Hour*24*65), now.Add(-time.Hour*24*31)),
|
||||
fmt.Sprintf("C\t%s\t%s\n", now.Add(-time.Hour*24*31), now.Add(-time.Hour*24*30)),
|
||||
fmt.Sprintf("A\t%s\t%s\n", now.Add(-time.Hour*24*30), now.Add(-time.Hour*24*29)),
|
||||
fmt.Sprintf("C\t%s\t%s\n", now.Add(-time.Hour*24*29), now.Add(time.Hour*24*90)),
|
||||
fmt.Sprintf("B\t%s\t%s\n", now.Add(time.Hour*24*90), now.Add(time.Hour*24*120)),
|
||||
fmt.Sprintf("C\t%s\t%s\n", now.Add(time.Hour*24*120), now.Add(time.Hour*24*140)),
|
||||
fmt.Sprintf("E\t%s\t%s\n", now.Add(time.Hour*24*140), now.Add(time.Hour*24*175)),
|
||||
},
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Hour * 24 * 30),
|
||||
EndTime: now.Add(-time.Hour * 24 * 29),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(time.Hour * 24 * 90),
|
||||
EndTime: now.Add(time.Hour * 24 * 120),
|
||||
Identifier: "B",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Hour * 24 * 31),
|
||||
EndTime: now.Add(time.Hour * 24 * 140),
|
||||
Identifier: "C",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Hour * 24 * 150),
|
||||
EndTime: now.Add(time.Hour * 24 * 175),
|
||||
Identifier: "D",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Hour * 24 * 65),
|
||||
EndTime: now.Add(time.Hour * 24 * 175),
|
||||
Identifier: "E",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
testID: "periods with a gap in the middle",
|
||||
ts: now,
|
||||
result: []string{
|
||||
fmt.Sprintf("A\t%s\t%s\n", now.Add(-time.Minute*10), now.Add(-time.Minute*5)),
|
||||
fmt.Sprintf("B\t%s\t%s\n", now.Add(time.Minute*5), now.Add(time.Minute*10)),
|
||||
},
|
||||
periods: []Period{
|
||||
TimeWindow{
|
||||
StartTime: now.Add(-time.Minute * 10),
|
||||
EndTime: now.Add(-time.Minute * 5),
|
||||
Identifier: "A",
|
||||
},
|
||||
TimeWindow{
|
||||
StartTime: now.Add(time.Minute * 5),
|
||||
EndTime: now.Add(time.Minute * 10),
|
||||
Identifier: "B",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%s", tc.testID), func(t *testing.T) {
|
||||
timeline := GenerateTimeline(tc.periods...)
|
||||
if len(timeline) != len(tc.result) {
|
||||
t.Fatalf("Time line had %d results, expected %d", len(timeline), len(tc.result))
|
||||
}
|
||||
for idx, period := range timeline {
|
||||
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