forked from UTDNebula/api-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_test.go
452 lines (384 loc) · 12.1 KB
/
validator_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
package parser
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"testing"
"github.com/UTDNebula/nebula-api/api/schema"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// Globals for testing these validation units
var testCourses []*schema.Course
var testSections []*schema.Section
var testProfessors []*schema.Professor
// Map used to map index of test sections to test courses
var indexMap map[int]int
func init() {
// parse the test courses
data, err := os.ReadFile("./testdata/courses.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(data, &testCourses)
if err != nil {
panic(err)
}
// parse the test sections
data, err = os.ReadFile("./testdata/sections.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(data, &testSections)
if err != nil {
panic(err)
}
// parse the test professors
data, err = os.ReadFile("./testdata/professors.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(data, &testProfessors)
if err != nil {
panic(err)
}
indexMap = map[int]int{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 4}
}
// Test duplicate courses. Designed for fail cases
func TestDuplicateCoursesFail(t *testing.T) {
for i := range len(testCourses) {
t.Run(fmt.Sprintf("Duplicate course %v", i), func(t *testing.T) {
testDuplicateFail("course", i, t)
})
}
}
// Test duplicate sections. Designed for fail cases
func TestDuplicateSectionsFail(t *testing.T) {
for i := range len(testSections) {
t.Run(fmt.Sprintf("Duplicate section %v", i), func(t *testing.T) {
testDuplicateFail("section", i, t)
})
}
}
// Test duplicate professors . Designed for fail cases
func TestDuplicateProfFail(t *testing.T) {
for i := range len(testProfessors) {
t.Run(fmt.Sprintf("Duplicate professor %v", i), func(t *testing.T) {
testDuplicateFail("professor", i, t)
})
}
}
// Test duplicate courses. Designed for pass case
func TestDuplicateCoursesPass(t *testing.T) {
for i := range len(testCourses) - 1 {
t.Run(fmt.Sprintf("Duplicate courses %v, %v", i, i+1), func(t *testing.T) {
testDuplicatePass("course", i, i+1, t)
})
}
}
// Test duplicate sections. Designed for pass cases
func TestDuplicateSectionsPass(t *testing.T) {
for i := range len(testSections) - 1 {
t.Run(fmt.Sprintf("Duplicate sections %v, %v", i, i+1), func(t *testing.T) {
testDuplicatePass("section", i, i+1, t)
})
}
}
// Test duplicate professors. Designed for pass cases
func TestDuplicateProfPass(t *testing.T) {
for i := range len(testProfessors) - 1 {
t.Run(fmt.Sprintf("Duplicate professors %v, %v", i, i+1), func(t *testing.T) {
testDuplicatePass("professor", i, i+1, t)
})
}
}
// Test if course references to anything nonexistent. Designed for pass case
func TestCourseReferencePass(t *testing.T) {
sectionMap := make(map[primitive.ObjectID]*schema.Section)
for _, section := range testSections {
sectionMap[section.Id] = section
}
// Buffer to capture the output
var logBuffer bytes.Buffer
log.SetOutput(&logBuffer)
defer func() {
logOutput := logBuffer.String()
if logOutput != "" {
t.Errorf("Expected nothing printed in log")
}
if r := recover(); r != nil {
t.Errorf("The function panic unexpectedly for course")
}
}()
// Run func
for _, course := range testCourses {
valCourseReference(course, sectionMap)
}
}
// Test if function log expected msgs when course references non-existent sections
// 2 types of fail:
// - Course references non-existent section
// - Section doesn't reference back to same course
//
// This is fail type 1
func TestCourseReferenceFail1(t *testing.T) {
for key, value := range indexMap {
t.Run(fmt.Sprintf("Section %v & course %v", key, value), func(t *testing.T) {
testCourseReferenceFail(1, value, key, t)
})
}
}
// This is fail type 2
func TestCourseReferenceFail2(t *testing.T) {
for key, value := range indexMap {
t.Run(fmt.Sprintf("Section %v & course %v", key, value), func(t *testing.T) {
testCourseReferenceFail(2, value, key, t)
})
}
}
// Test section reference to professor, designed for pass case
func TestSectionReferenceProfPass(t *testing.T) {
// Build profIDMap & profs
profIDMap := make(map[primitive.ObjectID]string)
profs := make(map[string]*schema.Professor)
for _, professor := range testProfessors {
profIDMap[professor.Id] = professor.First_name + professor.Last_name
profs[professor.First_name+professor.Last_name] = professor
}
var logBuffer bytes.Buffer
log.SetOutput(&logBuffer)
defer func() {
logOutput := logBuffer.String()
if logOutput != "" {
t.Errorf("Expected nothing printed in log")
}
if r := recover(); r != nil {
t.Errorf("The function panic unexpectedly for section")
}
}()
for _, section := range testSections {
valSectionReferenceProf(section, profs, profIDMap)
}
}
// Test section reference to professors, designed for fail case
func TestSectionReferenceProfFail(t *testing.T) {
profIDMap := make(map[primitive.ObjectID]string)
profs := make(map[string]*schema.Professor)
for i, professor := range testProfessors {
if i != 0 {
profIDMap[professor.Id] = professor.First_name + professor.Last_name
profs[professor.First_name+professor.Last_name] = professor
}
}
var logBuffer bytes.Buffer
log.SetOutput(&logBuffer)
defer func() {
logOutput := logBuffer.String()
for _, msg := range []string{
"Nonexistent professor reference found for section ID ObjectID(\"67d07ee0c972c18731e23bea\")!",
"Referenced professor ID: ObjectID(\"67d07ee0c972c18731e23beb\")",
} {
if !strings.Contains(logOutput, msg) {
t.Errorf("The function didn't log correct message. Expected \"%v\"", msg)
}
}
if r := recover(); r == nil {
t.Errorf("The function didn't panic")
} else {
if r != "Sections failed to validate!" {
t.Errorf("The function panic the wrong message")
}
}
}()
for _, section := range testSections {
valSectionReferenceProf(section, profs, profIDMap)
}
}
// Test section reference to course
func TestSectionReferenceCourse(t *testing.T) {
courseIDMap := make(map[primitive.ObjectID]string)
for _, course := range testCourses {
courseIDMap[course.Id] = course.Internal_course_number + course.Catalog_year
}
var logBuffer bytes.Buffer
log.SetOutput(&logBuffer)
defer func() {
logOutput := logBuffer.String()
if logOutput != "" {
t.Errorf("Expected nothing printed in log")
}
if r := recover(); r != nil {
t.Errorf("The function panic unexpectedly for section")
}
}()
for _, section := range testSections {
valSectionReferenceCourse(section, courseIDMap)
}
}
/* BELOW HERE ARE HELPER FUNCTION FOR TESTS ABOVE */
// Helper function
// Test if validate() throws erros when encountering duplicate
// Design for fail cases
func testDuplicateFail(objType string, index int, t *testing.T) {
// the buffer used to capture the log output
var logBuffer bytes.Buffer
log.SetOutput(&logBuffer)
// determine the expected msgs and panic msgs based on object type
var expectedMsgs []string
var panicMsg string
switch objType {
case "course":
failCourse := testCourses[index]
// list of msgs it must print
expectedMsgs = []string{
fmt.Sprintf("Duplicate course found for %s%s!", failCourse.Subject_prefix, failCourse.Course_number),
fmt.Sprintf("Course 1: %v\n\nCourse 2: %v", failCourse, failCourse),
}
panicMsg = "Courses failed to validate!"
case "section":
failSection := testSections[index]
expectedMsgs = []string{
"Duplicate section found!",
fmt.Sprintf("Section 1: %v\n\nSection 2: %v", failSection, failSection),
}
panicMsg = "Sections failed to validate!"
case "professor":
failProf := testProfessors[index]
expectedMsgs = []string{
"Duplicate professor found!",
fmt.Sprintf("Professor 1: %v\n\nProfessor 2: %v", failProf, failProf),
}
panicMsg = "Professors failed to validate!"
}
defer func() {
logOutput := logBuffer.String() // log output after running the function
// log output needs to contain lines in the list
for _, msg := range expectedMsgs {
if !strings.Contains(logOutput, msg) {
t.Errorf("Exptected the message for %v: %v", objType, msg)
}
}
// test whether func panics and sends the correct panic msg
if r := recover(); r == nil {
t.Errorf("The function didn't panic for %v", objType)
} else {
if r != panicMsg {
// The panic msg is incorrect
t.Errorf("The function outputted the wrong panic message for %v.", objType)
}
}
}()
// Run func
switch objType {
case "course":
valDuplicateCourses(testCourses[index], testCourses[index])
case "section":
valDuplicateSections(testSections[index], testSections[index])
case "professor":
valDuplicateProfs(testProfessors[index], testProfessors[index])
}
}
// Helper function
// Test if func doesn't log anything and doesn't panic.
// Design for pass cases
func testDuplicatePass(objType string, index1 int, index2 int, t *testing.T) {
// Buffer to capture the output
var logBuffer bytes.Buffer
log.SetOutput(&logBuffer)
defer func() {
logOutput := logBuffer.String()
if logOutput != "" {
t.Errorf("Expected nothing in log for " + objType)
}
if r := recover(); r != nil {
t.Errorf("The function panic unexpectedly for " + objType)
}
}()
// Run func according to the object type. Choose pair of objects which are not duplicate
switch objType {
case "course":
valDuplicateCourses(testCourses[index1], testCourses[index2])
case "section":
valDuplicateSections(testSections[index1], testSections[index2])
case "professor":
valDuplicateProfs(testProfessors[index1], testProfessors[index2])
}
}
// Helper function for the case of course reference that fails
// failType: 1 means it lacks one sections
// failType: 2 means one section's course reference has been modified
func testCourseReferenceFail(failType int, courseIndex int, sectionIndex int, t *testing.T) {
sectionMap := make(map[primitive.ObjectID]*schema.Section)
var sectionID, originalID primitive.ObjectID // used to store IDs of modified sections
// Build the failed section map based on fail type
if failType == 1 {
// misses a section
for i, section := range testSections {
if sectionIndex != i {
sectionMap[section.Id] = section
} else {
sectionID = section.Id // Nonexistent ID referenced by course
}
}
} else {
// one section doesn't reference to correct courses
for i, section := range testSections {
sectionMap[section.Id] = section
if sectionIndex == i {
// save the section ID and original course reference to be restored later on
sectionID = section.Id
originalID = section.Course_reference
// modify part
sectionMap[section.Id].Course_reference = primitive.NewObjectID()
}
}
}
// Expected msgs
var expectedMsgs []string
// The course that references nonexistent stuff
var failCourse *schema.Course
if failType == 1 {
failCourse = testCourses[courseIndex]
expectedMsgs = []string{
fmt.Sprintf("Nonexistent section reference found for %v%v!", failCourse.Subject_prefix, failCourse.Course_number),
fmt.Sprintf("Referenced section ID: %s\nCourse ID: %s", sectionID, failCourse.Id),
}
} else {
failCourse = testCourses[courseIndex]
failSection := testSections[sectionIndex]
expectedMsgs = []string{
fmt.Sprintf("Inconsistent section reference found for %v%v! The course references the section, but not vice-versa!",
failCourse.Subject_prefix, failCourse.Course_number),
fmt.Sprintf("Referenced section ID: %s\nCourse ID: %s\nSection course reference: %s",
failSection.Id, failCourse.Id, failSection.Course_reference),
}
}
// Buffer to capture the output
var logBuffer bytes.Buffer
log.SetOutput(&logBuffer)
defer func() {
logOutput := logBuffer.String()
for _, msg := range expectedMsgs {
if !strings.Contains(logOutput, msg) {
t.Errorf("The function didn't log correct message. Expected \"%v\"", msg)
}
}
// restore to original course reference of modified section (if needed)
if failType == 2 {
sectionMap[sectionID].Course_reference = originalID
}
if r := recover(); r == nil {
t.Errorf("The function didn't panic")
} else {
if r != "Courses failed to validate!" {
t.Errorf("The function panic the wrong message")
}
}
}()
// Run func
for _, course := range testCourses {
valCourseReference(course, sectionMap)
}
}