@@ -18,11 +18,11 @@ var testCourses []*schema.Course
1818var testSections []* schema.Section
1919var testProfessors []* schema.Professor
2020
21- // Map used to map index of test sections to test courses
21+ // Map index of test sections to test courses
2222var indexMap map [int ]int
2323
2424func init () {
25- // parse the test courses
25+ // Parse the test courses
2626 data , err := os .ReadFile ("./testdata/courses.json" )
2727 if err != nil {
2828 panic (err )
@@ -32,7 +32,7 @@ func init() {
3232 panic (err )
3333 }
3434
35- // parse the test sections
35+ // Parse the test sections
3636 data , err = os .ReadFile ("./testdata/sections.json" )
3737 if err != nil {
3838 panic (err )
@@ -42,7 +42,7 @@ func init() {
4242 panic (err )
4343 }
4444
45- // parse the test professors
45+ // Parse the test professors
4646 data , err = os .ReadFile ("./testdata/professors.json" )
4747 if err != nil {
4848 panic (err )
@@ -52,6 +52,7 @@ func init() {
5252 panic (err )
5353 }
5454
55+ // The correct mapping
5556 indexMap = map [int ]int {0 : 0 , 1 : 1 , 2 : 2 , 3 : 3 , 4 : 4 , 5 : 4 }
5657}
5758
@@ -142,20 +143,20 @@ func TestCourseReferencePass(t *testing.T) {
142143// - Course references non-existent section
143144// - Section doesn't reference back to same course
144145//
145- // This is fail type 1
146+ // This is fail: missing
146147func TestCourseReferenceFail1 (t * testing.T ) {
147148 for key , value := range indexMap {
148149 t .Run (fmt .Sprintf ("Section %v & course %v" , key , value ), func (t * testing.T ) {
149- testCourseReferenceFail (1 , value , key , t )
150+ testCourseReferenceFail ("missing" , value , key , t )
150151 })
151152 }
152153}
153154
154- // This is fail type 2
155+ // This is fail: modified
155156func TestCourseReferenceFail2 (t * testing.T ) {
156157 for key , value := range indexMap {
157158 t .Run (fmt .Sprintf ("Section %v & course %v" , key , value ), func (t * testing.T ) {
158- testCourseReferenceFail (2 , value , key , t )
159+ testCourseReferenceFail ("modified" , value , key , t )
159160 })
160161 }
161162}
@@ -192,6 +193,7 @@ func TestSectionReferenceProfPass(t *testing.T) {
192193
193194// Test section reference to professors, designed for fail case
194195func TestSectionReferenceProfFail (t * testing.T ) {
196+
195197 profIDMap := make (map [primitive.ObjectID ]string )
196198 profs := make (map [string ]* schema.Professor )
197199
@@ -257,23 +259,22 @@ func TestSectionReferenceCourse(t *testing.T) {
257259 }
258260}
259261
260- /* BELOW HERE ARE HELPER FUNCTION FOR TESTS ABOVE */
262+ /******** BELOW HERE ARE HELPER FUNCTION FOR TESTS ABOVE ******* */
261263
262- // Helper function
263264// Test if validate() throws erros when encountering duplicate
264265// Design for fail cases
265- func testDuplicateFail (objType string , index int , t * testing.T ) {
266+ func testDuplicateFail (objType string , ix int , t * testing.T ) {
266267 // the buffer used to capture the log output
267268 var logBuffer bytes.Buffer
268269 log .SetOutput (& logBuffer )
269270
270- // determine the expected msgs and panic msgs based on object type
271+ // Determine the expected messages and panic messages based on object type
271272 var expectedMsgs []string
272273 var panicMsg string
273274
274275 switch objType {
275276 case "course" :
276- failCourse := testCourses [index ]
277+ failCourse := testCourses [ix ]
277278
278279 // list of msgs it must print
279280 expectedMsgs = []string {
@@ -282,15 +283,15 @@ func testDuplicateFail(objType string, index int, t *testing.T) {
282283 }
283284 panicMsg = "Courses failed to validate!"
284285 case "section" :
285- failSection := testSections [index ]
286+ failSection := testSections [ix ]
286287
287288 expectedMsgs = []string {
288289 "Duplicate section found!" ,
289290 fmt .Sprintf ("Section 1: %v\n \n Section 2: %v" , failSection , failSection ),
290291 }
291292 panicMsg = "Sections failed to validate!"
292293 case "professor" :
293- failProf := testProfessors [index ]
294+ failProf := testProfessors [ix ]
294295
295296 expectedMsgs = []string {
296297 "Duplicate professor found!" ,
@@ -302,14 +303,14 @@ func testDuplicateFail(objType string, index int, t *testing.T) {
302303 defer func () {
303304 logOutput := logBuffer .String () // log output after running the function
304305
305- // log output needs to contain lines in the list
306+ // Log output needs to contain lines in the list
306307 for _ , msg := range expectedMsgs {
307308 if ! strings .Contains (logOutput , msg ) {
308309 t .Errorf ("Exptected the message for %v: %v" , objType , msg )
309310 }
310311 }
311312
312- // test whether func panics and sends the correct panic msg
313+ // Test whether func panics and sends the correct panic msg
313314 if r := recover (); r == nil {
314315 t .Errorf ("The function didn't panic for %v" , objType )
315316 } else {
@@ -323,18 +324,17 @@ func testDuplicateFail(objType string, index int, t *testing.T) {
323324 // Run func
324325 switch objType {
325326 case "course" :
326- valDuplicateCourses (testCourses [index ], testCourses [index ])
327+ valDuplicateCourses (testCourses [ix ], testCourses [ix ])
327328 case "section" :
328- valDuplicateSections (testSections [index ], testSections [index ])
329+ valDuplicateSections (testSections [ix ], testSections [ix ])
329330 case "professor" :
330- valDuplicateProfs (testProfessors [index ], testProfessors [index ])
331+ valDuplicateProfs (testProfessors [ix ], testProfessors [ix ])
331332 }
332333}
333334
334- // Helper function
335335// Test if func doesn't log anything and doesn't panic.
336336// Design for pass cases
337- func testDuplicatePass (objType string , index1 int , index2 int , t * testing.T ) {
337+ func testDuplicatePass (objType string , ix1 int , ix2 int , t * testing.T ) {
338338 // Buffer to capture the output
339339 var logBuffer bytes.Buffer
340340 log .SetOutput (& logBuffer )
@@ -349,45 +349,45 @@ func testDuplicatePass(objType string, index1 int, index2 int, t *testing.T) {
349349 }
350350 }()
351351
352- // Run func according to the object type. Choose pair of objects which are not duplicate
352+ // Run func according to the object type.
353+ // Choose pair of objects which are not duplicate
353354 switch objType {
354355 case "course" :
355- valDuplicateCourses (testCourses [index1 ], testCourses [index2 ])
356+ valDuplicateCourses (testCourses [ix1 ], testCourses [ix2 ])
356357 case "section" :
357- valDuplicateSections (testSections [index1 ], testSections [index2 ])
358+ valDuplicateSections (testSections [ix1 ], testSections [ix2 ])
358359 case "professor" :
359- valDuplicateProfs (testProfessors [index1 ], testProfessors [index2 ])
360+ valDuplicateProfs (testProfessors [ix1 ], testProfessors [ix2 ])
360361 }
361362}
362363
363- // Helper function for the case of course reference that fails
364- // failType: 1 means it lacks one sections
365- // failType: 2 means one section's course reference has been modified
366- func testCourseReferenceFail (failType int , courseIndex int , sectionIndex int , t * testing.T ) {
364+ // fail = "missing" means it lacks one sections
365+ // fail = "modified" means one section's course reference has been modified
366+ func testCourseReferenceFail (fail string , courseIx int , sectionIx int , t * testing.T ) {
367367 sectionMap := make (map [primitive.ObjectID ]* schema.Section )
368368
369369 var sectionID , originalID primitive.ObjectID // used to store IDs of modified sections
370370
371371 // Build the failed section map based on fail type
372- if failType == 1 {
373- // misses a section
372+ if fail == "missing" {
373+ // Misses a section
374374 for i , section := range testSections {
375- if sectionIndex != i {
375+ if sectionIx != i {
376376 sectionMap [section .Id ] = section
377377 } else {
378378 sectionID = section .Id // Nonexistent ID referenced by course
379379 }
380380 }
381- } else {
382- // one section doesn't reference to correct courses
381+ } else if fail == "modified" {
382+ // One section doesn't reference to correct courses
383383 for i , section := range testSections {
384384 sectionMap [section .Id ] = section
385- if sectionIndex == i {
386- // save the section ID and original course reference to be restored later on
385+ if sectionIx == i {
386+ // Save the section ID and original course reference to be restored later on
387387 sectionID = section .Id
388388 originalID = section .Course_reference
389389
390- // modify part
390+ // Modified part
391391 sectionMap [section .Id ].Course_reference = primitive .NewObjectID ()
392392 }
393393 }
@@ -399,16 +399,16 @@ func testCourseReferenceFail(failType int, courseIndex int, sectionIndex int, t
399399 // The course that references nonexistent stuff
400400 var failCourse * schema.Course
401401
402- if failType == 1 {
403- failCourse = testCourses [courseIndex ]
402+ if fail == "missing" {
403+ failCourse = testCourses [courseIx ]
404404
405405 expectedMsgs = []string {
406406 fmt .Sprintf ("Nonexistent section reference found for %v%v!" , failCourse .Subject_prefix , failCourse .Course_number ),
407407 fmt .Sprintf ("Referenced section ID: %s\n Course ID: %s" , sectionID , failCourse .Id ),
408408 }
409409 } else {
410- failCourse = testCourses [courseIndex ]
411- failSection := testSections [sectionIndex ]
410+ failCourse = testCourses [courseIx ]
411+ failSection := testSections [sectionIx ]
412412
413413 expectedMsgs = []string {
414414 fmt .Sprintf ("Inconsistent section reference found for %v%v! The course references the section, but not vice-versa!" ,
@@ -431,8 +431,8 @@ func testCourseReferenceFail(failType int, courseIndex int, sectionIndex int, t
431431 }
432432 }
433433
434- // restore to original course reference of modified section (if needed)
435- if failType == 2 {
434+ // Restore to original course reference of modified section (if needed)
435+ if fail == "modified" {
436436 sectionMap [sectionID ].Course_reference = originalID
437437 }
438438
0 commit comments