util_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package util
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestUtil_ConvertParseLocalUnix(t *testing.T) {
  7. type args struct {
  8. unix int64
  9. }
  10. tests := []struct {
  11. Name string
  12. Args args
  13. }{
  14. {
  15. Name: "08:00:00",
  16. Args: args{
  17. unix: 1727049600,
  18. },
  19. },
  20. {
  21. Name: "09:00:00",
  22. Args: args{
  23. unix: 1727053200,
  24. },
  25. },
  26. }
  27. for _, tt := range tests {
  28. t.Run(tt.Name, func(t *testing.T) {
  29. got, err := ConvertParseLocalUnix(tt.Name)
  30. assert.Nil(t, err)
  31. assert.Equal(t, tt.Args.unix, got)
  32. })
  33. }
  34. }
  35. func TestGetLastDayOfMonth(t *testing.T) {
  36. tests := []struct {
  37. month string
  38. got string
  39. }{
  40. {
  41. month: "2022-02",
  42. got: "2022-02-28",
  43. },
  44. {
  45. month: "2023-02",
  46. got: "2023-02-28",
  47. },
  48. {
  49. month: "2024-02",
  50. got: "2024-02-29",
  51. },
  52. {
  53. month: "2024-10",
  54. got: "2024-10-31",
  55. }, {
  56. month: "2024-09",
  57. got: "2024-09-30",
  58. },
  59. }
  60. for _, tt := range tests {
  61. t.Run(tt.month, func(t *testing.T) {
  62. got, err := GetLastDayOfMonth(tt.month)
  63. assert.Nil(t, err)
  64. assert.Equal(t, tt.got, got)
  65. })
  66. }
  67. }
  68. func TestGetMonthsInRange(t *testing.T) {
  69. tests := []struct {
  70. startMonth string
  71. endMonth string
  72. got []string
  73. }{
  74. {
  75. startMonth: "2023-01",
  76. endMonth: "2023-03",
  77. got: []string{
  78. "2023-01",
  79. "2023-02",
  80. "2023-03",
  81. },
  82. },
  83. {
  84. startMonth: "2023-01",
  85. endMonth: "2023-12",
  86. got: []string{
  87. "2023-01",
  88. "2023-02",
  89. "2023-03",
  90. "2023-04",
  91. "2023-05",
  92. "2023-06",
  93. "2023-07",
  94. "2023-08",
  95. "2023-09",
  96. "2023-10",
  97. "2023-11",
  98. "2023-12",
  99. },
  100. },
  101. {
  102. startMonth: "2023-01",
  103. endMonth: "2023-01",
  104. got: []string{
  105. "2023-01",
  106. },
  107. },
  108. {
  109. startMonth: "2023-01",
  110. endMonth: "2023-01",
  111. got: []string{
  112. "2023-01",
  113. },
  114. },
  115. }
  116. for _, tt := range tests {
  117. t.Run(tt.startMonth, func(t *testing.T) {
  118. got, err := GetMonthsInRange(tt.startMonth, tt.endMonth)
  119. assert.Nil(t, err)
  120. assert.Equal(t, tt.got, got)
  121. })
  122. }
  123. }
  124. func TestRoundToTwoDecimals(t *testing.T) {
  125. tests := []struct {
  126. exp float64
  127. got float64
  128. }{
  129. {
  130. exp: 123.456,
  131. got: 123.46,
  132. },
  133. {
  134. exp: 123.455,
  135. got: 123.46,
  136. },
  137. {
  138. exp: 123.444,
  139. got: 123.44,
  140. },
  141. {
  142. exp: 123.4551,
  143. got: 123.46,
  144. },
  145. {
  146. exp: 123.4449,
  147. got: 123.44,
  148. },
  149. {
  150. exp: 0,
  151. got: 0,
  152. },
  153. {
  154. exp: 0.2,
  155. got: 0.2,
  156. },
  157. }
  158. for _, tt := range tests {
  159. t.Run("TestRoundToTwoDecimals", func(t *testing.T) {
  160. got := RoundToTwoDecimals(tt.exp)
  161. assert.Equal(t, tt.got, got)
  162. })
  163. }
  164. }