package tool import ( "fmt" "testing" "time" "github.com/stretchr/testify/assert" ) func TestConditionsInterpreter(t *testing.T) { conditions := `[ { "condition_item": [ { "condition_name": "aaa", "conditions_kind": 2, "conditions_value": "11111" }, { "condition_name": "bbb", "conditions_kind": 1, "conditions_value": "22222" } ] }, { "condition_item": [ { "condition_name": "ccc", "conditions_kind": 4, "conditions_value": "3333" }, { "condition_name": "ddddd", "conditions_kind": 1, "conditions_value": "4444" } ] } ]` t.Run("ok", func(t *testing.T) { sql, err := ConditionsInterpreter(conditions) if err != nil { t.Error(err) } want := "( aaa != '11111' AND bbb = '22222' ) OR ( ccc <= '3333' AND ddddd = '4444' )" assert.Equal(t, sql, want) }) } 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) { }) }