util_test.go 13 KB

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