-
Notifications
You must be signed in to change notification settings - Fork 2
/
timeframe_test.go
79 lines (75 loc) · 2.16 KB
/
timeframe_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"reflect"
"testing"
"time"
)
func TestTimeFrame_IsValid(t *testing.T) {
type fields struct {
since string
until string
}
tests := []struct {
name string
fields fields
want bool
}{
{"identical dates", fields{since: "2018-05-03", until: "2018-05-03"}, false},
{"since before until", fields{since: "2018-05-02", until: "2018-05-03"}, true},
{"since after until", fields{since: "2018-05-03", until: "2018-05-02"}, false},
{"contains random string", fields{since: "abcd", until: "2018-05-02"}, false},
{"contains wrong since date format", fields{since: "01.04.2018", until: "2018-05-02"}, false},
{"contains wrong until date format", fields{since: "2018-05-03", until: "02.05.2018"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
timeFrame := TimeFrame{
since: tt.fields.since,
until: tt.fields.until,
}
if got := timeFrame.IsValid(); got != tt.want {
t.Errorf("TimeFrame.IsValid() = %v, want %v", got, tt.want)
}
})
}
}
func TestTimeFrameFromDays(t *testing.T) {
type args struct {
days int
}
tests := []struct {
name string
args args
want TimeFrame
}{
{"since{yesterday}, until:{today}", args{days: 1}, TimeFrame{since: time.Now().Format(dateFormat), until: time.Now().AddDate(0, 0, 1).Format(dateFormat)}},
{"negative number of days are not supported", args{days: -1}, TimeFrame{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := TimeFrameFromDays(tt.args.days); !reflect.DeepEqual(got, tt.want) {
t.Errorf("TimeFrameFromDays() = %v, want %v, name: %v", got, tt.want, tt.name)
}
})
}
}
func TestTimeFrameFromSince(t *testing.T) {
type args struct {
since string
}
tests := []struct {
name string
args args
want TimeFrame
}{
{"empty since", args{since: ""}, TimeFrame{}},
{"correct format", args{since: "2018-05-3"}, TimeFrame{since: "2018-05-3", until: time.Now().Format(dateFormat)}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := TimeFrameFromSince(tt.args.since); !reflect.DeepEqual(got, tt.want) {
t.Errorf("TimeFrameFromSince() = %v, want %v", got, tt.want)
}
})
}
}