util_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package util
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestConvertParseLocalUnix(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. }
  165. func TestGet21DayPeriods(t *testing.T) {
  166. tests := []struct {
  167. startDay string
  168. endDay string
  169. got struct {
  170. DateRange [][]string
  171. Day int32
  172. MiddleDayString []string
  173. }
  174. }{
  175. {
  176. startDay: "2023-01-01",
  177. endDay: "2023-01-02",
  178. got: struct {
  179. DateRange [][]string
  180. Day int32
  181. MiddleDayString []string
  182. }{
  183. DateRange: [][]string{
  184. {"2022-12-13", "2023-01-02"},
  185. },
  186. Day: 11,
  187. MiddleDayString: []string{"2022-12-23"},
  188. },
  189. },
  190. {
  191. startDay: "2024-10-23",
  192. endDay: "2024-10-24",
  193. got: struct {
  194. DateRange [][]string
  195. Day int32
  196. MiddleDayString []string
  197. }{
  198. DateRange: [][]string{
  199. {"2024-10-04", "2024-10-24"},
  200. },
  201. Day: 11,
  202. MiddleDayString: []string{"2024-10-14"},
  203. },
  204. },
  205. {
  206. startDay: "2024-06-22",
  207. endDay: "2024-10-24",
  208. got: struct {
  209. DateRange [][]string
  210. Day int32
  211. MiddleDayString []string
  212. }{
  213. DateRange: [][]string{
  214. {"2024-06-21", "2024-07-11"},
  215. {"2024-07-12", "2024-08-01"},
  216. {"2024-08-02", "2024-08-22"},
  217. {"2024-08-23", "2024-09-12"},
  218. {"2024-09-13", "2024-10-03"},
  219. {"2024-10-04", "2024-10-24"},
  220. }, Day: 11,
  221. MiddleDayString: []string{"2024-07-01", "2024-07-22", "2024-08-12", "2024-09-02", "2024-09-23", "2024-10-14"},
  222. },
  223. },
  224. }
  225. for _, tt := range tests {
  226. t.Run(tt.startDay, func(t *testing.T) {
  227. got, err := Get21DayPeriods(tt.startDay, tt.endDay)
  228. assert.Nil(t, err)
  229. assert.Equal(t, tt.got.DateRange, got)
  230. for i, v := range tt.got.DateRange {
  231. middleDate, err := GetRangeDayMiddleDay(v, tt.got.Day)
  232. assert.Nil(t, err)
  233. assert.Equal(t, tt.got.MiddleDayString[i], middleDate)
  234. }
  235. })
  236. }
  237. }
  238. func TestGetRangeDayByDays(t *testing.T) {
  239. tests := []struct {
  240. startDay string
  241. endDay string
  242. days int32
  243. got [][]string
  244. }{
  245. {
  246. startDay: "2024-10-23",
  247. endDay: "2024-10-24",
  248. days: 5,
  249. got: [][]string{
  250. {"2024-10-23", "2024-10-24"},
  251. },
  252. },
  253. {
  254. startDay: "2024-10-23",
  255. endDay: "2024-10-24",
  256. days: 1,
  257. got: [][]string{
  258. {"2024-10-23", "2024-10-23"},
  259. {"2024-10-24", "2024-10-24"},
  260. },
  261. },
  262. {
  263. startDay: "2024-10-01",
  264. endDay: "2024-10-31",
  265. days: 7,
  266. got: [][]string{
  267. {"2024-10-01", "2024-10-07"},
  268. {"2024-10-08", "2024-10-14"},
  269. {"2024-10-15", "2024-10-21"},
  270. {"2024-10-22", "2024-10-28"},
  271. {"2024-10-29", "2024-10-31"},
  272. },
  273. },
  274. {
  275. startDay: "2024-10-01",
  276. endDay: "2024-10-31",
  277. days: 5,
  278. got: [][]string{
  279. {"2024-10-01", "2024-10-05"},
  280. {"2024-10-06", "2024-10-10"},
  281. {"2024-10-11", "2024-10-15"},
  282. {"2024-10-16", "2024-10-20"},
  283. {"2024-10-21", "2024-10-25"},
  284. {"2024-10-26", "2024-10-30"},
  285. {"2024-10-31", "2024-10-31"},
  286. },
  287. },
  288. }
  289. for _, tt := range tests {
  290. t.Run(tt.startDay, func(t *testing.T) {
  291. got, err := GetRangeDayByDays(tt.startDay, tt.endDay, tt.days)
  292. assert.Nil(t, err)
  293. assert.Equal(t, tt.got, got)
  294. })
  295. }
  296. }