-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcourse_service_test.dart
More file actions
588 lines (515 loc) · 17.6 KB
/
course_service_test.dart
File metadata and controls
588 lines (515 loc) · 17.6 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import 'package:flutter_test/flutter_test.dart';
import 'package:tattoo/models/course.dart';
import 'package:tattoo/services/course_service.dart';
import 'package:tattoo/services/portal_service.dart';
import '../test_helpers.dart';
void main() {
group('CourseService Integration Tests', () {
late PortalService portalService;
late CourseService courseService;
setUpAll(() {
TestCredentials.validate();
});
setUp(() async {
portalService = PortalService();
courseService = CourseService();
await portalService.login(
TestCredentials.username,
TestCredentials.password,
);
await portalService.sso(PortalServiceCode.courseService);
await respectfulDelay();
});
group('getCourseSemesterList', () {
test('should return and parse semester list correctly', () async {
final semesters = await courseService.getCourseSemesterList(
TestCredentials.username,
);
expect(
semesters,
isNotEmpty,
reason: 'Should have at least one semester',
);
for (final semester in semesters) {
expect(semester.year, isNotNull);
expect(semester.term, isNotNull);
expect(
semester.year,
greaterThan(80),
reason: 'ROC year should be from modern era (>80 means after 1991)',
);
expect(
semester.term,
isIn([0, 1, 2, 3]),
reason:
'Term should be 0 (pre-study), 1 (fall), 2 (spring), or 3 (summer)',
);
}
});
});
group('getCourseTable', () {
test('should return course schedule for valid semester', () async {
final semesters = await courseService.getCourseSemesterList(
TestCredentials.username,
);
if (semesters.isEmpty) {
fail('No semesters available for testing');
}
final courseTable = await courseService.getCourseTable(
username: TestCredentials.username,
semester: semesters.pickRandom(),
);
expect(
courseTable,
isNotEmpty,
reason: 'Should have at least one course',
);
});
test('should parse all expected fields from course schedule', () async {
final semesters = await courseService.getCourseSemesterList(
TestCredentials.username,
);
final courseTable = await courseService.getCourseTable(
username: TestCredentials.username,
semester: semesters.pickRandom(),
);
// Find a real course (not special rows like 班週會及導師時間)
// Real courses have a course number
final regularCourses = courseTable
.where(
(schedule) =>
schedule.number != null && schedule.number!.isNotEmpty,
)
.toList();
expect(
regularCourses,
isNotEmpty,
reason: 'Should have at least one regular course with a number',
);
final firstRegularCourse = regularCourses.pickRandom();
// Verify required fields are present for regular courses
expect(firstRegularCourse.number, isNotEmpty);
expect(firstRegularCourse.course?.name, isNotNull);
expect(firstRegularCourse.course?.name, isNotEmpty);
// Verify numeric fields have reasonable values
if (firstRegularCourse.credits != null) {
expect(firstRegularCourse.credits, greaterThanOrEqualTo(0));
expect(
firstRegularCourse.credits,
lessThanOrEqualTo(10),
reason: 'Credits should be reasonable (≤10)',
);
}
if (firstRegularCourse.hours != null) {
expect(firstRegularCourse.hours, greaterThan(0));
}
// Verify phase is valid
if (firstRegularCourse.phase != null) {
expect(firstRegularCourse.phase, greaterThan(0));
}
});
test('should parse course names correctly', () async {
final semesters = await courseService.getCourseSemesterList(
TestCredentials.username,
);
final courseTable = await courseService.getCourseTable(
username: TestCredentials.username,
semester: semesters.pickRandom(),
);
final coursesWithNames = courseTable
.where(
(schedule) => schedule.course?.name != null,
)
.toList();
expect(
coursesWithNames,
isNotEmpty,
reason: 'At least one course should have a name',
);
for (final course in coursesWithNames) {
expect(
course.course!.name,
isNotEmpty,
reason: 'Course name should not be empty',
);
}
});
test('should parse schedule times correctly', () async {
final semesters = await courseService.getCourseSemesterList(
TestCredentials.username,
);
final courseTable = await courseService.getCourseTable(
username: TestCredentials.username,
semester: semesters.pickRandom(),
);
final coursesWithSchedule = courseTable
.where(
(schedule) =>
schedule.schedule != null && schedule.schedule!.isNotEmpty,
)
.toList();
expect(
coursesWithSchedule,
isNotEmpty,
reason: 'At least one course should have a schedule',
);
// Verify classroom names are non-empty when present
final coursesWithClassrooms = coursesWithSchedule.where(
(s) => s.schedule!.any((e) => e.$3 != null),
);
expect(
coursesWithClassrooms,
isNotEmpty,
reason: 'At least one course should have a classroom',
);
});
test('should parse course types correctly', () async {
final semesters = await courseService.getCourseSemesterList(
TestCredentials.username,
);
final courseTable = await courseService.getCourseTable(
username: TestCredentials.username,
semester: semesters.pickRandom(),
);
// Find regular courses (not special rows like 班週會及導師時間)
final regularCourses = courseTable
.where(
(schedule) =>
schedule.number != null && schedule.number!.isNotEmpty,
)
.toList();
expect(
regularCourses,
isNotEmpty,
reason: 'Should have at least one regular course',
);
for (final course in regularCourses) {
expect(
course.type,
isNotNull,
reason: 'Regular courses should have a type',
);
expect(
course.type,
isNotEmpty,
reason: 'Course type should not be empty',
);
}
});
});
group('getCourse', () {
test('should parse all course detail fields correctly', () async {
final semesters = await courseService.getCourseSemesterList(
TestCredentials.username,
);
final courseTable = await courseService.getCourseTable(
username: TestCredentials.username,
semester: semesters.pickRandom(),
);
final coursesWithId = courseTable
.where(
(schedule) => schedule.course?.id != null,
)
.toList();
final courseWithId = coursesWithId.pickRandom();
final courseDetails = await courseService.getCourse(
courseWithId.course!.id!,
);
// Verify required fields
expect(courseDetails.id, isNotNull);
expect(courseDetails.id, isNotEmpty);
// Verify both names exist
expect(
courseDetails.nameZh,
isNotNull,
reason: 'Course should have a Chinese name',
);
expect(
courseDetails.nameEn,
isNotNull,
reason: 'Course should have an English name',
);
// Verify numeric fields are reasonable (can be 0 for special courses)
if (courseDetails.credits != null) {
expect(courseDetails.credits, greaterThanOrEqualTo(0));
expect(courseDetails.credits, lessThanOrEqualTo(10));
}
if (courseDetails.hours != null) {
expect(courseDetails.hours, greaterThanOrEqualTo(0));
}
// Verify non-empty strings
if (courseDetails.nameZh != null) {
expect(courseDetails.nameZh, isNotEmpty);
}
if (courseDetails.nameEn != null) {
expect(courseDetails.nameEn, isNotEmpty);
}
});
});
group('getSyllabus', () {
test('should parse syllabus fields correctly', () async {
final semesters = await courseService.getCourseSemesterList(
TestCredentials.username,
);
final courseTable = await courseService.getCourseTable(
username: TestCredentials.username,
semester: semesters.pickRandom(),
);
// Find a course with a syllabus ID
final coursesWithSyllabus = courseTable
.where(
(schedule) =>
schedule.number != null &&
schedule.number!.isNotEmpty &&
schedule.syllabusId != null,
)
.toList();
expect(
coursesWithSyllabus,
isNotEmpty,
reason: 'Should have at least one course with a syllabus',
);
final course = coursesWithSyllabus.pickRandom();
final syllabus = await courseService.getSyllabus(
courseNumber: course.number!,
syllabusId: course.syllabusId!,
);
// Verify course type is a valid enum value
expect(
syllabus.type,
isNotNull,
reason: 'Syllabus should have a course type',
);
expect(
syllabus.type,
isIn(CourseType.values),
reason: 'Course type should be a valid CourseType enum',
);
// Verify enrollment numbers are reasonable
if (syllabus.enrolled != null) {
expect(
syllabus.enrolled,
greaterThanOrEqualTo(0),
reason: 'Enrolled count should be non-negative',
);
}
if (syllabus.withdrawn != null) {
expect(
syllabus.withdrawn,
greaterThanOrEqualTo(0),
reason: 'Withdrawn count should be non-negative',
);
}
// Verify lastUpdated is a valid date
expect(
syllabus.lastUpdated,
isNotNull,
reason: 'Syllabus should have a last updated date',
);
});
test('should parse syllabus content fields', () async {
final semesters = await courseService.getCourseSemesterList(
TestCredentials.username,
);
final courseTable = await courseService.getCourseTable(
username: TestCredentials.username,
semester: semesters.pickRandom(),
);
final coursesWithSyllabus = courseTable
.where(
(schedule) =>
schedule.number != null &&
schedule.number!.isNotEmpty &&
schedule.syllabusId != null,
)
.toList();
final course = coursesWithSyllabus.pickRandom();
final syllabus = await courseService.getSyllabus(
courseNumber: course.number!,
syllabusId: course.syllabusId!,
);
// At least some content fields should be populated
final hasContent =
syllabus.objective != null ||
syllabus.weeklyPlan != null ||
syllabus.evaluation != null ||
syllabus.materials != null;
expect(
hasContent,
isTrue,
reason: 'Syllabus should have at least one content field populated',
);
// Verify non-empty strings when present
if (syllabus.objective != null) {
expect(syllabus.objective, isNotEmpty);
}
if (syllabus.weeklyPlan != null) {
expect(syllabus.weeklyPlan, isNotEmpty);
}
if (syllabus.evaluation != null) {
expect(syllabus.evaluation, isNotEmpty);
}
if (syllabus.materials != null) {
expect(syllabus.materials, isNotEmpty);
}
});
test('should parse email when available', () async {
final semesters = await courseService.getCourseSemesterList(
TestCredentials.username,
);
final courseTable = await courseService.getCourseTable(
username: TestCredentials.username,
semester: semesters.pickRandom(),
);
final coursesWithSyllabus = courseTable
.where(
(schedule) =>
schedule.number != null &&
schedule.number!.isNotEmpty &&
schedule.syllabusId != null,
)
.toList();
final course = coursesWithSyllabus.pickRandom();
final syllabus = await courseService.getSyllabus(
courseNumber: course.number!,
syllabusId: course.syllabusId!,
);
// Email should contain @ if present
if (syllabus.email != null) {
expect(
syllabus.email,
contains('@'),
reason: 'Email should be a valid email format',
);
}
});
});
group('getTeacher', () {
test('should parse teacher profile fields correctly', () async {
final semesters = await courseService.getCourseSemesterList(
TestCredentials.username,
);
final semester = semesters.pickRandom();
final courseTable = await courseService.getCourseTable(
username: TestCredentials.username,
semester: semester,
);
// Find a course with a teacher ID
final coursesWithTeacher = courseTable
.where((schedule) => schedule.teacher?.id != null)
.toList();
expect(
coursesWithTeacher,
isNotEmpty,
reason: 'Should have at least one course with a teacher',
);
final course = coursesWithTeacher.pickRandom();
final teacher = await courseService.getTeacher(
teacherId: course.teacher!.id!,
semester: semester,
);
// Verify profile fields from format=-3
expect(
teacher.department,
isNotNull,
reason: 'Teacher should have a department',
);
expect(
teacher.department?.name,
isNotEmpty,
reason: 'Department name should not be empty',
);
expect(
teacher.title,
isNotNull,
reason: 'Teacher should have a title',
);
expect(
teacher.title,
isNotEmpty,
reason: 'Title should not be empty',
);
expect(
teacher.nameZh,
isNotNull,
reason: 'Teacher should have a Chinese name',
);
expect(
teacher.nameZh,
isNotEmpty,
reason: 'Chinese name should not be empty',
);
// Teaching hours should be reasonable (can be 0 for some faculty)
if (teacher.teachingHours != null) {
expect(
teacher.teachingHours,
greaterThanOrEqualTo(0),
reason: 'Teaching hours should be non-negative',
);
expect(
teacher.teachingHours,
lessThanOrEqualTo(40),
reason: 'Teaching hours should be reasonable (≤40)',
);
}
});
test('should parse office hours correctly', () async {
final semesters = await courseService.getCourseSemesterList(
TestCredentials.username,
);
final semester = semesters.pickRandom();
final courseTable = await courseService.getCourseTable(
username: TestCredentials.username,
semester: semester,
);
final coursesWithTeacher = courseTable
.where((schedule) => schedule.teacher?.id != null)
.toList();
expect(
coursesWithTeacher,
isNotEmpty,
reason: 'Should have at least one course with a teacher',
);
final course = coursesWithTeacher.pickRandom();
final teacher = await courseService.getTeacher(
teacherId: course.teacher!.id!,
semester: semester,
);
// Office hours may or may not be set by teacher
if (teacher.officeHours != null && teacher.officeHours!.isNotEmpty) {
for (final officeHour in teacher.officeHours!) {
// Verify time ranges are valid
expect(
officeHour.startTime.hour,
inInclusiveRange(0, 23),
reason: 'Start hour should be 0-23',
);
expect(
officeHour.startTime.minute,
inInclusiveRange(0, 59),
reason: 'Start minute should be 0-59',
);
expect(
officeHour.endTime.hour,
inInclusiveRange(0, 23),
reason: 'End hour should be 0-23',
);
expect(
officeHour.endTime.minute,
inInclusiveRange(0, 59),
reason: 'End minute should be 0-59',
);
}
}
// English name from format=-6
if (teacher.nameEn != null) {
expect(
teacher.nameEn,
isNotEmpty,
reason: 'English name should not be empty if present',
);
}
});
});
});
}