时间选定库
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.

548 lines
11 KiB

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