util_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. package util
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestConvertParseLocalUnix(t *testing.T) {
  9. type args struct {
  10. unix int64
  11. }
  12. tests := []struct {
  13. Name string
  14. Args args
  15. }{
  16. {
  17. Name: "08:00:00",
  18. Args: args{
  19. unix: 1727049600,
  20. },
  21. },
  22. {
  23. Name: "09:00:00",
  24. Args: args{
  25. unix: 1727053200,
  26. },
  27. },
  28. }
  29. for _, tt := range tests {
  30. t.Run(tt.Name, func(t *testing.T) {
  31. got, err := ConvertParseLocalUnix(tt.Name)
  32. assert.Nil(t, err)
  33. assert.Equal(t, tt.Args.unix, got)
  34. })
  35. }
  36. }
  37. func TestGetLastDayOfMonth(t *testing.T) {
  38. tests := []struct {
  39. month string
  40. got string
  41. }{
  42. {
  43. month: "2022-02",
  44. got: "2022-02-28",
  45. },
  46. {
  47. month: "2023-02",
  48. got: "2023-02-28",
  49. },
  50. {
  51. month: "2024-02",
  52. got: "2024-02-29",
  53. },
  54. {
  55. month: "2024-10",
  56. got: "2024-10-31",
  57. }, {
  58. month: "2024-09",
  59. got: "2024-09-30",
  60. },
  61. }
  62. for _, tt := range tests {
  63. t.Run(tt.month, func(t *testing.T) {
  64. got, err := GetLastDayOfMonth(tt.month)
  65. assert.Nil(t, err)
  66. assert.Equal(t, tt.got, got)
  67. })
  68. }
  69. }
  70. func TestGetMonthsInRange(t *testing.T) {
  71. tests := []struct {
  72. startMonth string
  73. endMonth string
  74. got []string
  75. }{
  76. {
  77. startMonth: "2023-01",
  78. endMonth: "2023-03",
  79. got: []string{
  80. "2023-01",
  81. "2023-02",
  82. "2023-03",
  83. },
  84. },
  85. {
  86. startMonth: "2023-01",
  87. endMonth: "2023-12",
  88. got: []string{
  89. "2023-01",
  90. "2023-02",
  91. "2023-03",
  92. "2023-04",
  93. "2023-05",
  94. "2023-06",
  95. "2023-07",
  96. "2023-08",
  97. "2023-09",
  98. "2023-10",
  99. "2023-11",
  100. "2023-12",
  101. },
  102. },
  103. {
  104. startMonth: "2023-01",
  105. endMonth: "2023-01",
  106. got: []string{
  107. "2023-01",
  108. },
  109. },
  110. {
  111. startMonth: "2023-01",
  112. endMonth: "2023-01",
  113. got: []string{
  114. "2023-01",
  115. },
  116. },
  117. }
  118. for _, tt := range tests {
  119. t.Run(tt.startMonth, func(t *testing.T) {
  120. got, err := GetMonthsInRange(tt.startMonth, tt.endMonth)
  121. assert.Nil(t, err)
  122. assert.Equal(t, tt.got, got)
  123. })
  124. }
  125. }
  126. func TestRoundToTwoDecimals(t *testing.T) {
  127. tests := []struct {
  128. exp float64
  129. got float64
  130. }{
  131. {
  132. exp: 123.456,
  133. got: 123.46,
  134. },
  135. {
  136. exp: 123.455,
  137. got: 123.46,
  138. },
  139. {
  140. exp: 123.444,
  141. got: 123.44,
  142. },
  143. {
  144. exp: 123.4551,
  145. got: 123.46,
  146. },
  147. {
  148. exp: 123.4449,
  149. got: 123.44,
  150. },
  151. {
  152. exp: 0,
  153. got: 0,
  154. },
  155. {
  156. exp: 0.2,
  157. got: 0.2,
  158. },
  159. }
  160. for _, tt := range tests {
  161. t.Run("TestRoundToTwoDecimals", func(t *testing.T) {
  162. got := RoundToTwoDecimals(tt.exp)
  163. assert.Equal(t, tt.got, got)
  164. })
  165. }
  166. }
  167. func TestGet21DayPeriods(t *testing.T) {
  168. tests := []struct {
  169. startDay string
  170. endDay string
  171. got struct {
  172. DateRange [][]string
  173. Day int32
  174. MiddleDayString []string
  175. }
  176. }{
  177. {
  178. startDay: "2023-01-01",
  179. endDay: "2023-01-02",
  180. got: struct {
  181. DateRange [][]string
  182. Day int32
  183. MiddleDayString []string
  184. }{
  185. DateRange: [][]string{
  186. {"2022-12-13", "2023-01-02"},
  187. },
  188. Day: 11,
  189. MiddleDayString: []string{"2022-12-23"},
  190. },
  191. },
  192. {
  193. startDay: "2024-10-23",
  194. endDay: "2024-10-24",
  195. got: struct {
  196. DateRange [][]string
  197. Day int32
  198. MiddleDayString []string
  199. }{
  200. DateRange: [][]string{
  201. {"2024-10-04", "2024-10-24"},
  202. },
  203. Day: 11,
  204. MiddleDayString: []string{"2024-10-14"},
  205. },
  206. },
  207. {
  208. startDay: "2024-06-22",
  209. endDay: "2024-10-24",
  210. got: struct {
  211. DateRange [][]string
  212. Day int32
  213. MiddleDayString []string
  214. }{
  215. DateRange: [][]string{
  216. {"2024-06-21", "2024-07-11"},
  217. {"2024-07-12", "2024-08-01"},
  218. {"2024-08-02", "2024-08-22"},
  219. {"2024-08-23", "2024-09-12"},
  220. {"2024-09-13", "2024-10-03"},
  221. {"2024-10-04", "2024-10-24"},
  222. }, Day: 11,
  223. MiddleDayString: []string{"2024-07-01", "2024-07-22", "2024-08-12", "2024-09-02", "2024-09-23", "2024-10-14"},
  224. },
  225. },
  226. }
  227. for _, tt := range tests {
  228. t.Run(tt.startDay, func(t *testing.T) {
  229. got, err := Get21DayPeriods(tt.startDay, tt.endDay)
  230. assert.Nil(t, err)
  231. assert.Equal(t, tt.got.DateRange, got)
  232. for i, v := range tt.got.DateRange {
  233. middleDate, err := GetRangeDayMiddleDay(v, tt.got.Day)
  234. assert.Nil(t, err)
  235. assert.Equal(t, tt.got.MiddleDayString[i], middleDate)
  236. }
  237. })
  238. }
  239. }
  240. func TestGetRangeDayByDays(t *testing.T) {
  241. tests := []struct {
  242. startDay string
  243. endDay string
  244. days int32
  245. got [][]string
  246. }{
  247. {
  248. startDay: "2024-10-23",
  249. endDay: "2024-10-24",
  250. days: 5,
  251. got: [][]string{
  252. {"2024-10-23", "2024-10-24"},
  253. },
  254. },
  255. {
  256. startDay: "2024-10-23",
  257. endDay: "2024-10-24",
  258. days: 1,
  259. got: [][]string{
  260. {"2024-10-23", "2024-10-23"},
  261. {"2024-10-24", "2024-10-24"},
  262. },
  263. },
  264. {
  265. startDay: "2024-10-01",
  266. endDay: "2024-10-31",
  267. days: 7,
  268. got: [][]string{
  269. {"2024-10-01", "2024-10-07"},
  270. {"2024-10-08", "2024-10-14"},
  271. {"2024-10-15", "2024-10-21"},
  272. {"2024-10-22", "2024-10-28"},
  273. {"2024-10-29", "2024-10-31"},
  274. },
  275. },
  276. {
  277. startDay: "2024-10-01",
  278. endDay: "2024-10-31",
  279. days: 5,
  280. got: [][]string{
  281. {"2024-10-01", "2024-10-05"},
  282. {"2024-10-06", "2024-10-10"},
  283. {"2024-10-11", "2024-10-15"},
  284. {"2024-10-16", "2024-10-20"},
  285. {"2024-10-21", "2024-10-25"},
  286. {"2024-10-26", "2024-10-30"},
  287. {"2024-10-31", "2024-10-31"},
  288. },
  289. },
  290. }
  291. for _, tt := range tests {
  292. t.Run(tt.startDay, func(t *testing.T) {
  293. got, err := GetRangeDayByDays(tt.startDay, tt.endDay, tt.days)
  294. assert.Nil(t, err)
  295. assert.Equal(t, tt.got, got)
  296. })
  297. }
  298. }
  299. func TestConfidenceInterval2(t *testing.T) {
  300. tests := []struct {
  301. p float64
  302. total float64
  303. min float64
  304. max float64
  305. }{
  306. {
  307. p: 0.38,
  308. total: 114,
  309. min: 29,
  310. max: 47,
  311. },
  312. {
  313. p: 0.49,
  314. total: 142,
  315. min: 40,
  316. max: 58,
  317. },
  318. {
  319. p: 0.41,
  320. total: 125,
  321. min: 32,
  322. max: 50,
  323. },
  324. {
  325. p: 0,
  326. total: 0,
  327. min: 0,
  328. max: 0,
  329. },
  330. }
  331. for _, tt := range tests {
  332. min, max := ConfidenceInterval2(tt.p, tt.total)
  333. assert.Equal(t, tt.min, min)
  334. assert.Equal(t, tt.max, max)
  335. }
  336. }
  337. func TestRemoveDuplicates(t *testing.T) {
  338. tests := []struct {
  339. actual []string
  340. got []string
  341. }{
  342. {
  343. actual: []string{
  344. "1", "1",
  345. },
  346. got: []string{
  347. "1",
  348. },
  349. },
  350. {
  351. actual: []string{
  352. "1", "1", "2",
  353. },
  354. got: []string{
  355. "1", "2",
  356. },
  357. },
  358. {
  359. actual: []string{
  360. "1",
  361. },
  362. got: []string{
  363. "1",
  364. },
  365. },
  366. {
  367. actual: []string{},
  368. got: []string{},
  369. },
  370. }
  371. for _, tt := range tests {
  372. got := RemoveDuplicates(tt.actual)
  373. assert.Equal(t, tt.got, got)
  374. }
  375. }
  376. func TestDaysBetween(t *testing.T) {
  377. tests := []struct {
  378. actual []int64
  379. got int64
  380. }{
  381. {
  382. actual: []int64{
  383. 1730736000, // 2024-11-05 00:00:00
  384. 1730772000, // 2024-11-05 10:00:00
  385. },
  386. got: 0,
  387. },
  388. {
  389. actual: []int64{
  390. 1730772000, // 2024-11-05 10:00:00
  391. 1730908800, // 2024-11-07 00:00:00
  392. },
  393. got: 2,
  394. },
  395. {
  396. actual: []int64{
  397. 1730908800, // 2024-11-07 00:00:00
  398. 1730772000, // 2024-11-05 10:00:00
  399. },
  400. got: -2,
  401. },
  402. }
  403. for _, v := range tests {
  404. got := DaysBetween(v.actual[0], v.actual[1])
  405. assert.Equal(t, got, v.got)
  406. }
  407. }
  408. func TestGetNeckRingActiveTimer(t *testing.T) {
  409. nowTime := time.Now().Format(Layout)
  410. tests := struct {
  411. frameId []int32
  412. dateTime []string
  413. hours []int32
  414. }{
  415. frameId: []int32{
  416. 1, 2, 3, 4, 5, 6,
  417. 11, 12, 13, 14, 15, 16,
  418. 21, 22, 23, 24, 25, 26,
  419. 31, 32, 33, 34, 35, 36,
  420. 41, 42, 43, 44, 45, 46,
  421. 51, 52, 53, 54, 55, 56,
  422. 61, 62, 63, 64, 65, 66,
  423. 71, 72, 73, 74, 75, 76,
  424. 81, 82, 83, 84, 85, 86,
  425. 91, 92, 93, 94, 95, 96,
  426. 101, 102, 103, 104, 105, 106,
  427. 111, 112, 113, 114, 115, 116,
  428. 8, 18, 28, 38, 48, 58, 68, 78, 88, 98, 108, 118,
  429. },
  430. hours: []int32{
  431. 0, 0, 1, 1, 1, 2,
  432. 2, 2, 3, 3, 3, 4,
  433. 4, 4, 5, 5, 5, 6,
  434. 6, 6, 7, 7, 7, 8,
  435. 8, 8, 9, 9, 9, 10,
  436. 10, 10, 11, 11, 11, 12,
  437. 12, 12, 13, 13, 13, 14,
  438. 14, 14, 15, 15, 15, 16,
  439. 16, 16, 17, 17, 17, 18,
  440. 18, 18, 19, 19, 19, 20,
  441. 20, 20, 21, 21, 21, 22,
  442. 22, 22, 23, 23, 23, 24,
  443. 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 0,
  444. },
  445. dateTime: []string{
  446. fmt.Sprintf("%s 00:20:00", nowTime), fmt.Sprintf("%s 00:40:00", nowTime), fmt.Sprintf("%s 01:00:00", nowTime),
  447. fmt.Sprintf("%s 01:20:00", nowTime), fmt.Sprintf("%s 01:40:00", nowTime), fmt.Sprintf("%s 02:00:00", nowTime),
  448. fmt.Sprintf("%s 02:20:00", nowTime), fmt.Sprintf("%s 02:40:00", nowTime), fmt.Sprintf("%s 03:00:00", nowTime),
  449. fmt.Sprintf("%s 03:20:00", nowTime), fmt.Sprintf("%s 03:40:00", nowTime), fmt.Sprintf("%s 04:00:00", nowTime),
  450. fmt.Sprintf("%s 04:20:00", nowTime), fmt.Sprintf("%s 04:40:00", nowTime), fmt.Sprintf("%s 05:00:00", nowTime),
  451. fmt.Sprintf("%s 05:20:00", nowTime), fmt.Sprintf("%s 05:40:00", nowTime), fmt.Sprintf("%s 06:00:00", nowTime),
  452. fmt.Sprintf("%s 06:20:00", nowTime), fmt.Sprintf("%s 06:40:00", nowTime), fmt.Sprintf("%s 07:00:00", nowTime),
  453. fmt.Sprintf("%s 07:20:00", nowTime), fmt.Sprintf("%s 07:40:00", nowTime), fmt.Sprintf("%s 08:00:00", nowTime),
  454. fmt.Sprintf("%s 08:20:00", nowTime), fmt.Sprintf("%s 08:40:00", nowTime), fmt.Sprintf("%s 09:00:00", nowTime),
  455. fmt.Sprintf("%s 09:20:00", nowTime), fmt.Sprintf("%s 09:40:00", nowTime), fmt.Sprintf("%s 10:00:00", nowTime),
  456. fmt.Sprintf("%s 10:20:00", nowTime), fmt.Sprintf("%s 10:40:00", nowTime), fmt.Sprintf("%s 11:00:00", nowTime),
  457. fmt.Sprintf("%s 11:20:00", nowTime), fmt.Sprintf("%s 11:40:00", nowTime), fmt.Sprintf("%s 12:00:00", nowTime),
  458. fmt.Sprintf("%s 12:20:00", nowTime), fmt.Sprintf("%s 12:40:00", nowTime), fmt.Sprintf("%s 13:00:00", nowTime),
  459. fmt.Sprintf("%s 13:20:00", nowTime), fmt.Sprintf("%s 13:40:00", nowTime), fmt.Sprintf("%s 14:00:00", nowTime),
  460. fmt.Sprintf("%s 14:20:00", nowTime), fmt.Sprintf("%s 14:40:00", nowTime), fmt.Sprintf("%s 15:00:00", nowTime),
  461. fmt.Sprintf("%s 15:20:00", nowTime), fmt.Sprintf("%s 15:40:00", nowTime), fmt.Sprintf("%s 16:00:00", nowTime),
  462. fmt.Sprintf("%s 16:20:00", nowTime), fmt.Sprintf("%s 16:40:00", nowTime), fmt.Sprintf("%s 17:00:00", nowTime),
  463. fmt.Sprintf("%s 17:20:00", nowTime), fmt.Sprintf("%s 17:40:00", nowTime), fmt.Sprintf("%s 18:00:00", nowTime),
  464. fmt.Sprintf("%s 18:20:00", nowTime), fmt.Sprintf("%s 18:40:00", nowTime), fmt.Sprintf("%s 19:00:00", nowTime),
  465. fmt.Sprintf("%s 19:20:00", nowTime), fmt.Sprintf("%s 19:40:00", nowTime), fmt.Sprintf("%s 20:00:00", nowTime),
  466. fmt.Sprintf("%s 20:20:00", nowTime), fmt.Sprintf("%s 20:40:00", nowTime), fmt.Sprintf("%s 21:00:00", nowTime),
  467. fmt.Sprintf("%s 21:20:00", nowTime), fmt.Sprintf("%s 21:40:00", nowTime), fmt.Sprintf("%s 22:00:00", nowTime),
  468. fmt.Sprintf("%s 22:20:00", nowTime), fmt.Sprintf("%s 22:40:00", nowTime), fmt.Sprintf("%s 23:00:00", nowTime),
  469. fmt.Sprintf("%s 23:20:00", nowTime), fmt.Sprintf("%s 23:40:00", nowTime), fmt.Sprintf("%s 24:00:00", nowTime),
  470. fmt.Sprintf("%s 02:00:00", nowTime), fmt.Sprintf("%s 04:00:00", nowTime), fmt.Sprintf("%s 06:00:00", nowTime),
  471. fmt.Sprintf("%s 08:00:00", nowTime), fmt.Sprintf("%s 10:00:00", nowTime), fmt.Sprintf("%s 12:00:00", nowTime),
  472. fmt.Sprintf("%s 14:00:00", nowTime), fmt.Sprintf("%s 16:00:00", nowTime), fmt.Sprintf("%s 18:00:00", nowTime),
  473. fmt.Sprintf("%s 20:00:00", nowTime), fmt.Sprintf("%s 22:00:00", nowTime), fmt.Sprintf("%s 00:00:00", nowTime),
  474. },
  475. }
  476. for i, frameId := range tests.frameId {
  477. got, hours := GetNeckRingActiveTimer(frameId)
  478. t.Logf("frameId: %d, test-hours: %d,hours:%d got :%s", frameId, tests.hours[i], hours, got)
  479. //assert.Equal(t, got, tests.dateTime[i])
  480. assert.Equal(t, int32(hours), tests.hours[i])
  481. }
  482. }
  483. func Test_demo(t *testing.T) {
  484. oldValue := float64(850)
  485. currValue := float64(364)
  486. a := RoundToTwoDecimals((oldValue - currValue) / oldValue * 100)
  487. fmt.Println(a)
  488. }