tool_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package tool
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestConditionsInterpreter(t *testing.T) {
  9. conditions := `[
  10. {
  11. "condition_item": [
  12. {
  13. "condition_name": "aaa",
  14. "conditions_kind": 2,
  15. "conditions_value": "11111"
  16. },
  17. {
  18. "condition_name": "bbb",
  19. "conditions_kind": 1,
  20. "conditions_value": "22222"
  21. }
  22. ]
  23. },
  24. {
  25. "condition_item": [
  26. {
  27. "condition_name": "ccc",
  28. "conditions_kind": 4,
  29. "conditions_value": "3333"
  30. },
  31. {
  32. "condition_name": "ddddd",
  33. "conditions_kind": 1,
  34. "conditions_value": "4444"
  35. }
  36. ]
  37. }
  38. ]`
  39. t.Run("ok", func(t *testing.T) {
  40. sql, err := ConditionsInterpreter(conditions)
  41. if err != nil {
  42. t.Error(err)
  43. }
  44. want := "( aaa != '11111' AND bbb = '22222' ) OR ( ccc <= '3333' AND ddddd = '4444' )"
  45. assert.Equal(t, sql, want)
  46. })
  47. }
  48. func TestTimeParseLocalUnix(t *testing.T) {
  49. t.Run("ok", func(t *testing.T) {
  50. testMap := map[string]int64{
  51. "2023-02-22": 1676995200,
  52. "2023-02-23": 1677081600,
  53. }
  54. for localTime, timestamp := range testMap {
  55. assert.Equal(t, TimeParseLocalUnix(localTime), timestamp)
  56. }
  57. })
  58. }
  59. func TestGetTargetByValueForTag(t *testing.T) {
  60. }
  61. func TestStringToTimeUnix(t *testing.T) {
  62. // 测试分钟维度
  63. t.Run("minute", func(t *testing.T) {
  64. for i := 0; i < 10; i++ {
  65. execTime := fmt.Sprintf("0_0_%d", i)
  66. want := time.Now().Add(time.Duration(i) * time.Minute).Unix()
  67. got := StringToTimeUnix(execTime)
  68. assert.Equal(t, want, got)
  69. }
  70. })
  71. // 测试小时维度
  72. t.Run("hour", func(t *testing.T) {
  73. for i := 0; i < 10; i++ {
  74. execTime := fmt.Sprintf("0_%d_0", i)
  75. want := time.Now().Add(time.Duration(i) * time.Hour).Unix()
  76. got := StringToTimeUnix(execTime)
  77. assert.Equal(t, want, got)
  78. }
  79. })
  80. // 测试天维度
  81. t.Run("day", func(t *testing.T) {
  82. for i := 0; i < 10; i++ {
  83. execTime := fmt.Sprintf("%d_0_0", i)
  84. want := time.Now().Add(time.Duration(i) * time.Hour * 24).Unix()
  85. got := StringToTimeUnix(execTime)
  86. assert.Equal(t, want, got)
  87. }
  88. })
  89. }
  90. func TestGetLocalTime(t *testing.T) {
  91. t.Run("ok", func(t *testing.T) {
  92. tests := []struct {
  93. PushTimeStr string
  94. Got int64
  95. }{
  96. {
  97. PushTimeStr: "2023-02-28 09:30:00",
  98. Got: 1677547800,
  99. },
  100. }
  101. for _, tt := range tests {
  102. want := GetLocalTime(tt.PushTimeStr)
  103. assert.Equal(t, want.Unix(), tt.Got)
  104. }
  105. })
  106. }
  107. func TestDemo(t *testing.T) {
  108. t.Run("ok", func(t *testing.T) {
  109. })
  110. }