123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- package util
- import (
- "testing"
- "github.com/stretchr/testify/assert"
- )
- func TestUtil_ConvertParseLocalUnix(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)
- })
- }
- }
|