Skip to content

Commit

Permalink
Fix deletion of subjects if final type of term was changed. (#1595)
Browse files Browse the repository at this point in the history
Fix the bug that would cause subjects that have no overridden final
grade type to be deleted when changing the final grade type of a term.
  • Loading branch information
Jonas-Sander authored May 3, 2024
1 parent bbc04cc commit d17acbe
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/lib/grades/grades_service/src/term.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ class TermModel extends Equatable {
}

TermModel setFinalGradeType(GradeTypeId gradeType) {
final newSubjects =
subjects.where((s) => s.isFinalGradeTypeOverridden == false).map((s) {
final newSubjects = subjects.map((s) {
if (s.isFinalGradeTypeOverridden) return s;

final newSubject = s.copyWith(finalGradeType: gradeType);
return newSubject;
}).toIList();
Expand Down
41 changes: 41 additions & 0 deletions app/test/grades/grades_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1388,4 +1388,45 @@ void main() {
throwsA(const SubjectNotFoundException(SubjectId('Unknown'))));
});
});
test(
'Regression test: Changing the final grade type of a term shouldnt delete any subjects',
() {
final controller = GradesTestController();

controller.createTerm(
termWith(
id: const TermId('term1'),
finalGradeType: GradeType.other.id,
subjects: [
subjectWith(
id: const SubjectId('Deutsch'),
finalGradeType: GradeType.vocabularyTest.id,
grades: [gradeWith(value: 2)],
),
subjectWith(
id: const SubjectId('Sport'),
grades: [gradeWith(value: 2)],
),
],
),
);

// This would delete any subject with no overridden final grade type before
// fixing the bug.
controller.editTerm(
const TermId('term1'),
finalGradeType: GradeType.vocabularyTest.id,
);

expect(
controller
.term(const TermId('term1'))
.subjects
.map((sub) => sub.id)
.toList(),
[
const SubjectId('Deutsch'),
const SubjectId('Sport'),
]);
});
}

0 comments on commit d17acbe

Please sign in to comment.