|
| 1 | +package timeRangeLib |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestGetWindowForFixedTime(t *testing.T) { |
| 9 | + // Define the time range in UTC , as db will be having this time in UTC/GMT |
| 10 | + tr := TimeRange{ |
| 11 | + TimeFrom: time.Date(2024, 10, 14, 9, 0, 0, 0, time.UTC), // 9:00 AM UTC |
| 12 | + TimeTo: time.Date(2024, 10, 14, 17, 0, 0, 0, time.UTC), // 5:00 PM UTC |
| 13 | + } |
| 14 | + |
| 15 | + // Define the time zones with GMT offsets |
| 16 | + locBangui, _ := time.LoadLocation("Africa/Bangui") // GMT+01:00 |
| 17 | + locParamaribo, _ := time.LoadLocation("America/Paramaribo") // GMT-03:00 |
| 18 | + zeroTime := time.Time{} |
| 19 | + // Define test cases with different time zones |
| 20 | + testCases := []struct { |
| 21 | + name string |
| 22 | + targetTime time.Time |
| 23 | + expectedStart time.Time |
| 24 | + expectedEnd time.Time |
| 25 | + }{ |
| 26 | + |
| 27 | + { |
| 28 | + name: "Target time in Africa/Bangui (GMT+01:00), within range", |
| 29 | + targetTime: time.Date(2024, 10, 14, 11, 30, 0, 0, locBangui), // 11:30 AM GMT+01 (within range) |
| 30 | + expectedStart: time.Date(2024, 10, 14, 10, 0, 0, 0, locBangui), // 10:00 AM GMT+01 (adjusted) |
| 31 | + expectedEnd: time.Date(2024, 10, 14, 18, 0, 0, 0, locBangui), // 6:00 PM GMT+01 (adjusted) |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "Target time in Africa/Bangui (GMT+01:00), before range", |
| 35 | + targetTime: time.Date(2024, 10, 14, 8, 30, 0, 0, locBangui), // 11:30 AM GMT+01 (before range) |
| 36 | + expectedStart: time.Date(2024, 10, 14, 10, 0, 0, 0, locBangui), // 10:00 AM GMT+01 (adjusted) |
| 37 | + expectedEnd: time.Date(2024, 10, 14, 18, 0, 0, 0, locBangui), // 6:00 PM GMT+01 (adjusted) |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Target time in Africa/Bangui (GMT+01:00), after the range (Outside)", |
| 41 | + targetTime: time.Date(2024, 10, 14, 19, 30, 0, 0, locBangui), // 7:30 AM GMT+01 (outside) |
| 42 | + expectedStart: time.Time{}, // zeroTime |
| 43 | + expectedEnd: time.Time{}, // zeroTime |
| 44 | + }, |
| 45 | + |
| 46 | + { |
| 47 | + name: "Target time in America/Paramaribo (GMT-03:00), within range", |
| 48 | + targetTime: time.Date(2024, 10, 14, 07, 00, 0, 0, locParamaribo), // 7:00 AM GMT-03 (within range) |
| 49 | + expectedStart: time.Date(2024, 10, 14, 06, 00, 0, 0, locParamaribo), // 6:00 AM GMT-03 (adjusted) |
| 50 | + expectedEnd: time.Date(2024, 10, 14, 14, 00, 0, 0, locParamaribo), // 2:00 PM GMT-03 (adjusted) |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "Target time in America/Paramaribo (GMT-03:00), before range", |
| 54 | + targetTime: time.Date(2024, 10, 14, 04, 00, 0, 0, locParamaribo), // 4:00 AM GMT-03 (within range) |
| 55 | + expectedStart: time.Date(2024, 10, 14, 06, 00, 0, 0, locParamaribo), // 6:00 AM GMT-03 (adjusted) |
| 56 | + expectedEnd: time.Date(2024, 10, 14, 14, 00, 0, 0, locParamaribo), // 2:00 PM GMT-03 (adjusted) |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Target time in America/Paramaribo (GMT-03:00), after the range (Outside)", |
| 60 | + targetTime: time.Date(2024, 10, 14, 14, 30, 0, 0, locParamaribo), // 2:30 PM GMT-03 (outside) |
| 61 | + expectedStart: zeroTime, // zeroTime |
| 62 | + expectedEnd: zeroTime, // zeroTime |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + // Loop through the test cases |
| 67 | + for _, tc := range testCases { |
| 68 | + t.Run(tc.name, func(t *testing.T) { |
| 69 | + windowStart, windowEnd := tr.getWindowForFixedTime(tc.targetTime) |
| 70 | + if windowStart != tc.expectedStart || windowEnd != tc.expectedEnd { |
| 71 | + t.Errorf("Failed %s: got start=%v, end=%v; want start=%v, end=%v", |
| 72 | + tc.name, windowStart, windowEnd, tc.expectedStart, tc.expectedEnd) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments