tool_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. }