package util import ( "encoding/json" "fmt" "testing" "time" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" "github.com/stretchr/testify/assert" ) func TestConvertParseLocalUnix(t *testing.T) { type args struct { unix int64 } tests := []struct { Name string Args args }{ { Name: "08:00:00", Args: args{ unix: 1727049600, }, }, { Name: "09:00:00", Args: args{ unix: 1727053200, }, }, } for _, tt := range tests { t.Run(tt.Name, func(t *testing.T) { got, err := ConvertParseLocalUnix(tt.Name) assert.Nil(t, err) assert.Equal(t, tt.Args.unix, got) }) } } func TestGetLastDayOfMonth(t *testing.T) { tests := []struct { month string got string }{ { month: "2022-02", got: "2022-02-28", }, { month: "2023-02", got: "2023-02-28", }, { month: "2024-02", got: "2024-02-29", }, { month: "2024-10", got: "2024-10-31", }, { month: "2024-09", got: "2024-09-30", }, } for _, tt := range tests { t.Run(tt.month, func(t *testing.T) { got, err := GetLastDayOfMonth(tt.month) assert.Nil(t, err) assert.Equal(t, tt.got, got) }) } } func TestGetMonthsInRange(t *testing.T) { tests := []struct { startMonth string endMonth string got []string }{ { startMonth: "2023-01", endMonth: "2023-03", got: []string{ "2023-01", "2023-02", "2023-03", }, }, { startMonth: "2023-01", endMonth: "2023-12", got: []string{ "2023-01", "2023-02", "2023-03", "2023-04", "2023-05", "2023-06", "2023-07", "2023-08", "2023-09", "2023-10", "2023-11", "2023-12", }, }, { startMonth: "2023-01", endMonth: "2023-01", got: []string{ "2023-01", }, }, { startMonth: "2023-01", endMonth: "2023-01", got: []string{ "2023-01", }, }, } for _, tt := range tests { t.Run(tt.startMonth, func(t *testing.T) { got, err := GetMonthsInRange(tt.startMonth, tt.endMonth) assert.Nil(t, err) assert.Equal(t, tt.got, got) }) } } func TestRoundToTwoDecimals(t *testing.T) { tests := []struct { exp float64 got float64 }{ { exp: 123.456, got: 123.46, }, { exp: 123.455, got: 123.46, }, { exp: 123.444, got: 123.44, }, { exp: 123.4551, got: 123.46, }, { exp: 123.4449, got: 123.44, }, { exp: 0, got: 0, }, { exp: 0.2, got: 0.2, }, } for _, tt := range tests { t.Run("TestRoundToTwoDecimals", func(t *testing.T) { got := RoundToTwoDecimals(tt.exp) assert.Equal(t, tt.got, got) }) } } func TestGet21DayPeriods(t *testing.T) { tests := []struct { startDay string endDay string got struct { DateRange [][]string Day int32 MiddleDayString []string } }{ { startDay: "2023-01-01", endDay: "2023-01-02", got: struct { DateRange [][]string Day int32 MiddleDayString []string }{ DateRange: [][]string{ {"2022-12-13", "2023-01-02"}, }, Day: 11, MiddleDayString: []string{"2022-12-23"}, }, }, { startDay: "2024-10-23", endDay: "2024-10-24", got: struct { DateRange [][]string Day int32 MiddleDayString []string }{ DateRange: [][]string{ {"2024-10-04", "2024-10-24"}, }, Day: 11, MiddleDayString: []string{"2024-10-14"}, }, }, { startDay: "2024-06-22", endDay: "2024-10-24", got: struct { DateRange [][]string Day int32 MiddleDayString []string }{ DateRange: [][]string{ {"2024-06-21", "2024-07-11"}, {"2024-07-12", "2024-08-01"}, {"2024-08-02", "2024-08-22"}, {"2024-08-23", "2024-09-12"}, {"2024-09-13", "2024-10-03"}, {"2024-10-04", "2024-10-24"}, }, Day: 11, MiddleDayString: []string{"2024-07-01", "2024-07-22", "2024-08-12", "2024-09-02", "2024-09-23", "2024-10-14"}, }, }, } for _, tt := range tests { t.Run(tt.startDay, func(t *testing.T) { got, err := Get21DayPeriods(tt.startDay, tt.endDay) assert.Nil(t, err) assert.Equal(t, tt.got.DateRange, got) for i, v := range tt.got.DateRange { middleDate, err := GetRangeDayMiddleDay(v, tt.got.Day) assert.Nil(t, err) assert.Equal(t, tt.got.MiddleDayString[i], middleDate) } }) } } func TestGetRangeDayByDays(t *testing.T) { tests := []struct { startDay string endDay string days int32 got [][]string }{ { startDay: "2024-10-23", endDay: "2024-10-24", days: 5, got: [][]string{ {"2024-10-23", "2024-10-24"}, }, }, { startDay: "2024-10-23", endDay: "2024-10-24", days: 1, got: [][]string{ {"2024-10-23", "2024-10-23"}, {"2024-10-24", "2024-10-24"}, }, }, { startDay: "2024-10-01", endDay: "2024-10-31", days: 7, got: [][]string{ {"2024-10-01", "2024-10-07"}, {"2024-10-08", "2024-10-14"}, {"2024-10-15", "2024-10-21"}, {"2024-10-22", "2024-10-28"}, {"2024-10-29", "2024-10-31"}, }, }, { startDay: "2024-10-01", endDay: "2024-10-31", days: 5, got: [][]string{ {"2024-10-01", "2024-10-05"}, {"2024-10-06", "2024-10-10"}, {"2024-10-11", "2024-10-15"}, {"2024-10-16", "2024-10-20"}, {"2024-10-21", "2024-10-25"}, {"2024-10-26", "2024-10-30"}, {"2024-10-31", "2024-10-31"}, }, }, } for _, tt := range tests { t.Run(tt.startDay, func(t *testing.T) { got, err := GetRangeDayByDays(tt.startDay, tt.endDay, tt.days) assert.Nil(t, err) assert.Equal(t, tt.got, got) }) } } func TestConfidenceInterval2(t *testing.T) { tests := []struct { p float64 total float64 min float64 max float64 }{ { p: 0.38, total: 114, min: 29, max: 47, }, { p: 0.49, total: 142, min: 40, max: 58, }, { p: 0.41, total: 125, min: 32, max: 50, }, { p: 0, total: 0, min: 0, max: 0, }, } for _, tt := range tests { min, max := ConfidenceInterval2(tt.p, tt.total) assert.Equal(t, tt.min, min) assert.Equal(t, tt.max, max) } } func TestRemoveDuplicates(t *testing.T) { tests := []struct { actual []string got []string }{ { actual: []string{ "1", "1", }, got: []string{ "1", }, }, { actual: []string{ "1", "1", "2", }, got: []string{ "1", "2", }, }, { actual: []string{ "1", }, got: []string{ "1", }, }, { actual: []string{}, got: []string{}, }, } for _, tt := range tests { got := RemoveDuplicates(tt.actual) assert.Equal(t, tt.got, got) } } func TestDaysBetween(t *testing.T) { tests := []struct { actual []int64 got int64 }{ { actual: []int64{ 1730736000, // 2024-11-05 00:00:00 1730772000, // 2024-11-05 10:00:00 }, got: 0, }, { actual: []int64{ 1730772000, // 2024-11-05 10:00:00 1730908800, // 2024-11-07 00:00:00 }, got: 2, }, { actual: []int64{ 1730908800, // 2024-11-07 00:00:00 1730772000, // 2024-11-05 10:00:00 }, got: -2, }, } for _, v := range tests { got := DaysBetween(v.actual[0], v.actual[1]) assert.Equal(t, got, v.got) } } func TestGetNeckRingActiveTimer(t *testing.T) { nowTime := time.Now().Format(Layout) tests := struct { frameId []int32 dateTime []string hours []int32 }{ frameId: []int32{ 1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 15, 16, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 45, 46, 51, 52, 53, 54, 55, 56, 61, 62, 63, 64, 65, 66, 71, 72, 73, 74, 75, 76, 81, 82, 83, 84, 85, 86, 91, 92, 93, 94, 95, 96, 101, 102, 103, 104, 105, 106, 111, 112, 113, 114, 115, 116, 8, 18, 28, 38, 48, 58, 68, 78, 88, 98, 108, 118, }, hours: []int32{ 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23, 23, 24, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 0, }, dateTime: []string{ fmt.Sprintf("%s 00:20:00", nowTime), fmt.Sprintf("%s 00:40:00", nowTime), fmt.Sprintf("%s 01:00:00", nowTime), fmt.Sprintf("%s 01:20:00", nowTime), fmt.Sprintf("%s 01:40:00", nowTime), fmt.Sprintf("%s 02:00:00", nowTime), fmt.Sprintf("%s 02:20:00", nowTime), fmt.Sprintf("%s 02:40:00", nowTime), fmt.Sprintf("%s 03:00:00", nowTime), fmt.Sprintf("%s 03:20:00", nowTime), fmt.Sprintf("%s 03:40:00", nowTime), fmt.Sprintf("%s 04:00:00", nowTime), fmt.Sprintf("%s 04:20:00", nowTime), fmt.Sprintf("%s 04:40:00", nowTime), fmt.Sprintf("%s 05:00:00", nowTime), fmt.Sprintf("%s 05:20:00", nowTime), fmt.Sprintf("%s 05:40:00", nowTime), fmt.Sprintf("%s 06:00:00", nowTime), fmt.Sprintf("%s 06:20:00", nowTime), fmt.Sprintf("%s 06:40:00", nowTime), fmt.Sprintf("%s 07:00:00", nowTime), fmt.Sprintf("%s 07:20:00", nowTime), fmt.Sprintf("%s 07:40:00", nowTime), fmt.Sprintf("%s 08:00:00", nowTime), fmt.Sprintf("%s 08:20:00", nowTime), fmt.Sprintf("%s 08:40:00", nowTime), fmt.Sprintf("%s 09:00:00", nowTime), fmt.Sprintf("%s 09:20:00", nowTime), fmt.Sprintf("%s 09:40:00", nowTime), fmt.Sprintf("%s 10:00:00", nowTime), fmt.Sprintf("%s 10:20:00", nowTime), fmt.Sprintf("%s 10:40:00", nowTime), fmt.Sprintf("%s 11:00:00", nowTime), fmt.Sprintf("%s 11:20:00", nowTime), fmt.Sprintf("%s 11:40:00", nowTime), fmt.Sprintf("%s 12:00:00", nowTime), fmt.Sprintf("%s 12:20:00", nowTime), fmt.Sprintf("%s 12:40:00", nowTime), fmt.Sprintf("%s 13:00:00", nowTime), fmt.Sprintf("%s 13:20:00", nowTime), fmt.Sprintf("%s 13:40:00", nowTime), fmt.Sprintf("%s 14:00:00", nowTime), fmt.Sprintf("%s 14:20:00", nowTime), fmt.Sprintf("%s 14:40:00", nowTime), fmt.Sprintf("%s 15:00:00", nowTime), fmt.Sprintf("%s 15:20:00", nowTime), fmt.Sprintf("%s 15:40:00", nowTime), fmt.Sprintf("%s 16:00:00", nowTime), fmt.Sprintf("%s 16:20:00", nowTime), fmt.Sprintf("%s 16:40:00", nowTime), fmt.Sprintf("%s 17:00:00", nowTime), fmt.Sprintf("%s 17:20:00", nowTime), fmt.Sprintf("%s 17:40:00", nowTime), fmt.Sprintf("%s 18:00:00", nowTime), fmt.Sprintf("%s 18:20:00", nowTime), fmt.Sprintf("%s 18:40:00", nowTime), fmt.Sprintf("%s 19:00:00", nowTime), fmt.Sprintf("%s 19:20:00", nowTime), fmt.Sprintf("%s 19:40:00", nowTime), fmt.Sprintf("%s 20:00:00", nowTime), fmt.Sprintf("%s 20:20:00", nowTime), fmt.Sprintf("%s 20:40:00", nowTime), fmt.Sprintf("%s 21:00:00", nowTime), fmt.Sprintf("%s 21:20:00", nowTime), fmt.Sprintf("%s 21:40:00", nowTime), fmt.Sprintf("%s 22:00:00", nowTime), fmt.Sprintf("%s 22:20:00", nowTime), fmt.Sprintf("%s 22:40:00", nowTime), fmt.Sprintf("%s 23:00:00", nowTime), fmt.Sprintf("%s 23:20:00", nowTime), fmt.Sprintf("%s 23:40:00", nowTime), fmt.Sprintf("%s 24:00:00", nowTime), fmt.Sprintf("%s 02:00:00", nowTime), fmt.Sprintf("%s 04:00:00", nowTime), fmt.Sprintf("%s 06:00:00", nowTime), fmt.Sprintf("%s 08:00:00", nowTime), fmt.Sprintf("%s 10:00:00", nowTime), fmt.Sprintf("%s 12:00:00", nowTime), fmt.Sprintf("%s 14:00:00", nowTime), fmt.Sprintf("%s 16:00:00", nowTime), fmt.Sprintf("%s 18:00:00", nowTime), fmt.Sprintf("%s 20:00:00", nowTime), fmt.Sprintf("%s 22:00:00", nowTime), fmt.Sprintf("%s 00:00:00", nowTime), }, } for i, frameId := range tests.frameId { got, hours := GetNeckRingActiveTimer(frameId) t.Logf("frameId: %d, test-hours: %d,hours:%d got :%s", frameId, tests.hours[i], hours, got) //assert.Equal(t, got, tests.dateTime[i]) assert.Equal(t, int32(hours), tests.hours[i]) } } func Test_demo(t *testing.T) { s := `[{"id":1,"sameTimeName":"PG保健1","sameTimeType":1,"nextNodeDay":7,"nextId":2,"showDays":"7 ~ 14","sumStart":7,"sumEnd":14},{"id":2,"sameTimeName":"PG保健2","sameTimeType":1,"nextNodeDay":7,"nextId":3,"showDays":"14 ~ 21","sumStart":14,"sumEnd":21},{"id":3,"sameTimeName":"PG保健3","sameTimeType":1,"nextNodeDay":3,"nextId":4,"showDays":"21 ~ 28","sumStart":21,"sumEnd":28},{"id":4,"sameTimeName":"RnGH1","sameTimeType":3,"nextNodeDay":7,"nextId":5,"showDays":"24 - 31","sumStart":24,"sumEnd":31},{"id":5,"sameTimeName":"PG同期1","sameTimeType":2,"nextNodeDay":2,"nextId":6,"showDays":"31 - 38","sumStart":31,"sumEnd":38},{"id":6,"sameTimeName":"RnGH2","sameTimeType":3,"nextNodeDay":1,"nextId":7,"showDays":"33 - 40","sumStart":33,"sumEnd":40},{"id":7,"sameTimeName":"TAI输精","sameTimeType":4,"showDays":"34 - 41","sumStart":34,"sumEnd":41}]` collateNodes := make([]*pasturePb.CollateNode, 0) err := json.Unmarshal([]byte(s), &collateNodes) if err != nil { fmt.Println(err) return } nowTime := time.Now() for i, collateNode := range collateNodes { showDay := nowTime if i > 0 { showDay = nowTime.AddDate(0, 0, int(collateNode.NextNodeDay)) } fmt.Println(collateNode.NextNodeDay, showDay.Format("2006-01-02")) } }