| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- package util
- import (
- "reflect"
- "testing"
- "time"
- "github.com/opencost/opencost/core/pkg/util/timeutil"
- )
- func TestNewInterval(t *testing.T) {
- tests := map[string]struct {
- def string
- want Interval
- wantErr bool
- }{
- "invalid": {
- def: "invalid",
- want: nil,
- wantErr: true,
- },
- "invalid2": {
- def: "1M",
- want: nil,
- wantErr: true,
- },
- "invalid3": {
- def: "d20",
- want: nil,
- wantErr: true,
- },
- "one minute": {
- def: "1m",
- want: &durationInterval{
- duration: time.Minute,
- },
- wantErr: false,
- },
- "ten minute": {
- def: "10m",
- want: &durationInterval{
- duration: time.Minute * 10,
- },
- wantErr: false,
- },
- "one hour": {
- def: "1h",
- want: &durationInterval{
- duration: time.Hour,
- },
- },
- "six hours": {
- def: "6h",
- want: &durationInterval{
- duration: time.Hour * 6,
- },
- },
- "one day": {
- def: "1d",
- want: &durationInterval{
- duration: timeutil.Day,
- },
- },
- "seven days": {
- def: "7d",
- want: &durationInterval{
- duration: timeutil.Day * 7,
- },
- },
- "one week": {
- def: "1w",
- want: &weekInterval{
- count: 1,
- },
- },
- "two weeks": {
- def: "2w",
- want: &weekInterval{
- count: 2,
- },
- },
- }
- for name, tt := range tests {
- t.Run(name, func(t *testing.T) {
- got, err := NewInterval(tt.def)
- if (err != nil) != tt.wantErr {
- t.Errorf("NewInterval() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("NewInterval() got = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_durationInterval_Add(t *testing.T) {
- type args struct {
- t time.Time
- i int
- }
- tests := map[string]struct {
- duration time.Duration
- args args
- want time.Time
- }{
- "day interval add 1": {
- duration: timeutil.Day,
- args: args{
- t: time.Date(2025, time.April, 2, 0, 0, 0, 0, time.UTC),
- i: 1,
- },
- want: time.Date(2025, time.April, 3, 0, 0, 0, 0, time.UTC),
- },
- "day interval sub 1": {
- duration: timeutil.Day,
- args: args{
- t: time.Date(2025, time.April, 2, 0, 0, 0, 0, time.UTC),
- i: -1,
- },
- want: time.Date(2025, time.April, 1, 0, 0, 0, 0, time.UTC),
- },
- }
- for name, tt := range tests {
- t.Run(name, func(t *testing.T) {
- d := &durationInterval{
- duration: tt.duration,
- }
- if got := d.Add(tt.args.t, tt.args.i); !reflect.DeepEqual(got, tt.want) {
- t.Errorf("Add() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_durationInterval_Truncate(t *testing.T) {
- tests := map[string]struct {
- duration time.Duration
- input time.Time
- want time.Time
- }{
- "one day truncate": {
- duration: timeutil.Day,
- input: time.Date(2025, time.April, 7, 3, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.April, 7, 0, 0, 0, 0, time.UTC),
- },
- "two day truncate": {
- duration: 2 * timeutil.Day,
- input: time.Date(2025, time.April, 7, 3, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.April, 6, 0, 0, 0, 0, time.UTC),
- },
- "two day truncate 2": {
- duration: 2 * timeutil.Day,
- input: time.Date(2025, time.April, 6, 3, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.April, 6, 0, 0, 0, 0, time.UTC),
- },
- "seven day truncate": {
- duration: 7 * timeutil.Day,
- input: time.Date(2025, time.April, 7, 3, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.April, 7, 0, 0, 0, 0, time.UTC),
- },
- "seven day truncate 2": {
- duration: 7 * timeutil.Day,
- input: time.Date(2025, time.March, 7, 3, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.March, 3, 0, 0, 0, 0, time.UTC),
- },
- "seven day truncate 3": {
- duration: 7 * timeutil.Day,
- input: time.Date(2025, time.March, 3, 0, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.March, 3, 0, 0, 0, 0, time.UTC),
- },
- }
- for name, tt := range tests {
- t.Run(name, func(t *testing.T) {
- d := &durationInterval{
- duration: tt.duration,
- }
- if got := d.Truncate(tt.input); !reflect.DeepEqual(got, tt.want) {
- t.Errorf("Truncate() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_weekInterval_Add(t *testing.T) {
- tests := map[string]struct {
- count int
- t time.Time
- num int
- want time.Time
- }{
- "one week add one": {
- count: 1,
- t: time.Date(2025, time.April, 2, 0, 0, 0, 0, time.UTC),
- num: 1,
- want: time.Date(2025, time.April, 9, 0, 0, 0, 0, time.UTC),
- },
- "one week subtract one": {
- count: 1,
- t: time.Date(2025, time.April, 9, 0, 0, 0, 0, time.UTC),
- num: -1,
- want: time.Date(2025, time.April, 2, 0, 0, 0, 0, time.UTC),
- },
- "two week add one": {
- count: 1,
- t: time.Date(2025, time.April, 2, 0, 0, 0, 0, time.UTC),
- num: 2,
- want: time.Date(2025, time.April, 16, 0, 0, 0, 0, time.UTC),
- },
- "one week add two": {
- count: 2,
- t: time.Date(2025, time.April, 2, 0, 0, 0, 0, time.UTC),
- num: 1,
- want: time.Date(2025, time.April, 16, 0, 0, 0, 0, time.UTC),
- },
- "two week add two": {
- count: 2,
- t: time.Date(2025, time.April, 2, 0, 0, 0, 0, time.UTC),
- num: 2,
- want: time.Date(2025, time.April, 30, 0, 0, 0, 0, time.UTC),
- },
- }
- for name, tt := range tests {
- t.Run(name, func(t *testing.T) {
- w := &weekInterval{
- count: tt.count,
- }
- if got := w.Add(tt.t, tt.num); !reflect.DeepEqual(got, tt.want) {
- t.Errorf("Add() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_weekInterval_Truncate(t *testing.T) {
- tests := map[string]struct {
- count int
- input time.Time
- want time.Time
- }{
- "one week no change": {
- count: 1,
- input: time.Date(2025, time.April, 6, 0, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.April, 6, 0, 0, 0, 0, time.UTC),
- },
- "one week": {
- count: 1,
- input: time.Date(2025, time.April, 7, 3, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.April, 6, 0, 0, 0, 0, time.UTC),
- },
- "one week 2": {
- count: 1,
- input: time.Date(2025, time.March, 7, 3, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.March, 2, 0, 0, 0, 0, time.UTC),
- },
- "one week 3": {
- count: 1,
- input: time.Date(2025, time.March, 3, 0, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.March, 2, 0, 0, 0, 0, time.UTC),
- },
- "two week no change": {
- count: 2,
- input: time.Date(2025, time.March, 30, 0, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.March, 30, 0, 0, 0, 0, time.UTC),
- },
- "two week": {
- count: 2,
- input: time.Date(2025, time.April, 6, 0, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.March, 30, 0, 0, 0, 0, time.UTC),
- },
- "two week 2": {
- count: 2,
- input: time.Date(2025, time.April, 13, 0, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.April, 13, 0, 0, 0, 0, time.UTC),
- },
- "three week": {
- count: 3,
- input: time.Date(2025, time.April, 7, 0, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.April, 6, 0, 0, 0, 0, time.UTC),
- },
- "three week 2": {
- count: 3,
- input: time.Date(2025, time.April, 14, 0, 0, 0, 0, time.UTC),
- want: time.Date(2025, time.April, 6, 0, 0, 0, 0, time.UTC),
- },
- "one week first week": {
- count: 1,
- input: time.Date(1, time.January, 6, 0, 0, 0, 0, time.UTC),
- want: time.Time{}.Add(-1 * timeutil.Day),
- },
- "one week second week": {
- count: 1,
- input: time.Date(1, time.January, 9, 0, 0, 0, 0, time.UTC),
- want: time.Date(1, time.January, 7, 0, 0, 0, 0, time.UTC),
- },
- "two week second week": {
- count: 2,
- input: time.Date(1, time.January, 9, 0, 0, 0, 0, time.UTC),
- want: time.Time{}.Add(-1 * timeutil.Day),
- },
- }
- for name, tt := range tests {
- t.Run(name, func(t *testing.T) {
- w := &weekInterval{
- count: tt.count,
- }
- got := w.Truncate(tt.input)
- if got.Weekday() != time.Sunday {
- t.Errorf("result was not a sunday: %s", got.Weekday().String())
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("Truncate() = %v, want %v", got, tt.want)
- }
- })
- }
- }
|