| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603 | package utilimport (	"fmt"	"sort"	"strconv"	"strings"	"testing"	"time"	"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().Local().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 TestSubstr(t *testing.T) {	nowTime := time.Now().Local().Format(Layout)	text := `102053      0   0   53   12.2   4.7  101415 101538   0.4    0        0 0 ???????? 000000  0.0  0.0  0.0  0.0 0`	fmt.Println()	detachTime := fmt.Sprintf("%s %s:%s:%s", nowTime, Substr(text, 0, 2), Substr(text, 2, 2), Substr(text, 4, 2))	varCowCode := fmt.Sprintf("%s", Substr(text, 7, 6))	detacherAddress := fmt.Sprintf("%s", Substr(text, 20, 2))	milkWeight := fmt.Sprintf("%s", strings.TrimSpace(Substr(text, 25, 4)))	s1 := strings.TrimSpace(Substr(text, 0, 6))	s2 := strings.TrimSpace(Substr(text, 25, 4))	fmt.Println("detachTime", detachTime, varCowCode, detacherAddress, milkWeight, s1, s2)	fmt.Println("milkWeight:", milkWeight, strings.TrimSpace(Substr(text, 25, 4)))	milkWeight1, err := strconv.ParseInt(strings.TrimSpace(Substr(text, 25, 4)), 10, 64)	fmt.Println("milkWeight1:", milkWeight1, err)}type WeeklyActiveModel struct {	CowId    int64  `json:"cow_id"`	HeatDate string `json:"heat_date"`	Nb       int32  `json:"nb"`	High     int32  `json:"high"`}func Test_demo(t *testing.T) {	weeklyActiveModelList := make([]*WeeklyActiveModel, 0)	weeklyActiveModelList = append(weeklyActiveModelList, &WeeklyActiveModel{		CowId:    8859,		HeatDate: "2025-04-08",		Nb:       12,		High:     1633,	}, &WeeklyActiveModel{		CowId:    8859,		HeatDate: "2025-04-09",		Nb:       12,		High:     1697,	}, &WeeklyActiveModel{		CowId:    8859,		HeatDate: "2025-04-10",		Nb:       12,		High:     1588,	}, &WeeklyActiveModel{		CowId:    8859,		HeatDate: "2025-04-11",		Nb:       12,		High:     1789,	}, &WeeklyActiveModel{		CowId:    8859,		HeatDate: "2025-04-12",		Nb:       12,		High:     1822,	}, &WeeklyActiveModel{		CowId:    8859,		HeatDate: "2025-04-13",		Nb:       12,		High:     1672,	}, &WeeklyActiveModel{		CowId:    8859,		HeatDate: "2025-04-14",		Nb:       12,		High:     1817,	})	// 1. 先按High值排序	sort.Slice(weeklyActiveModelList, func(i, j int) bool {		return weeklyActiveModelList[i].High < weeklyActiveModelList[j].High	})	countHigh := len(weeklyActiveModelList)	sumHigh := int32(0)	minHigh := weeklyActiveModelList[0].High	maxHigh := weeklyActiveModelList[len(weeklyActiveModelList)-1].High	for _, v := range weeklyActiveModelList {		sumHigh += v.High	}	diff := 2	x := int32(0)	if countHigh > 3 {		diff = 3		x = weeklyActiveModelList[1].High	}	fmt.Println("sumHigh", sumHigh)	fmt.Println("minHigh", minHigh)	fmt.Println("maxHigh", maxHigh)	fmt.Println("x", x)	fmt.Println("diff", diff, "countHigh", countHigh)	avgHigh := float64(sumHigh-minHigh-maxHigh-x) / float64(countHigh-diff)	fmt.Println(int32(avgHigh))}
 |