-
Notifications
You must be signed in to change notification settings - Fork 3
/
calculation_test.go
752 lines (640 loc) · 26.4 KB
/
calculation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
package timekit
import (
"testing"
"time"
)
// Developers Note:
// Special thanks to the following article which helped explain how to stub
// out the `time.Now()` function in Golang.
// https://labs.yulrizka.com/en/stubbing-time-dot-now-in-golang/
func TestFirstDayOfLastYear(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2000, 1, 1, 0, 0, 0, 0, loc) // Jan 1st 2000
}
actual := FirstDayOfLastYear(timeFn)
expected := time.Date(1999, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 1999
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestFirstDayOfThisYear(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2000, 7, 1, 0, 0, 0, 0, loc) // July 1st 2000
}
actual := FirstDayOfThisYear(timeFn)
expected := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 2000
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestFirstDayOfNextYear(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2022, 7, 1, 0, 0, 0, 0, loc) // July 1st 2022
}
actual := FirstDayOfNextYear(timeFn)
expected := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 2023
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestFirstDayOfLastMonth(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2000, 1, 1, 0, 0, 0, 0, loc) // Jan 1st 2000
}
actual := FirstDayOfLastMonth(timeFn)
expected := time.Date(1999, 12, 1, 0, 0, 0, 0, time.UTC) // Dec 1st 1999
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestFirstDayOfThisMonth(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2000, 1, 20, 0, 0, 0, 0, loc) // Jan 20th 2000
}
actual := FirstDayOfThisMonth(timeFn)
expected := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 2000
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestFirstDayOfNextMonth(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2000, 1, 20, 0, 0, 0, 0, loc) // Jan 20th 2000
}
actual := FirstDayOfNextMonth(timeFn)
expected := time.Date(2000, 2, 1, 0, 0, 0, 0, time.UTC) // Feb 1st 2000
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestMidnightYesterday(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2000, 1, 20, 0, 0, 0, 0, loc) // Jan 20th 2000 12AM
}
actual := MidnightYesterday(timeFn)
expected := time.Date(2000, 1, 19, 0, 0, 0, 0, time.UTC) // Jan 19th 2000 12AM
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestMidnight(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2000, 1, 20, 5, 0, 0, 0, loc) // Jan 20th 2000 5AM
}
actual := Midnight(timeFn)
expected := time.Date(2000, 1, 20, 0, 0, 0, 0, time.UTC) // Jan 20th 2000 12AM
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestMidnightTomorrow(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2000, 1, 20, 5, 0, 0, 0, loc) // Jan 20th 2000 5AM
}
actual := MidnightTomorrow(timeFn)
expected := time.Date(2000, 1, 21, 0, 0, 0, 0, time.UTC) // Jan 21st 2000 12AM
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestNoon(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2000, 1, 20, 5, 0, 0, 0, loc) // Jan 20th 2000 5AM
}
actual := Noon(timeFn)
expected := time.Date(2000, 1, 20, 12, 0, 0, 0, time.UTC) // Jan 200th 2000 12PM
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestFirstDayOfLastISOWeek(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2022, 1, 9, 1, 0, 0, 0, loc) // Sunday Jan 9th
}
actual := FirstDayOfLastISOWeek(timeFn)
expected := time.Date(2021, 12, 27, 0, 0, 0, 0, time.UTC) // Monday Jan 3ed (Midnight - Start of day)
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestFirstDayOfThisISOWeek(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2022, 1, 9, 1, 0, 0, 0, loc) // Sunday Jan 9th
}
actual := FirstDayOfThisISOWeek(timeFn)
expected := time.Date(2022, 1, 3, 0, 0, 0, 0, time.UTC) // Monday Jan 3ed (Midnight - Start of day)
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestLastDayOfThisISOWeek(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2022, 1, 7, 1, 0, 0, 0, loc) // Friday Jan 7th
}
actual := LastDayOfThisISOWeek(timeFn)
expected := time.Date(2022, 1, 9, 0, 0, 0, 0, loc) // Sunday Jan 9th (Midnight - Start of day)
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestFirstDayOfNextISOWeekSample1(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2022, 1, 7, 1, 0, 0, 0, loc) // Friday Jan 7th
}
actual := FirstDayOfNextISOWeek(timeFn)
expected := time.Date(2022, 1, 10, 0, 0, 0, 0, loc) // Monday Jan 10th (Midnight - Start of day)
if actual != expected {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestFirstDayOfNextISOWeekSample2(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc, _ := time.LoadLocation("America/Toronto")
timeFn := func() time.Time {
return time.Date(2023, 12, 18, 9, 0, 0, 0, loc) // Monday Dec 18th - 9 AM
}
actual := FirstDayOfNextISOWeek(timeFn)
expected := time.Date(2023, 12, 25, 0, 0, 0, 0, loc) // Monday Dec 25th (Midnight - Start of day)
if actual != expected {
t.Errorf("Incorrect date when given %s, got %s but was expecting %s", timeFn(), actual, expected)
}
}
func TestIsFirstDayOfYear(t *testing.T) {
loc := time.UTC // closure can be used if necessary
t1 := time.Date(2022, 1, 7, 1, 0, 0, 0, loc) // Jan 7th
t2 := time.Date(2022, 1, 1, 1, 0, 0, 0, loc) // Jan 1st
isT1OK := IsFirstDayOfYear(t1)
if isT1OK != false {
t.Errorf("Incorrect boolean, got %v but was expecting %v", isT1OK, false)
}
isT2OK := IsFirstDayOfYear(t2)
if isT2OK != true {
t.Errorf("Incorrect boolean, got %v but was expecting %v", isT1OK, true)
}
}
func TestGetWeekNumberFromDate(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
// Week 52.
t1 := time.Date(2022, 1, 1, 1, 0, 0, 0, loc) // Friday Jan 1st.
wkn := GetWeekNumberFromDate(t1)
if wkn != 52 {
t.Errorf("Incorrect integer, got %v but was expecting %v", wkn, 52)
}
// Week 1.
t2 := time.Date(2022, 1, 7, 1, 0, 0, 0, loc) // Friday Jan 7th
wkn = GetWeekNumberFromDate(t2)
if wkn != 1 {
t.Errorf("Incorrect integer, got %v but was expecting %v", wkn, 1)
}
// Week 52 (again).
t3 := time.Date(2022, 12, 30, 1, 0, 0, 0, loc) // December 30th
wkn = GetWeekNumberFromDate(t3)
if wkn != 52 {
t.Errorf("Incorrect integer, got %v but was expecting %v", wkn, 52)
}
}
func TestGetFirstDateFromWeekAndYear(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
// Week 1
expected := time.Date(2022, 1, 3, 1, 0, 0, 0, loc)
actual := GetFirstDateFromWeekAndYear(1, 2022, loc)
if expected != actual {
t.Errorf("Incorrect date, got %v but was expecting %v", actual, expected)
}
// Week 4
expected = time.Date(2022, 1, 24, 1, 0, 0, 0, loc)
actual = GetFirstDateFromWeekAndYear(4, 2022, loc)
if expected != actual {
t.Errorf("Incorrect date, got %v but was expecting %v", actual, expected)
}
// Week 52 (from last week)
expected = time.Date(2022, 1, 1, 1, 0, 0, 0, loc)
actual = GetFirstDateFromWeekAndYear(52, 2022, loc)
if expected != actual {
t.Errorf("Incorrect date, got %v but was expecting %v", actual, expected)
}
}
func TestGetFirstDateFromMonthAndYear(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
// Week 1
expected := time.Date(2022, 1, 1, 1, 0, 0, 0, loc)
actual := GetFirstDateFromMonthAndYear(1, 2022, loc)
if expected != actual {
t.Errorf("Incorrect date, got %v but was expecting %v", actual, expected)
}
}
func TestGetWeekNumberFromTotalDaysCount(t *testing.T) {
type ExpectedTestResult struct {
StartDay uint64
EndDay uint64
Week uint64
}
testData := []*ExpectedTestResult{
{0, 0, 0}, // This is a zero case to test for.
// DEVELOPERS NOTE: Here are some hard-coded test values. Uncomment these
// if you'd like to run for human-entered values instead of the for loop
// below.
// {1, 7, 1}, {8, 14, 2}, {15, 21, 3}, {22, 28, 4}, {29, 35, 5},
// {36, 42, 6}, {43, 49, 7},
}
// The following code will populate our test weeks to verify our code works.
days := uint64(1)
for wk := uint64(1); wk <= 52; wk++ {
testDatum := &ExpectedTestResult{days, days + 6, wk}
days += 7
testData = append(testData, testDatum)
}
// Iterate through all the test data on our function and verify that the
// function works correctly.
for _, testDatum := range testData {
for d := uint64(testDatum.StartDay); d <= testDatum.EndDay; d++ {
wk := GetWeekNumberFromTotalDaysCount(d)
if wk != testDatum.Week {
t.Errorf("Incorrect week number for day #%d, got week %d but was expecting week %d", d, wk, testDatum.Week)
return
}
}
}
}
func TestGetDayOfWeekUsingTomohikoSakamotoAlgorithm(t *testing.T) {
// Value of `1` for input of `14/09/1998` was taken from this link:
// for https://www.geeksforgeeks.org/find-day-of-the-week-for-a-given-date/
actualDay := GetDayOfWeekUsingTomohikoSakamotoAlgorithm(14, 9, 1998)
if actualDay != 1 {
t.Errorf("Incorrect actual day #%d, expecting day %d", actualDay, 1)
}
// Returned value of `2` for input `15/11/2022` was taken by looking in the
// calendar.
actualDay = GetDayOfWeekUsingTomohikoSakamotoAlgorithm(15, 11, 2022)
if actualDay != 2 {
t.Errorf("Incorrect actual day #%d, expecting day %d", actualDay, 1)
}
// Used calendar to for test.
actualDay = GetDayOfWeekUsingTomohikoSakamotoAlgorithm(1, 1, 2023)
if actualDay != 0 { // 0 = Sunday
t.Errorf("Incorrect actual day #%d, expecting day %d", actualDay, 1)
}
}
func TestAddWeeksToTime(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
// Week 1
expected := time.Date(2022, 1, 8, 1, 0, 0, 0, loc)
dt := time.Date(2022, 1, 1, 1, 0, 0, 0, loc)
actual := AddWeeksToTime(dt, 1)
if expected != actual {
t.Errorf("Incorrect date, got %v but was expecting %v", actual, expected)
}
// Week 2
expected = time.Date(2022, 1, 15, 1, 0, 0, 0, loc)
dt = time.Date(2022, 1, 1, 1, 0, 0, 0, loc)
actual = AddWeeksToTime(dt, 2)
if expected != actual {
t.Errorf("Incorrect date, got %v but was expecting %v", actual, expected)
}
// Week 3
expected = time.Date(2022, 1, 22, 1, 0, 0, 0, loc)
dt = time.Date(2022, 1, 1, 1, 0, 0, 0, loc)
actual = AddWeeksToTime(dt, 3)
if expected != actual {
t.Errorf("Incorrect date, got %v but was expecting %v", actual, expected)
}
}
func timeEqual(a, b []time.Time) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func TestGetDatesForWeekdaysBetweenRange(t *testing.T) {
startDateTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 2023
endDateTime := time.Date(2023, 1, 14, 0, 0, 0, 0, time.UTC) // Jan 14th 2023
////
//// Case 1
////
weekdays := []int8{0, 1} // 0=Sunday, 1=Monday
expected := GetDatesForWeekdaysBetweenRange(startDateTime, endDateTime, weekdays)
actual := []time.Time{
time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), // Sunday
time.Date(2023, 1, 2, 0, 0, 0, 0, time.UTC), // Monday
time.Date(2023, 1, 8, 0, 0, 0, 0, time.UTC), // Sunday
time.Date(2023, 1, 9, 0, 0, 0, 0, time.UTC), // Monday
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
////
//// Case 2
////
weekdays = []int8{2} // 2=Tuesday
expected = GetDatesForWeekdaysBetweenRange(startDateTime, endDateTime, weekdays)
actual = []time.Time{
time.Date(2023, 1, 3, 0, 0, 0, 0, time.UTC), // Tuesday
time.Date(2023, 1, 10, 0, 0, 0, 0, time.UTC), // Tuesday
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
////
//// Case X: Do you have an idea to help improve the quality? Feel free to
//// submit an issue via https://github.com/bartmika/timekit/issues
//// or contribute via https://github.com/bartmika/timekit/compare.
////
}
func TestIsTimeOnFirstWeekOfMonth(t *testing.T) {
if IsTimeOnFirstWeekOfMonth(time.Date(2022, 12, 3, 0, 0, 0, 0, time.UTC)) == false { // Dec 3ed 2022 | First Week
t.Error("Incorrect value - this is the first week!")
}
if IsTimeOnFirstWeekOfMonth(time.Date(2022, 12, 7, 0, 0, 0, 0, time.UTC)) == true { // Dec 12th 2022 | Second Week
t.Error("Incorrect value - this is not the first week")
}
if IsTimeOnFirstWeekOfMonth(time.Date(2022, 25, 7, 0, 0, 0, 0, time.UTC)) == true { // Dec 25rh 2022 | Last Week
t.Error("Incorrect value - this is not the first week")
}
if IsTimeOnFirstWeekOfMonth(time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC)) == false { // April 1st 2023 | First Week | Good test case because lands on a Saturday!
t.Error("Incorrect value - this is on the first week")
}
}
func TestIsTimeOnLastWeekOfMonth(t *testing.T) {
if IsTimeOnLastWeekOfMonth(time.Date(2023, 4, 30, 0, 0, 0, 0, time.UTC)) == false { // April 30th, 2023 | Last week | On a Sunday.
t.Error("Incorrect value - this is the last week!")
}
if IsTimeOnLastWeekOfMonth(time.Date(2023, 4, 17, 0, 0, 0, 0, time.UTC)) == true { // April 17th, 2023
t.Error("Incorrect value - this is not on the last week!")
}
if IsTimeOnLastWeekOfMonth(time.Date(2023, 3, 27, 0, 0, 0, 0, time.UTC)) == false { // Marsh 27th, 2023 | Last week | On a Monday.
t.Error("Incorrect value - this is the last week!")
}
if IsTimeOnLastWeekOfMonth(time.Date(2022, 12, 3, 0, 0, 0, 0, time.UTC)) == true { // Dec 3ed 2022 | First Week
t.Error("Incorrect value - this is not on the last week!")
}
}
func TestGetDatesByWeeklyBasedRecurringSchedule(t *testing.T) {
////
//// Case 1: Run every 2 weeks for a total of 4 weeks selecting only Sunday
//// and mondays in the week.
////
weekdays := []int8{0, 1} // 0=Sunday, 1=Monday
totalWeeks := int(4) // There will be a maximum of 4 weeks in our schedule.
weekFrequency := int(2) // A.k.a. every 2 weeks
// --- Start on a Sunday. --- //
startDateTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 2023
actual := GetDatesByWeeklyBasedRecurringSchedule(startDateTime, weekdays, totalWeeks, weekFrequency)
expected := []time.Time{
time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC), // Sunday
time.Date(2023, 1, 2, 0, 0, 0, 0, time.UTC), // Monday
time.Date(2023, 1, 15, 0, 0, 0, 0, time.UTC), // Sunday
time.Date(2023, 1, 16, 0, 0, 0, 0, time.UTC), // Monday
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
// --- Start in the middle of the week. --- //
startDateTime = time.Date(2023, 1, 4, 0, 0, 0, 0, time.UTC) // Jan 4th 2023
actual = GetDatesByWeeklyBasedRecurringSchedule(startDateTime, weekdays, totalWeeks, weekFrequency)
expected = []time.Time{
time.Date(2023, 1, 8, 0, 0, 0, 0, time.UTC), // Sunday
time.Date(2023, 1, 9, 0, 0, 0, 0, time.UTC), // Monday
time.Date(2023, 1, 22, 0, 0, 0, 0, time.UTC), // Sunday
time.Date(2023, 1, 23, 0, 0, 0, 0, time.UTC), // Monday
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
// --- Start at the end of the week. --- //
startDateTime = time.Date(2023, 1, 6, 0, 0, 0, 0, time.UTC) // Jan 4th 2023
actual = GetDatesByWeeklyBasedRecurringSchedule(startDateTime, weekdays, totalWeeks, weekFrequency)
expected = []time.Time{
time.Date(2023, 1, 8, 0, 0, 0, 0, time.UTC), // Sunday
time.Date(2023, 1, 9, 0, 0, 0, 0, time.UTC), // Monday
time.Date(2023, 1, 22, 0, 0, 0, 0, time.UTC), // Sunday
time.Date(2023, 1, 23, 0, 0, 0, 0, time.UTC), // Monday
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
////
//// Case 2: Run every week for a total of 5 weeks selecting only Friday
//// and Saturday every week. Start first day in month.
///
weekdays = []int8{5, 6} // 5=Friday, 6=Saturday
totalWeeks = int(4) // There will be a maximum of 5 weeks in our schedule.
weekFrequency = int(1) // A.k.a. every week
startDateTime = time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 2023
actual = GetDatesByWeeklyBasedRecurringSchedule(startDateTime, weekdays, totalWeeks, weekFrequency)
expected = []time.Time{
time.Date(2023, 1, 6, 0, 0, 0, 0, time.UTC), // Friday
time.Date(2023, 1, 7, 0, 0, 0, 0, time.UTC), // Saturday
time.Date(2023, 1, 13, 0, 0, 0, 0, time.UTC), // Friday
time.Date(2023, 1, 14, 0, 0, 0, 0, time.UTC), // Saturday
time.Date(2023, 1, 20, 0, 0, 0, 0, time.UTC), // Friday
time.Date(2023, 1, 21, 0, 0, 0, 0, time.UTC), // Saturday
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
////
//// Case X: Do you have an idea to help improve the quality? Feel free to
//// submit an issue via https://github.com/bartmika/timekit/issues
//// or contribute via https://github.com/bartmika/timekit/compare.
////
}
func TestGetDatesForExactDayByMonthlyBasedRecurringSchedule(t *testing.T) {
////
//// Test 1
////
totalMonths := int(4) // There will be a maximum of 4 months in our schedule.
startDateTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 2023
actual := GetDatesForExactDayByMonthlyBasedRecurringSchedule(startDateTime, totalMonths, 1)
expected := []time.Time{
time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC),
time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC),
time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC),
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
////
//// Test 2
////
totalMonths = int(2) // There will be a maximum of 2 months in our schedule.
startDateTime = time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 2023
actual = GetDatesForExactDayByMonthlyBasedRecurringSchedule(startDateTime, totalMonths, 1)
expected = []time.Time{
time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC),
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
////
//// Test 3
////
totalMonths = int(1) // There will be a maximum of 2 months in our schedule.
startDateTime = time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 2023
actual = GetDatesForExactDayByMonthlyBasedRecurringSchedule(startDateTime, totalMonths, 1)
expected = []time.Time{
time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
////
//// Test 4
////
totalMonths = int(3)
startDateTime = time.Date(2006, 1, 2, 15, 04, 05, 0, time.UTC) // Jan 1st 2023
actual = GetDatesForExactDayByMonthlyBasedRecurringSchedule(startDateTime, totalMonths, 1)
expected = []time.Time{
time.Date(2006, 2, 1, 15, 4, 5, 0, time.UTC),
time.Date(2006, 3, 1, 15, 4, 5, 0, time.UTC),
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
////
//// Test X: Do you have an idea to help improve the quality? Feel free to
//// submit an issue via https://github.com/bartmika/timekit/issues
//// or contribute via https://github.com/bartmika/timekit/compare.
////
}
func TestGetDatesForFirstDayByMonthlyBasedRecurringSchedule(t *testing.T) {
////
//// Case 1: Test.
////
totalMonths := int(4) // There will be a maximum of 4 months in our schedule.
startDateTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 2023
actual := GetDatesForFirstWeekDayByMonthlyBasedRecurringSchedule(startDateTime, totalMonths, 1) // 1 = Monday
expected := []time.Time{
time.Date(2023, 1, 2, 0, 0, 0, 0, time.UTC),
time.Date(2023, 2, 6, 0, 0, 0, 0, time.UTC),
time.Date(2023, 3, 6, 0, 0, 0, 0, time.UTC),
time.Date(2023, 4, 3, 0, 0, 0, 0, time.UTC),
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
////
//// Case 2: Another test.
////
totalMonths = int(4) // There will be a maximum of 4 months in our schedule.
startDateTime = time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 2023
actual = GetDatesForFirstWeekDayByMonthlyBasedRecurringSchedule(startDateTime, totalMonths, 6) // 6 = Saturday
expected = []time.Time{
time.Date(2023, 1, 7, 0, 0, 0, 0, time.UTC),
time.Date(2023, 2, 4, 0, 0, 0, 0, time.UTC),
time.Date(2023, 3, 4, 0, 0, 0, 0, time.UTC),
time.Date(2023, 4, 1, 0, 0, 0, 0, time.UTC),
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
////
//// Case X: Do you have an idea to help improve the quality? Feel free to
//// submit an issue via https://github.com/bartmika/timekit/issues
//// or contribute via https://github.com/bartmika/timekit/compare.
////
}
func TestGetDatesForLastWeekDayByMonthlyBasedRecurringSchedule(t *testing.T) {
////
//// Case 1: Test.
////
totalMonths := int(4) // There will be a maximum of 4 months in our schedule.
startDateTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) // Jan 1st 2023
actual := GetDatesForLastWeekDayByMonthlyBasedRecurringSchedule(startDateTime, totalMonths, 1) // 1 = Monday
expected := []time.Time{
time.Date(2023, 1, 30, 0, 0, 0, 0, time.UTC),
time.Date(2023, 2, 27, 0, 0, 0, 0, time.UTC),
time.Date(2023, 3, 27, 0, 0, 0, 0, time.UTC),
// time.Date(2023, 4, 24, 0, 0, 0, 0, time.UTC), This will not exist because we have a single Sunday 30th for ISO week.
}
if timeEqual(expected, actual) == false {
t.Errorf("Incorrect date, got %s but was expecting %s", actual, expected)
}
}
func TestGetHourRange(t *testing.T) {
loc := time.UTC // closure can be used if necessary
given := time.Date(2023, 12, 18, 9, 30, 0, 0, loc) // Monday Dec 18th - 9:30 AM
act1, act2 := GetHourRange(given)
exp1 := time.Date(2023, 12, 18, 9, 0, 0, 0, loc) // Monday Dec 18th - 9 AM
exp2 := time.Date(2023, 12, 18, 10, 0, 0, 0, loc) // Monday Dec 18th - 10 AM
if exp1 != act1 {
t.Errorf("Incorrect date, got %s but was expecting %s", act1, exp1)
}
if exp2 != act2 {
t.Errorf("Incorrect date, got %s but was expecting %s", act2, exp2)
}
}
func TestHourRangeForNow(t *testing.T) {
// Stub out the `time.Now()` function with our custom value so we can
// simulate being in this current data.
loc := time.UTC // closure can be used if necessary
timeFn := func() time.Time {
return time.Date(2023, 12, 18, 9, 30, 0, 0, loc) // Monday Dec 18th - 9:30 AM
}
act1, act2 := HourRangeForNow(timeFn)
exp1 := time.Date(2023, 12, 18, 9, 0, 0, 0, loc) // Monday Dec 18th - 9 AM
exp2 := time.Date(2023, 12, 18, 10, 0, 0, 0, loc) // Monday Dec 18th - 10 AM
if exp1 != act1 {
t.Errorf("Incorrect date, got %s but was expecting %s", act1, exp1)
}
if exp2 != act2 {
t.Errorf("Incorrect date, got %s but was expecting %s", act2, exp2)
}
}