77 lines
2.5 KiB
Go
77 lines
2.5 KiB
Go
// Vikunja is a to-do list application to facilitate your life.
|
|
// Copyright 2018-2022 Vikunja and contributors. All rights reserved.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public Licensee for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package parse_time
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestParseTimeRange(t *testing.T) {
|
|
t.Run("last 7 days", func(t *testing.T) {
|
|
from, to, err := ParseTimeRange("now - 7d")
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, time.Now().Add(time.Hour*24*7*-1).Unix(), from.Unix())
|
|
assert.Equal(t, time.Now().Unix(), to.Unix())
|
|
})
|
|
t.Run("last 7 days, but different", func(t *testing.T) {
|
|
from, to, err := ParseTimeRange("7d - now")
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, time.Now().Add(time.Hour*24*7*-1).Unix(), from.Unix())
|
|
assert.Equal(t, time.Now().Unix(), to.Unix())
|
|
})
|
|
t.Run("last 24h", func(t *testing.T) {
|
|
from, to, err := ParseTimeRange("24h - now")
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, time.Now().Add(time.Hour*24*-1).Unix(), from.Unix())
|
|
assert.Equal(t, time.Now().Unix(), to.Unix())
|
|
})
|
|
t.Run("next 7 days", func(t *testing.T) {
|
|
from, to, err := ParseTimeRange("now + 7d")
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, time.Now().Unix(), from.Unix())
|
|
assert.Equal(t, time.Now().Add(time.Hour*24*7).Unix(), to.Unix())
|
|
})
|
|
t.Run("next 24h", func(t *testing.T) {
|
|
from, to, err := ParseTimeRange("now + 24h")
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, time.Now().Unix(), from.Unix())
|
|
assert.Equal(t, time.Now().Add(time.Hour*24).Unix(), to.Unix())
|
|
})
|
|
t.Run("range with only now", func(t *testing.T) {
|
|
_, _, err := ParseTimeRange("now")
|
|
assert.Error(t, err)
|
|
assert.True(t, IsErrInvalidTimeRange(err))
|
|
})
|
|
t.Run("range with only one duration part", func(t *testing.T) {
|
|
_, _, err := ParseTimeRange("7d")
|
|
assert.Error(t, err)
|
|
assert.True(t, IsErrInvalidTimeRange(err))
|
|
})
|
|
t.Run("invalid date range", func(t *testing.T) {
|
|
_, _, err := ParseTimeRange("now-7y")
|
|
assert.Error(t, err)
|
|
})
|
|
}
|