package time_arrow import ( "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) { ti, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-06-08 17:18:00", time.Local) if err != nil { panic(err) } ok := isInTimeOfDay(ti, TimeArrow{ TimesOnDay: []string{ "1:21", "1:21-2:22", "17:00-18:00", }, }) if !ok { t.Fatal("error") } ok = isInTimeOfDay(ti, TimeArrow{ TimesOnDay: []string{ "17:00-17:10", "18:00-19:00", }, }) if ok { 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") } } 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, }, } 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 := 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 = 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 = 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 = 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 = GetHitTimeArrow(ti, "", "三号场") if err != nil || result == nil { t.Fatal("error") panic(err) } if result.ExpandValue.(int) != 180 { t.Fatal("error") } }