storj/cmd/satellite/reports/range_test.go
Andrew Harding 62c58f4a9a satellite: consistent report range arguments
This change updates the three satellite report commands that accept date
ranges to parse and treat those dates uniformly.

- End dates are now uniformly exclusive. Exclusive end dates helps
operators avoid one-off errors on month boundaries, as in the operator
does not have to remember how many days are in that month and can just
run the report from the 1st (inclusive) through the 1st (exclusive).
- Fixed the date range validity check which only failed if the start
date came after the end date (it should have failed dates that were
equal since the check happened after adjusting for inclusivity).

Change-Id: Ib2ee1c71ddb916c6e1906834d5ff0dc47d1a5801
2019-12-20 17:17:09 +00:00

64 lines
1.4 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package reports_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"storj.io/storj/cmd/satellite/reports"
)
func TestParseRange(t *testing.T) {
for _, tt := range []struct {
name string
startIn string
endIn string
startOut time.Time
endOut time.Time
err string
}{
{
name: "range end is exclusive",
startIn: "2019-11-01",
endIn: "2019-12-01",
startOut: time.Date(2019, 11, 01, 0, 0, 0, 0, time.UTC),
endOut: time.Date(2019, 12, 01, 0, 0, 0, 0, time.UTC),
},
{
name: "invalid start date",
startIn: "BAD",
endIn: "2019-12-01",
err: "malformed start date (use YYYY-MM-DD)",
},
{
name: "invalid end date",
startIn: "2019-11-01",
endIn: "BAD",
err: "malformed end date (use YYYY-MM-DD)",
},
{
name: "start date must come before end date",
startIn: "2019-11-01",
endIn: "2019-11-01",
err: "invalid date range: start date must come before end date",
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
start, end, err := reports.ParseRange(tt.startIn, tt.endIn)
if tt.err != "" {
require.EqualError(t, err, tt.err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.startOut, start)
assert.Equal(t, tt.endOut, end)
})
}
}