时间选定库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

605 lines
12 KiB

package time_arrow
import (
"fmt"
"math/rand"
"reflect"
"testing"
"time"
)
func Test_isInWeekOfDay(t *testing.T) {
ti, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-06-08 00:00:00", time.Local)
if err != nil {
panic(err)
}
ok := isInWeekOfDay(ti, TimeArrow{
DayOfWeek: []int{
1,
3,
},
})
if !ok {
t.Fatal("error")
}
ok = isInWeekOfDay(ti, TimeArrow{
DayOfWeek: []int{
6,
3,
2,
},
})
if ok {
t.Fatal("error")
}
ok = isInWeekOfDay(ti, TimeArrow{})
if ok {
t.Fatal("error")
}
}
func Test_timeCompletion(t *testing.T) {
if timeCompletion("12") != "12:00:00" {
t.Fatal("error")
}
if timeCompletion("17:00") != "17:00:00" {
t.Fatal("error")
}
}
func Test_isInTimeOfDay(t *testing.T) {
rand.Seed(time.Now().Unix())
for k := 0; k < 300; k++ {
rt := rand.Int() % 24
rm := rand.Int() % 60
ti, err := time.ParseInLocation("2006-01-02 15:04:05", fmt.Sprintf("2020-06-08 %.2d:%.2d:00", rt, rm), time.Local)
if err != nil {
panic(err)
}
ok := isInTimeOfDay(ti, TimeArrow{
TimesOnDay: []string{
"1:21",
"1:21-2:22",
"17:00-18:00",
},
})
ok2 := isInTimeOfDay(ti, TimeArrow{
TimesOnDay: []string{
"1:21",
"1:21-2:22",
"17:00-18:00",
},
})
if ok != ok2 {
t.Fatal("error")
}
ok = isInTimeOfDay(ti, TimeArrow{
TimesOnDay: []string{
"17:00-17:10",
"18:00-19:00",
},
})
ok2 = isInTimeOfDay2(ti, TimeArrow{
TimesOnDay: []string{
"17:00-17:10",
"18:00-19:00",
},
})
if ok != ok2 {
t.Fatal("error")
}
tss := []string{}
for i := 0; i < 24; i++ {
tss = append(tss, fmt.Sprintf("%d:00-%d:30", i, i))
tss = append(tss, fmt.Sprintf("%d:30-%d:00", i, i+1))
}
ok = isInTimeOfDay(ti, TimeArrow{
TimesOnDay: tss,
})
ok2 = isInTimeOfDay2(ti, TimeArrow{
TimesOnDay: tss,
})
if ok != ok2 {
t.Fatal("error")
}
tss = []string{}
for i := 0; i < 24; i++ {
if i == rt {
continue
}
tss = append(tss, fmt.Sprintf("%d:00-%d:30", i, i))
tss = append(tss, fmt.Sprintf("%d:30-%d:00", i, i+1))
}
ok = isInTimeOfDay(ti, TimeArrow{
TimesOnDay: tss,
})
ok2 = isInTimeOfDay2(ti, TimeArrow{
TimesOnDay: tss,
})
if ok != ok2 {
t.Fatal("error")
}
}
}
func Test_isInDateSlice(t *testing.T) {
ti, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-06-08 17:18:00", time.Local)
if err != nil {
panic(err)
}
ok := isInDateSlice(ti, TimeArrow{
DateSlice: []DateSlice{},
})
if ok {
t.Fatal("error")
}
ok = isInDateSlice(ti, TimeArrow{
DateSlice: []DateSlice{
{
Start: "2020-06-05 17:18",
End: "2020-06-07",
},
},
})
if ok {
t.Fatal("error")
}
ok = isInDateSlice(ti, TimeArrow{
DateSlice: []DateSlice{
{
Start: "2020-06-05 17:18",
End: "2020-06-07",
},
{
Start: "2020-06-07 17:18",
End: "2020-06-09",
},
},
})
if !ok {
t.Fatal("error")
}
}
func Test_isInExpandTags(t *testing.T) {
ok := isInExpandTags(TimeArrow{
ExpandTags: []string{},
}, "一号场")
if !ok {
t.Fatal("error")
}
ok = isInExpandTags(TimeArrow{
ExpandTags: []string{
"一号场",
"二号场",
"四号场",
},
}, "一号场")
if !ok {
t.Fatal("error")
}
ok = isInExpandTags(TimeArrow{
ExpandTags: []string{
"一号场",
"二号场",
"四号场",
},
}, "三号场")
if ok {
t.Fatal("error")
}
}
var th TimeArrowHelper
func TestGetHitTimeArrow(t *testing.T) {
tas := TimeArrows{
{
Type: TimeArrowTypeDayOfWeek,
DayOfWeek: []int{
0,
1,
2,
3,
4,
5,
6,
},
ExpandTags: []string{
"一号场",
"二号场",
"三号场",
"四号场",
},
ExpandValue: 0,
Weights: 0,
},
{
Type: TimeArrowTypeDayOfWeek,
DayOfWeek: []int{
6,
0,
},
ExpandTags: []string{
"一号场",
"二号场",
"四号场",
},
ExpandValue: 30,
Weights: 1,
},
{
Type: TimeArrowTypeDayOfWeek,
DayOfWeek: []int{
6,
0,
},
ExpandTags: []string{
"三号场",
},
ExpandValue: 60,
Weights: 1,
},
{
Type: TimeArrowTypeDayOfMonth,
DayOfMonth: []int{
3,
5,
},
ExpandValue: 12,
Weights: 2,
},
{
Type: TimeArrowTypeDateSlice,
DateSlice: []DateSlice{
{
Start: "2020-04-05",
End: "2020-05-05",
},
{
Start: "2020-06-07 17:18",
End: "2020-06-09",
},
},
ExpandValue: 5,
Weights: 3,
},
{
Type: TimeArrowTypeDateSlice,
DateSlice: []DateSlice{
{
Start: "2020-04-05",
End: "2020-05-05",
},
{
Start: "2020-06-07 17:18",
End: "2020-06-09",
},
},
TimesOnDay: []string{
"18-19",
},
ExpandValue: 180,
Weights: 4,
},
}
th.GetData = func(group string) (arrows TimeArrows, e error) {
return tas, nil
}
ti, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-06-01 17:18:00", time.Local)
if err != nil {
panic(err)
}
result, err := th.GetHitTimeArrow(ti, "", "一号场")
if err != nil || result == nil {
t.Fatal("error")
panic(err)
}
if result.ExpandValue.(int) != 0 {
t.Fatal("error")
}
ti = ti.AddDate(0, 0, -1)
result, err = th.GetHitTimeArrow(ti, "", "一号场")
if err != nil || result == nil {
t.Fatal("error")
panic(err)
}
if result.ExpandValue.(int) != 30 {
t.Fatal("error", result.ExpandValue.(int))
}
result, err = th.GetHitTimeArrow(ti, "", "三号场")
if err != nil || result == nil {
t.Fatal("error")
panic(err)
}
if result.ExpandValue.(int) != 60 {
t.Fatal("error")
}
ti, err = time.ParseInLocation("2006-01-02 15:04:05", "2020-04-11 17:18:00", time.Local)
if err != nil {
panic(err)
}
result, err = th.GetHitTimeArrow(ti, "", "三号场")
if err != nil || result == nil {
t.Fatal("error")
panic(err)
}
if result.ExpandValue.(int) != 5 {
t.Fatal("error")
}
ti = ti.Add(time.Hour)
result, err = th.GetHitTimeArrow(ti, "", "三号场")
if err != nil || result == nil {
t.Fatal("error")
panic(err)
}
if result.ExpandValue.(int) != 180 {
t.Fatal("error")
}
}
func Test_isInDayOfMonth(t *testing.T) {
type args struct {
t time.Time
ta TimeArrow
}
tests := []struct {
name string
args args
want bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isInDayOfMonth(tt.args.t, tt.args.ta); got != tt.want {
t.Errorf("isInDayOfMonth() = %v, want %v", got, tt.want)
}
})
}
}
func Test_isInHolidays(t *testing.T) {
GetHolidaysData = func() (items []HolidaysItem, e error) {
start, _ := time.Parse("2006-01-02 15:04:05", "2020-10-01 00:00:00")
end, _ := time.Parse("2006-01-02 15:04:05", "2020-10-07 23:59:59")
return []HolidaysItem{
{
Name: "国庆节",
Start: start,
End: end,
},
}, nil
}
tt, _ := time.Parse("2006-01-02 15:04:05", "2020-10-03 11:00:00")
if !isInHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-10-08 11:00:00")
if isInHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-09-08 11:00:00")
if isInHolidays(tt) {
t.Fatal("error")
}
}
func Test_isInAtLastDayHolidays(t *testing.T) {
GetHolidaysData = func() (items []HolidaysItem, e error) {
start, _ := time.Parse("2006-01-02 15:04:05", "2020-10-01 00:00:00")
end, _ := time.Parse("2006-01-02 15:04:05", "2020-10-07 23:59:59")
return []HolidaysItem{
{
Name: "国庆节",
Start: start,
End: end,
},
}, nil
}
tt, _ := time.Parse("2006-01-02 15:04:05", "2020-10-03 11:00:00")
if isInAtLastDayHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-10-08 11:00:00")
if isInAtLastDayHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-09-08 11:00:00")
if isInAtLastDayHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-10-07 11:00:00")
if !isInAtLastDayHolidays(tt) {
t.Fatal("error")
}
}
func Test_isInPriorToDayHolidays(t *testing.T) {
GetHolidaysData = func() (items []HolidaysItem, e error) {
start, _ := time.Parse("2006-01-02 15:04:05", "2020-10-01 00:00:00")
end, _ := time.Parse("2006-01-02 15:04:05", "2020-10-07 23:59:59")
return []HolidaysItem{
{
Name: "国庆节",
Start: start,
End: end,
},
}, nil
}
tt, _ := time.Parse("2006-01-02 15:04:05", "2020-10-03 11:00:00")
if isInPriorToDayHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-10-08 11:00:00")
if isInPriorToDayHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-09-08 11:00:00")
if isInPriorToDayHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-09-30 11:00:00")
if !isInPriorToDayHolidays(tt) {
t.Fatal("error")
}
}
func Test_createUUID(t *testing.T) {
tests := []struct {
name string
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := createUUID(); got != tt.want {
t.Errorf("createUUID() = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateDayOfWeekTypePlan(t *testing.T) {
type args struct {
group string
dayOfWeek []int
timesOnDay []string
expandValue interface{}
expandTags []string
weights float64
}
tests := []struct {
name string
args args
want TimeArrow
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CreateDayOfWeekTypePlan(tt.args.group, tt.args.dayOfWeek, tt.args.timesOnDay, tt.args.expandValue, tt.args.expandTags, tt.args.weights); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateDayOfWeekTypePlan() = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateDayOfMonthTypePlan(t *testing.T) {
type args struct {
group string
dayOfMonth []int
timesOnDay []string
expandValue interface{}
expandTags []string
weights float64
}
tests := []struct {
name string
args args
want TimeArrow
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CreateDayOfMonthTypePlan(tt.args.group, tt.args.dayOfMonth, tt.args.timesOnDay, tt.args.expandValue, tt.args.expandTags, tt.args.weights); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateDayOfMonthTypePlan() = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateDateSliceTypePlan(t *testing.T) {
type args struct {
group string
dateSlice []DateSlice
timesOnDay []string
expandValue interface{}
expandTags []string
weights float64
}
tests := []struct {
name string
args args
want TimeArrow
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CreateDateSliceTypePlan(tt.args.group, tt.args.dateSlice, tt.args.timesOnDay, tt.args.expandValue, tt.args.expandTags, tt.args.weights); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateDateSliceTypePlan() = %v, want %v", got, tt.want)
}
})
}
}
func TestTimeArrowHelper_GetHitTimeArrow(t *testing.T) {
type fields struct {
GetData func(group string) (TimeArrows, error)
}
type args struct {
t time.Time
group string
expandTags []string
}
tests := []struct {
name string
fields fields
args args
want *TimeArrow
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := &TimeArrowHelper{
GetData: tt.fields.GetData,
}
got, err := th.GetHitTimeArrow(tt.args.t, tt.args.group, tt.args.expandTags...)
if (err != nil) != tt.wantErr {
t.Errorf("TimeArrowHelper.GetHitTimeArrow() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("TimeArrowHelper.GetHitTimeArrow() = %v, want %v", got, tt.want)
}
})
}
}