123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- package util
- import (
- "testing"
- "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,
- },
- }
- for _, tt := range tests {
- min, max := ConfidenceInterval2(tt.p, tt.total)
- assert.Equal(t, tt.min, min)
- assert.Equal(t, tt.max, max)
- }
- }
|