|
| 1 | +import { describe, expect, it, test } from "@jest/globals"; |
| 2 | + |
| 3 | +import { Offering } from "../../src/types/generatorTypes"; |
| 4 | +import { |
| 5 | + canInsert, |
| 6 | + canInsertList, |
| 7 | + createOffering, |
| 8 | +} from "../../src/utils/generatorHelpers"; |
| 9 | + |
| 10 | +describe("canInsert function", () => { |
| 11 | + const offering1: Offering = createOffering({ |
| 12 | + id: 1, |
| 13 | + course_id: 101, |
| 14 | + day: "MO", |
| 15 | + start: "09:00:00", |
| 16 | + end: "10:00:00", |
| 17 | + }); |
| 18 | + const offering2: Offering = createOffering({ |
| 19 | + id: 2, |
| 20 | + course_id: 102, |
| 21 | + day: "MO", |
| 22 | + start: "10:00:00", |
| 23 | + end: "11:00:00", |
| 24 | + }); |
| 25 | + const offering3: Offering = createOffering({ |
| 26 | + id: 3, |
| 27 | + course_id: 103, |
| 28 | + day: "MO", |
| 29 | + start: "11:00:00", |
| 30 | + end: "12:00:00", |
| 31 | + }); |
| 32 | + |
| 33 | + it("should return true if there is no overlap with existing offerings", async () => { |
| 34 | + const toInsert: Offering = createOffering({ |
| 35 | + id: 4, |
| 36 | + course_id: 104, |
| 37 | + day: "MO", |
| 38 | + start: "12:00:00", |
| 39 | + end: "13:00:00", |
| 40 | + }); |
| 41 | + const curList: Offering[] = [offering1, offering2, offering3]; |
| 42 | + |
| 43 | + const result = await canInsert(toInsert, curList); |
| 44 | + |
| 45 | + expect(result).toBe(true); // No overlap, should return true |
| 46 | + }); |
| 47 | + |
| 48 | + it("should return false if there is an overlap with an existing offering", async () => { |
| 49 | + const toInsert: Offering = createOffering({ |
| 50 | + id: 4, |
| 51 | + course_id: 104, |
| 52 | + day: "MO", |
| 53 | + start: "09:30:00", |
| 54 | + end: "10:30:00", |
| 55 | + }); |
| 56 | + const curList: Offering[] = [offering1, offering2, offering3]; |
| 57 | + |
| 58 | + const result = await canInsert(toInsert, curList); |
| 59 | + |
| 60 | + expect(result).toBe(false); // There is an overlap with offering1, should return false |
| 61 | + }); |
| 62 | + |
| 63 | + it("should return true if the new offering starts after the last one ends", async () => { |
| 64 | + const toInsert: Offering = createOffering({ |
| 65 | + id: 4, |
| 66 | + course_id: 104, |
| 67 | + day: "MO", |
| 68 | + start: "13:00:00", |
| 69 | + end: "14:00:00", |
| 70 | + }); |
| 71 | + const curList: Offering[] = [offering1, offering2, offering3]; |
| 72 | + |
| 73 | + const result = await canInsert(toInsert, curList); |
| 74 | + |
| 75 | + expect(result).toBe(true); // No overlap, should return true |
| 76 | + }); |
| 77 | + |
| 78 | + it("should return true if the new offering ends before the first one starts", async () => { |
| 79 | + const toInsert: Offering = createOffering({ |
| 80 | + id: 4, |
| 81 | + course_id: 104, |
| 82 | + day: "MO", |
| 83 | + start: "07:00:00", |
| 84 | + end: "08:00:00", |
| 85 | + }); |
| 86 | + const curList: Offering[] = [offering1, offering2, offering3]; |
| 87 | + |
| 88 | + const result = await canInsert(toInsert, curList); |
| 89 | + |
| 90 | + expect(result).toBe(true); // No overlap, should return true |
| 91 | + }); |
| 92 | + |
| 93 | + it("should return false if the new offering is completely inside an existing one", async () => { |
| 94 | + const toInsert: Offering = createOffering({ |
| 95 | + id: 4, |
| 96 | + course_id: 104, |
| 97 | + day: "MO", |
| 98 | + start: "09:30:00", |
| 99 | + end: "09:45:00", |
| 100 | + }); |
| 101 | + const curList: Offering[] = [offering1, offering2, offering3]; |
| 102 | + |
| 103 | + const result = await canInsert(toInsert, curList); |
| 104 | + |
| 105 | + expect(result).toBe(false); // Overlaps with offering1, should return false |
| 106 | + }); |
| 107 | + |
| 108 | + it("should return true if the day is different (no overlap)", async () => { |
| 109 | + const toInsert: Offering = createOffering({ |
| 110 | + id: 4, |
| 111 | + course_id: 104, |
| 112 | + day: "TU", |
| 113 | + start: "09:00:00", |
| 114 | + end: "10:00:00", |
| 115 | + }); |
| 116 | + const curList: Offering[] = [offering1, offering2, offering3]; |
| 117 | + |
| 118 | + const result = await canInsert(toInsert, curList); |
| 119 | + |
| 120 | + expect(result).toBe(true); // Different day, no overlap |
| 121 | + }); |
| 122 | + |
| 123 | + it("special case", async () => { |
| 124 | + const toInsert: Offering = createOffering({ |
| 125 | + id: 1069, |
| 126 | + course_id: 1271, |
| 127 | + day: "TH", |
| 128 | + start: "05:00:00", |
| 129 | + end: "17:00:00", |
| 130 | + }); |
| 131 | + const offering11: Offering = createOffering({ |
| 132 | + id: 414, |
| 133 | + course_id: 337, |
| 134 | + day: "TU", |
| 135 | + start: "15:00:00", |
| 136 | + end: "16:00:00", |
| 137 | + }); |
| 138 | + const offering12: Offering = createOffering({ |
| 139 | + id: 415, |
| 140 | + course_id: 337, |
| 141 | + day: "TH", |
| 142 | + start: "15:00:00", |
| 143 | + end: "17:00:00", |
| 144 | + }); |
| 145 | + const offering13: Offering = createOffering({ |
| 146 | + id: 1052, |
| 147 | + course_id: 1271, |
| 148 | + day: "TU", |
| 149 | + start: "10:00:00", |
| 150 | + end: "11:00:00", |
| 151 | + }); |
| 152 | + const offering14: Offering = createOffering({ |
| 153 | + id: 1053, |
| 154 | + course_id: 1271, |
| 155 | + day: "TU", |
| 156 | + start: "09:00:00", |
| 157 | + end: "11:00:00", |
| 158 | + }); |
| 159 | + const curList: Offering[] = [ |
| 160 | + offering11, |
| 161 | + offering12, |
| 162 | + offering13, |
| 163 | + offering14, |
| 164 | + ]; |
| 165 | + |
| 166 | + const result = await canInsertList([toInsert], curList); |
| 167 | + |
| 168 | + expect(result).toBe(false); // Special bug-causing case |
| 169 | + }); |
| 170 | +}); |
0 commit comments