123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package tool
- import (
- "fmt"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- )
- func TestTimeParseLocalUnix(t *testing.T) {
- t.Run("ok", func(t *testing.T) {
- testMap := map[string]int64{
- "2023-02-22": 1676995200,
- "2023-02-23": 1677081600,
- }
- for localTime, timestamp := range testMap {
- assert.Equal(t, TimeParseLocalUnix(localTime), timestamp)
- }
- })
- }
- func TestGetTargetByValueForTag(t *testing.T) {
- }
- func TestStringToTimeUnix(t *testing.T) {
- // 测试分钟维度
- t.Run("minute", func(t *testing.T) {
- for i := 0; i < 10; i++ {
- execTime := fmt.Sprintf("0_0_%d", i)
- want := time.Now().Add(time.Duration(i) * time.Minute).Unix()
- got := StringToTimeUnix(execTime)
- assert.Equal(t, want, got)
- }
- })
- // 测试小时维度
- t.Run("hour", func(t *testing.T) {
- for i := 0; i < 10; i++ {
- execTime := fmt.Sprintf("0_%d_0", i)
- want := time.Now().Add(time.Duration(i) * time.Hour).Unix()
- got := StringToTimeUnix(execTime)
- assert.Equal(t, want, got)
- }
- })
- // 测试天维度
- t.Run("day", func(t *testing.T) {
- for i := 0; i < 10; i++ {
- execTime := fmt.Sprintf("%d_0_0", i)
- want := time.Now().Add(time.Duration(i) * time.Hour * 24).Unix()
- got := StringToTimeUnix(execTime)
- assert.Equal(t, want, got)
- }
- })
- }
- func TestGetLocalTime(t *testing.T) {
- t.Run("ok", func(t *testing.T) {
- tests := []struct {
- PushTimeStr string
- Got int64
- }{
- {
- PushTimeStr: "2023-02-28 09:30:00",
- Got: 1677547800,
- },
- }
- for _, tt := range tests {
- want := GetLocalTime(tt.PushTimeStr)
- assert.Equal(t, want.Unix(), tt.Got)
- }
- })
- }
- func TestDemo(t *testing.T) {
- t.Run("ok", func(t *testing.T) {
- })
- }
- func TestDecimal(t *testing.T) {
- t.Run("ok", func(t *testing.T) {
- tests := []struct {
- Want float64
- Got float64
- }{
- {
- Want: 3.1415926,
- Got: 3.14,
- },
- {
- Want: 3.1465926,
- Got: 3.15,
- },
- {
- Want: 3.1,
- Got: 3.1,
- },
- {
- Want: 3.12,
- Got: 3.12,
- },
- }
- for _, tt := range tests {
- want := Decimal(tt.Want)
- assert.Equal(t, want, tt.Got)
- }
- })
- }
|