tool_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package tool
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestTimeParseLocalUnix(t *testing.T) {
  9. t.Run("ok", func(t *testing.T) {
  10. testMap := map[string]int64{
  11. "2023-02-22": 1676995200,
  12. "2023-02-23": 1677081600,
  13. }
  14. for localTime, timestamp := range testMap {
  15. assert.Equal(t, TimeParseLocalUnix(localTime), timestamp)
  16. }
  17. })
  18. }
  19. func TestGetTargetByValueForTag(t *testing.T) {
  20. }
  21. func TestStringToTimeUnix(t *testing.T) {
  22. // 测试分钟维度
  23. t.Run("minute", func(t *testing.T) {
  24. for i := 0; i < 10; i++ {
  25. execTime := fmt.Sprintf("0_0_%d", i)
  26. want := time.Now().Add(time.Duration(i) * time.Minute).Unix()
  27. got := StringToTimeUnix(execTime)
  28. assert.Equal(t, want, got)
  29. }
  30. })
  31. // 测试小时维度
  32. t.Run("hour", func(t *testing.T) {
  33. for i := 0; i < 10; i++ {
  34. execTime := fmt.Sprintf("0_%d_0", i)
  35. want := time.Now().Add(time.Duration(i) * time.Hour).Unix()
  36. got := StringToTimeUnix(execTime)
  37. assert.Equal(t, want, got)
  38. }
  39. })
  40. // 测试天维度
  41. t.Run("day", func(t *testing.T) {
  42. for i := 0; i < 10; i++ {
  43. execTime := fmt.Sprintf("%d_0_0", i)
  44. want := time.Now().Add(time.Duration(i) * time.Hour * 24).Unix()
  45. got := StringToTimeUnix(execTime)
  46. assert.Equal(t, want, got)
  47. }
  48. })
  49. }
  50. func TestGetLocalTime(t *testing.T) {
  51. t.Run("ok", func(t *testing.T) {
  52. tests := []struct {
  53. PushTimeStr string
  54. Got int64
  55. }{
  56. {
  57. PushTimeStr: "2023-02-28 09:30:00",
  58. Got: 1677547800,
  59. },
  60. }
  61. for _, tt := range tests {
  62. want := GetLocalTime(tt.PushTimeStr)
  63. assert.Equal(t, want.Unix(), tt.Got)
  64. }
  65. })
  66. }
  67. func TestDemo(t *testing.T) {
  68. t.Run("ok", func(t *testing.T) {
  69. })
  70. }
  71. func TestDecimal(t *testing.T) {
  72. t.Run("ok", func(t *testing.T) {
  73. tests := []struct {
  74. Want float64
  75. Got float64
  76. }{
  77. {
  78. Want: 3.1415926,
  79. Got: 3.14,
  80. },
  81. {
  82. Want: 3.1465926,
  83. Got: 3.15,
  84. },
  85. {
  86. Want: 3.1,
  87. Got: 3.1,
  88. },
  89. {
  90. Want: 3.12,
  91. Got: 3.12,
  92. },
  93. }
  94. for _, tt := range tests {
  95. want := Decimal(tt.Want)
  96. assert.Equal(t, want, tt.Got)
  97. }
  98. })
  99. }