feat: persist timetable and validate confirmed imports
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hepan_everyday/data/json_schedule_import.dart';
|
||||
import 'package:hepan_everyday/domain/course.dart';
|
||||
|
||||
Map<String, dynamic> row({String id = 'a', int day = 1}) => {
|
||||
'id': id,
|
||||
'name': '矩阵论',
|
||||
'teacher': '老师',
|
||||
'room': '教室',
|
||||
'weekday': day,
|
||||
'startPeriod': 1,
|
||||
'endPeriod': 2,
|
||||
'weeks': [1, 3, 5],
|
||||
};
|
||||
String input(List<dynamic> courses, {int version = 1, bool demo = false}) =>
|
||||
jsonEncode({'schemaVersion': version, 'isDemo': demo, 'courses': courses});
|
||||
void main() {
|
||||
test('file import normalizes weeks and cannot claim school provenance', () {
|
||||
final parsed = JsonScheduleImport.parse(input([row()]));
|
||||
expect(parsed.courses, hasLength(1));
|
||||
expect(parsed.courses.single.weeks, {1, 3, 5});
|
||||
expect(parsed.source, CourseSource.file);
|
||||
});
|
||||
test('one invalid course rejects the whole draft', () {
|
||||
expect(
|
||||
() => JsonScheduleImport.parse(input([row(), row(id: 'b', day: 8)])),
|
||||
throwsFormatException,
|
||||
);
|
||||
});
|
||||
test('duplicate identifiers and unsupported schema are rejected', () {
|
||||
expect(
|
||||
() => JsonScheduleImport.parse(input([row(), row()])),
|
||||
throwsFormatException,
|
||||
);
|
||||
expect(
|
||||
() => JsonScheduleImport.parse(input([], version: 2)),
|
||||
throwsFormatException,
|
||||
);
|
||||
});
|
||||
test('demo import is explicitly separated from real records', () {
|
||||
expect(
|
||||
JsonScheduleImport.parse(input([row()], demo: true)).source,
|
||||
CourseSource.demo,
|
||||
);
|
||||
});
|
||||
test('valid but oversized JSON is rejected before accepting a course', () {
|
||||
final large = row()..['name'] = List.filled(2100000, 'x').join();
|
||||
expect(
|
||||
() => JsonScheduleImport.parse(input([large])),
|
||||
throwsFormatException,
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user