forked from UTDNebula/api-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourseParser_test.go
160 lines (142 loc) · 3.5 KB
/
courseParser_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
package parser
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/UTDNebula/nebula-api/api/schema"
)
func TestGetCourse(t *testing.T) {
t.Parallel()
for name, testCase := range testData {
t.Run(name, func(t *testing.T) {
_, courseNum := getInternalClassAndCourseNum(testCase.ClassInfo)
output := *getCourse(courseNum, testCase.Section.Academic_session, testCase.RowInfo, testCase.ClassInfo)
expected := testCase.Course
diff := cmp.Diff(expected, output, cmpopts.IgnoreFields(schema.Course{}, "Id", "Sections", "Enrollment_reqs", "Prerequisites"))
if diff != "" {
t.Errorf("Failed (-expected +got)\n %s", diff)
}
})
}
}
func TestGetCatalogYear(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
Session schema.AcademicSession
Expected string
Panic bool
}{
"Case_001": {
Session: schema.AcademicSession{Name: "25S"},
Expected: "24",
},
"Case_002": {
Session: schema.AcademicSession{Name: "25F"},
Expected: "25",
},
"Case_003": {
Session: schema.AcademicSession{Name: "22U"},
Expected: "21",
},
"Case_004": {
Session: schema.AcademicSession{Name: "20S"},
Expected: "19",
},
"Case_005": {
Session: schema.AcademicSession{Name: "Garbage"},
Panic: true,
},
"Case_006": {
Session: schema.AcademicSession{Name: "20P"},
Panic: true,
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
defer func() {
// Test fails if we panic when we didn't want to or didn't when we did
if rec := recover(); rec != nil {
if !testCase.Panic {
t.Errorf("unexpected panic for session %q: %v", testCase.Session.Name, rec)
}
} else {
if testCase.Panic {
t.Errorf("expected panic for session %q but got none", testCase.Session.Name)
}
}
}()
// only call if we *expect* it to succeed
output := getCatalogYear(testCase.Session)
if !testCase.Panic && output != testCase.Expected {
t.Errorf("expected %q, got %q", testCase.Expected, output)
}
})
}
}
func TestGetPrefixAndCourseNum(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
ClassInfo map[string]string
Prefix string
Number string
Panic bool
}{
"Case_001": {
ClassInfo: map[string]string{
"Class Section:": "ACCT2301.001.25S",
},
Prefix: "ACCT",
Number: "2301",
},
"Case_002": {
ClassInfo: map[string]string{
"Class Section:": "ENTP3301.002.24S",
},
Prefix: "ENTP",
Number: "3301",
},
"Case_003": {
ClassInfo: map[string]string{
"Class Section:": "Garbage In, Garbage out",
},
Panic: true,
},
"Case_004": {
ClassInfo: map[string]string{
"Class Section:": "ENTP33S",
},
Panic: true,
},
"Case_005": {
ClassInfo: map[string]string{
"Class Section:": "",
},
Panic: true,
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if !testCase.Panic {
t.Errorf("unexpected panic for input %q: %v", name, r)
}
} else {
if testCase.Panic {
t.Errorf("expected panic for input %q but none occurred", name)
}
}
}()
prefix, number := getPrefixAndNumber(testCase.ClassInfo)
if !testCase.Panic {
if prefix != testCase.Prefix {
t.Errorf("expected %q got %q", testCase.Prefix, prefix)
}
if number != testCase.Number {
t.Errorf("expected %q got %q", testCase.Number, number)
}
}
})
}
}