时间选定库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

605 lines
12 KiB

5 years ago
3 years ago
4 years ago
5 years ago
5 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
  1. package time_arrow
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "reflect"
  6. "testing"
  7. "time"
  8. )
  9. func Test_isInWeekOfDay(t *testing.T) {
  10. ti, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-06-08 00:00:00", time.Local)
  11. if err != nil {
  12. panic(err)
  13. }
  14. ok := isInWeekOfDay(ti, TimeArrow{
  15. DayOfWeek: []int{
  16. 1,
  17. 3,
  18. },
  19. })
  20. if !ok {
  21. t.Fatal("error")
  22. }
  23. ok = isInWeekOfDay(ti, TimeArrow{
  24. DayOfWeek: []int{
  25. 6,
  26. 3,
  27. 2,
  28. },
  29. })
  30. if ok {
  31. t.Fatal("error")
  32. }
  33. ok = isInWeekOfDay(ti, TimeArrow{})
  34. if ok {
  35. t.Fatal("error")
  36. }
  37. }
  38. func Test_timeCompletion(t *testing.T) {
  39. if timeCompletion("12") != "12:00:00" {
  40. t.Fatal("error")
  41. }
  42. if timeCompletion("17:00") != "17:00:00" {
  43. t.Fatal("error")
  44. }
  45. }
  46. func Test_isInTimeOfDay(t *testing.T) {
  47. rand.Seed(time.Now().Unix())
  48. for k := 0; k < 300; k++ {
  49. rt := rand.Int() % 24
  50. rm := rand.Int() % 60
  51. ti, err := time.ParseInLocation("2006-01-02 15:04:05", fmt.Sprintf("2020-06-08 %.2d:%.2d:00", rt, rm), time.Local)
  52. if err != nil {
  53. panic(err)
  54. }
  55. ok := isInTimeOfDay(ti, TimeArrow{
  56. TimesOnDay: []string{
  57. "1:21",
  58. "1:21-2:22",
  59. "17:00-18:00",
  60. },
  61. })
  62. ok2 := isInTimeOfDay(ti, TimeArrow{
  63. TimesOnDay: []string{
  64. "1:21",
  65. "1:21-2:22",
  66. "17:00-18:00",
  67. },
  68. })
  69. if ok != ok2 {
  70. t.Fatal("error")
  71. }
  72. ok = isInTimeOfDay(ti, TimeArrow{
  73. TimesOnDay: []string{
  74. "17:00-17:10",
  75. "18:00-19:00",
  76. },
  77. })
  78. ok2 = isInTimeOfDay2(ti, TimeArrow{
  79. TimesOnDay: []string{
  80. "17:00-17:10",
  81. "18:00-19:00",
  82. },
  83. })
  84. if ok != ok2 {
  85. t.Fatal("error")
  86. }
  87. tss := []string{}
  88. for i := 0; i < 24; i++ {
  89. tss = append(tss, fmt.Sprintf("%d:00-%d:30", i, i))
  90. tss = append(tss, fmt.Sprintf("%d:30-%d:00", i, i+1))
  91. }
  92. ok = isInTimeOfDay(ti, TimeArrow{
  93. TimesOnDay: tss,
  94. })
  95. ok2 = isInTimeOfDay2(ti, TimeArrow{
  96. TimesOnDay: tss,
  97. })
  98. if ok != ok2 {
  99. t.Fatal("error")
  100. }
  101. tss = []string{}
  102. for i := 0; i < 24; i++ {
  103. if i == rt {
  104. continue
  105. }
  106. tss = append(tss, fmt.Sprintf("%d:00-%d:30", i, i))
  107. tss = append(tss, fmt.Sprintf("%d:30-%d:00", i, i+1))
  108. }
  109. ok = isInTimeOfDay(ti, TimeArrow{
  110. TimesOnDay: tss,
  111. })
  112. ok2 = isInTimeOfDay2(ti, TimeArrow{
  113. TimesOnDay: tss,
  114. })
  115. if ok != ok2 {
  116. t.Fatal("error")
  117. }
  118. }
  119. }
  120. func Test_isInDateSlice(t *testing.T) {
  121. ti, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-06-08 17:18:00", time.Local)
  122. if err != nil {
  123. panic(err)
  124. }
  125. ok := isInDateSlice(ti, TimeArrow{
  126. DateSlice: []DateSlice{},
  127. })
  128. if ok {
  129. t.Fatal("error")
  130. }
  131. ok = isInDateSlice(ti, TimeArrow{
  132. DateSlice: []DateSlice{
  133. {
  134. Start: "2020-06-05 17:18",
  135. End: "2020-06-07",
  136. },
  137. },
  138. })
  139. if ok {
  140. t.Fatal("error")
  141. }
  142. ok = isInDateSlice(ti, TimeArrow{
  143. DateSlice: []DateSlice{
  144. {
  145. Start: "2020-06-05 17:18",
  146. End: "2020-06-07",
  147. },
  148. {
  149. Start: "2020-06-07 17:18",
  150. End: "2020-06-09",
  151. },
  152. },
  153. })
  154. if !ok {
  155. t.Fatal("error")
  156. }
  157. }
  158. func Test_isInExpandTags(t *testing.T) {
  159. ok := isInExpandTags(TimeArrow{
  160. ExpandTags: []string{},
  161. }, "一号场")
  162. if !ok {
  163. t.Fatal("error")
  164. }
  165. ok = isInExpandTags(TimeArrow{
  166. ExpandTags: []string{
  167. "一号场",
  168. "二号场",
  169. "四号场",
  170. },
  171. }, "一号场")
  172. if !ok {
  173. t.Fatal("error")
  174. }
  175. ok = isInExpandTags(TimeArrow{
  176. ExpandTags: []string{
  177. "一号场",
  178. "二号场",
  179. "四号场",
  180. },
  181. }, "三号场")
  182. if ok {
  183. t.Fatal("error")
  184. }
  185. }
  186. var th TimeArrowHelper
  187. func TestGetHitTimeArrow(t *testing.T) {
  188. tas := TimeArrows{
  189. {
  190. Type: TimeArrowTypeDayOfWeek,
  191. DayOfWeek: []int{
  192. 0,
  193. 1,
  194. 2,
  195. 3,
  196. 4,
  197. 5,
  198. 6,
  199. },
  200. ExpandTags: []string{
  201. "一号场",
  202. "二号场",
  203. "三号场",
  204. "四号场",
  205. },
  206. ExpandValue: 0,
  207. Weights: 0,
  208. },
  209. {
  210. Type: TimeArrowTypeDayOfWeek,
  211. DayOfWeek: []int{
  212. 6,
  213. 0,
  214. },
  215. ExpandTags: []string{
  216. "一号场",
  217. "二号场",
  218. "四号场",
  219. },
  220. ExpandValue: 30,
  221. Weights: 1,
  222. },
  223. {
  224. Type: TimeArrowTypeDayOfWeek,
  225. DayOfWeek: []int{
  226. 6,
  227. 0,
  228. },
  229. ExpandTags: []string{
  230. "三号场",
  231. },
  232. ExpandValue: 60,
  233. Weights: 1,
  234. },
  235. {
  236. Type: TimeArrowTypeDayOfMonth,
  237. DayOfMonth: []int{
  238. 3,
  239. 5,
  240. },
  241. ExpandValue: 12,
  242. Weights: 2,
  243. },
  244. {
  245. Type: TimeArrowTypeDateSlice,
  246. DateSlice: []DateSlice{
  247. {
  248. Start: "2020-04-05",
  249. End: "2020-05-05",
  250. },
  251. {
  252. Start: "2020-06-07 17:18",
  253. End: "2020-06-09",
  254. },
  255. },
  256. ExpandValue: 5,
  257. Weights: 3,
  258. },
  259. {
  260. Type: TimeArrowTypeDateSlice,
  261. DateSlice: []DateSlice{
  262. {
  263. Start: "2020-04-05",
  264. End: "2020-05-05",
  265. },
  266. {
  267. Start: "2020-06-07 17:18",
  268. End: "2020-06-09",
  269. },
  270. },
  271. TimesOnDay: []string{
  272. "18-19",
  273. },
  274. ExpandValue: 180,
  275. Weights: 4,
  276. },
  277. }
  278. th.GetData = func(group string) (arrows TimeArrows, e error) {
  279. return tas, nil
  280. }
  281. ti, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-06-01 17:18:00", time.Local)
  282. if err != nil {
  283. panic(err)
  284. }
  285. result, err := th.GetHitTimeArrow(ti, "", "一号场")
  286. if err != nil || result == nil {
  287. t.Fatal("error")
  288. panic(err)
  289. }
  290. if result.ExpandValue.(int) != 0 {
  291. t.Fatal("error")
  292. }
  293. ti = ti.AddDate(0, 0, -1)
  294. result, err = th.GetHitTimeArrow(ti, "", "一号场")
  295. if err != nil || result == nil {
  296. t.Fatal("error")
  297. panic(err)
  298. }
  299. if result.ExpandValue.(int) != 30 {
  300. t.Fatal("error", result.ExpandValue.(int))
  301. }
  302. result, err = th.GetHitTimeArrow(ti, "", "三号场")
  303. if err != nil || result == nil {
  304. t.Fatal("error")
  305. panic(err)
  306. }
  307. if result.ExpandValue.(int) != 60 {
  308. t.Fatal("error")
  309. }
  310. ti, err = time.ParseInLocation("2006-01-02 15:04:05", "2020-04-11 17:18:00", time.Local)
  311. if err != nil {
  312. panic(err)
  313. }
  314. result, err = th.GetHitTimeArrow(ti, "", "三号场")
  315. if err != nil || result == nil {
  316. t.Fatal("error")
  317. panic(err)
  318. }
  319. if result.ExpandValue.(int) != 5 {
  320. t.Fatal("error")
  321. }
  322. ti = ti.Add(time.Hour)
  323. result, err = th.GetHitTimeArrow(ti, "", "三号场")
  324. if err != nil || result == nil {
  325. t.Fatal("error")
  326. panic(err)
  327. }
  328. if result.ExpandValue.(int) != 180 {
  329. t.Fatal("error")
  330. }
  331. }
  332. func Test_isInDayOfMonth(t *testing.T) {
  333. type args struct {
  334. t time.Time
  335. ta TimeArrow
  336. }
  337. tests := []struct {
  338. name string
  339. args args
  340. want bool
  341. }{
  342. // TODO: Add test cases.
  343. }
  344. for _, tt := range tests {
  345. t.Run(tt.name, func(t *testing.T) {
  346. if got := isInDayOfMonth(tt.args.t, tt.args.ta); got != tt.want {
  347. t.Errorf("isInDayOfMonth() = %v, want %v", got, tt.want)
  348. }
  349. })
  350. }
  351. }
  352. func Test_isInHolidays(t *testing.T) {
  353. GetHolidaysData = func() (items []HolidaysItem, e error) {
  354. start, _ := time.Parse("2006-01-02 15:04:05", "2020-10-01 00:00:00")
  355. end, _ := time.Parse("2006-01-02 15:04:05", "2020-10-07 23:59:59")
  356. return []HolidaysItem{
  357. {
  358. Name: "国庆节",
  359. Start: start,
  360. End: end,
  361. },
  362. }, nil
  363. }
  364. tt, _ := time.Parse("2006-01-02 15:04:05", "2020-10-03 11:00:00")
  365. if !isInHolidays(tt) {
  366. t.Fatal("error")
  367. }
  368. tt, _ = time.Parse("2006-01-02 15:04:05", "2020-10-08 11:00:00")
  369. if isInHolidays(tt) {
  370. t.Fatal("error")
  371. }
  372. tt, _ = time.Parse("2006-01-02 15:04:05", "2020-09-08 11:00:00")
  373. if isInHolidays(tt) {
  374. t.Fatal("error")
  375. }
  376. }
  377. func Test_isInAtLastDayHolidays(t *testing.T) {
  378. GetHolidaysData = func() (items []HolidaysItem, e error) {
  379. start, _ := time.Parse("2006-01-02 15:04:05", "2020-10-01 00:00:00")
  380. end, _ := time.Parse("2006-01-02 15:04:05", "2020-10-07 23:59:59")
  381. return []HolidaysItem{
  382. {
  383. Name: "国庆节",
  384. Start: start,
  385. End: end,
  386. },
  387. }, nil
  388. }
  389. tt, _ := time.Parse("2006-01-02 15:04:05", "2020-10-03 11:00:00")
  390. if isInAtLastDayHolidays(tt) {
  391. t.Fatal("error")
  392. }
  393. tt, _ = time.Parse("2006-01-02 15:04:05", "2020-10-08 11:00:00")
  394. if isInAtLastDayHolidays(tt) {
  395. t.Fatal("error")
  396. }
  397. tt, _ = time.Parse("2006-01-02 15:04:05", "2020-09-08 11:00:00")
  398. if isInAtLastDayHolidays(tt) {
  399. t.Fatal("error")
  400. }
  401. tt, _ = time.Parse("2006-01-02 15:04:05", "2020-10-07 11:00:00")
  402. if !isInAtLastDayHolidays(tt) {
  403. t.Fatal("error")
  404. }
  405. }
  406. func Test_isInPriorToDayHolidays(t *testing.T) {
  407. GetHolidaysData = func() (items []HolidaysItem, e error) {
  408. start, _ := time.Parse("2006-01-02 15:04:05", "2020-10-01 00:00:00")
  409. end, _ := time.Parse("2006-01-02 15:04:05", "2020-10-07 23:59:59")
  410. return []HolidaysItem{
  411. {
  412. Name: "国庆节",
  413. Start: start,
  414. End: end,
  415. },
  416. }, nil
  417. }
  418. tt, _ := time.Parse("2006-01-02 15:04:05", "2020-10-03 11:00:00")
  419. if isInPriorToDayHolidays(tt) {
  420. t.Fatal("error")
  421. }
  422. tt, _ = time.Parse("2006-01-02 15:04:05", "2020-10-08 11:00:00")
  423. if isInPriorToDayHolidays(tt) {
  424. t.Fatal("error")
  425. }
  426. tt, _ = time.Parse("2006-01-02 15:04:05", "2020-09-08 11:00:00")
  427. if isInPriorToDayHolidays(tt) {
  428. t.Fatal("error")
  429. }
  430. tt, _ = time.Parse("2006-01-02 15:04:05", "2020-09-30 11:00:00")
  431. if !isInPriorToDayHolidays(tt) {
  432. t.Fatal("error")
  433. }
  434. }
  435. func Test_createUUID(t *testing.T) {
  436. tests := []struct {
  437. name string
  438. want string
  439. }{
  440. // TODO: Add test cases.
  441. }
  442. for _, tt := range tests {
  443. t.Run(tt.name, func(t *testing.T) {
  444. if got := createUUID(); got != tt.want {
  445. t.Errorf("createUUID() = %v, want %v", got, tt.want)
  446. }
  447. })
  448. }
  449. }
  450. func TestCreateDayOfWeekTypePlan(t *testing.T) {
  451. type args struct {
  452. group string
  453. dayOfWeek []int
  454. timesOnDay []string
  455. expandValue interface{}
  456. expandTags []string
  457. weights float64
  458. }
  459. tests := []struct {
  460. name string
  461. args args
  462. want TimeArrow
  463. }{
  464. // TODO: Add test cases.
  465. }
  466. for _, tt := range tests {
  467. t.Run(tt.name, func(t *testing.T) {
  468. if got := CreateDayOfWeekTypePlan(tt.args.group, tt.args.dayOfWeek, tt.args.timesOnDay, tt.args.expandValue, tt.args.expandTags, tt.args.weights); !reflect.DeepEqual(got, tt.want) {
  469. t.Errorf("CreateDayOfWeekTypePlan() = %v, want %v", got, tt.want)
  470. }
  471. })
  472. }
  473. }
  474. func TestCreateDayOfMonthTypePlan(t *testing.T) {
  475. type args struct {
  476. group string
  477. dayOfMonth []int
  478. timesOnDay []string
  479. expandValue interface{}
  480. expandTags []string
  481. weights float64
  482. }
  483. tests := []struct {
  484. name string
  485. args args
  486. want TimeArrow
  487. }{
  488. // TODO: Add test cases.
  489. }
  490. for _, tt := range tests {
  491. t.Run(tt.name, func(t *testing.T) {
  492. if got := CreateDayOfMonthTypePlan(tt.args.group, tt.args.dayOfMonth, tt.args.timesOnDay, tt.args.expandValue, tt.args.expandTags, tt.args.weights); !reflect.DeepEqual(got, tt.want) {
  493. t.Errorf("CreateDayOfMonthTypePlan() = %v, want %v", got, tt.want)
  494. }
  495. })
  496. }
  497. }
  498. func TestCreateDateSliceTypePlan(t *testing.T) {
  499. type args struct {
  500. group string
  501. dateSlice []DateSlice
  502. timesOnDay []string
  503. expandValue interface{}
  504. expandTags []string
  505. weights float64
  506. }
  507. tests := []struct {
  508. name string
  509. args args
  510. want TimeArrow
  511. }{
  512. // TODO: Add test cases.
  513. }
  514. for _, tt := range tests {
  515. t.Run(tt.name, func(t *testing.T) {
  516. if got := CreateDateSliceTypePlan(tt.args.group, tt.args.dateSlice, tt.args.timesOnDay, tt.args.expandValue, tt.args.expandTags, tt.args.weights); !reflect.DeepEqual(got, tt.want) {
  517. t.Errorf("CreateDateSliceTypePlan() = %v, want %v", got, tt.want)
  518. }
  519. })
  520. }
  521. }
  522. func TestTimeArrowHelper_GetHitTimeArrow(t *testing.T) {
  523. type fields struct {
  524. GetData func(group string) (TimeArrows, error)
  525. }
  526. type args struct {
  527. t time.Time
  528. group string
  529. expandTags []string
  530. }
  531. tests := []struct {
  532. name string
  533. fields fields
  534. args args
  535. want *TimeArrow
  536. wantErr bool
  537. }{
  538. // TODO: Add test cases.
  539. }
  540. for _, tt := range tests {
  541. t.Run(tt.name, func(t *testing.T) {
  542. th := &TimeArrowHelper{
  543. GetData: tt.fields.GetData,
  544. }
  545. got, err := th.GetHitTimeArrow(tt.args.t, tt.args.group, tt.args.expandTags...)
  546. if (err != nil) != tt.wantErr {
  547. t.Errorf("TimeArrowHelper.GetHitTimeArrow() error = %v, wantErr %v", err, tt.wantErr)
  548. return
  549. }
  550. if !reflect.DeepEqual(got, tt.want) {
  551. t.Errorf("TimeArrowHelper.GetHitTimeArrow() = %v, want %v", got, tt.want)
  552. }
  553. })
  554. }
  555. }