diff --git a/Changelog.md b/Changelog.md index 3d980d3176..b2b8c34d97 100644 --- a/Changelog.md +++ b/Changelog.md @@ -28,6 +28,7 @@ - Fix: include original total mark in JSON response for remark requests (#7945) ### 🔧 Internal changes +- Added tests for `MarksGradersController` to achieve full test coverage for `randomly_assign` (#7947) - Fixed flaky test `can bulk assign duplicated TAs to grade entry students` in `/spec/models/grade_entry_student_spec.rb` (#7958) - Added tests for `GroupsController` to fully cover `global_actions` (#7955) - Added tests for `graders_controller` to fully cover `grader_criteria_mapping` function (#7949) diff --git a/doc/markus-contributors.txt b/doc/markus-contributors.txt index 58c39f6d41..d76f2e415d 100644 --- a/doc/markus-contributors.txt +++ b/doc/markus-contributors.txt @@ -58,6 +58,7 @@ Clément Delafargue Clément Schiano Danesh Dadachanji Daniel Dervishi +Daniel Rafailov Daniel St. Jules Daniyal Liaqat Daryn Lam diff --git a/spec/controllers/marks_graders_controller_spec.rb b/spec/controllers/marks_graders_controller_spec.rb index 2a590ec2b5..6aa07884b3 100644 --- a/spec/controllers/marks_graders_controller_spec.rb +++ b/spec/controllers/marks_graders_controller_spec.rb @@ -376,4 +376,115 @@ end end end + + describe '#randomly_assign' do + context 'does not require initial setup' do + context 'when students and graders are selected' do + it 'calls GradeEntryStudent `randomly_assign_tas` and ensures that every student gets a TA assigned to them' do + tas = create_list(:ta, 3) + ta_ids = tas.map(&:id) + + students = create_list(:student, 10, course: course) + student_ids = students.map(&:id) + + post_as instructor, :randomly_assign, params: { + course_id: course.id, + grade_entry_form_id: grade_entry_form.id, + students: student_ids, + graders: ta_ids + } + + expect(response).to have_http_status(:ok) + + students.each do |student| + ges = grade_entry_form.grade_entry_students.find_by!(role: student.id) + expect(ges.tas.count).to eq(1) + end + end + end + end + + context 'requires initial setup' do + before do + allow(GradeEntryStudent).to receive(:randomly_assign_tas) + end + + context 'when students and graders are selected' do + it 'calls GradeEntryStudent `randomly_assign_tas` and returns a success response' do + student = create(:student) + grade_entry_student = grade_entry_form.grade_entry_students.find_by(role: student) + grade_entry_student_ta = create(:grade_entry_student_ta, grade_entry_student: grade_entry_student) + + expect(GradeEntryStudent).to receive(:randomly_assign_tas).with( + [grade_entry_student.id.to_s], + [grade_entry_student_ta.id.to_s], + grade_entry_form + ) + + post_as instructor, :randomly_assign, params: { + course_id: course.id, + grade_entry_form_id: grade_entry_form.id, + students: [grade_entry_student.id], + graders: [grade_entry_student_ta.id] + } + + expect(response).to have_http_status(:ok) + end + end + + context 'when students are not selected' do + it 'returns bad request and sets a flash error' do + post_as instructor, :randomly_assign, params: { + course_id: course.id, + grade_entry_form_id: grade_entry_form.id, + students: [], + graders: [1] + } + + expect(response).to have_http_status(:bad_request) + expect(flash[:error]).to have_message(I18n.t('groups.select_a_student')) + end + end + + context 'when graders are not selected' do + it 'returns bad request and sets flash error' do + post_as instructor, :randomly_assign, params: { + course_id: course.id, + grade_entry_form_id: grade_entry_form.id, + students: [1], + graders: [] + } + + expect(response).to have_http_status(:bad_request) + expect(flash[:error]).to have_message(I18n.t('graders.select_a_grader')) + end + end + + context 'when students parameter is missing' do + it 'returns bad request and sets flash error' do + post_as instructor, :randomly_assign, params: { + course_id: course.id, + grade_entry_form_id: grade_entry_form.id, + graders: [1] + } + + expect(response).to have_http_status(:bad_request) + expect(flash[:error]).to have_message(I18n.t('groups.select_a_student')) + end + end + + context 'when graders parameter is missing' do + it 'returns bad request and sets flash error' do + post_as instructor, :randomly_assign, params: { + course_id: course.id, + grade_entry_form_id: grade_entry_form.id, + students: [1] + } + + expect(response).to have_http_status(:bad_request) + expect(flash[:error]).to have_message(I18n.t('graders.select_a_grader')) + end + end + end + end end diff --git a/spec/models/grade_entry_student_spec.rb b/spec/models/grade_entry_student_spec.rb index b79704cfd8..b998f4d119 100644 --- a/spec/models/grade_entry_student_spec.rb +++ b/spec/models/grade_entry_student_spec.rb @@ -84,7 +84,7 @@ GradeEntryStudent.assign_all_tas(student_ids, ta_ids.first, form) # First grade entry student gets all the TAs. - grade_entry_student = form.grade_entry_students.find_by!(role_id: student_ids.first) + grade_entry_student = form.grade_entry_students.first grade_entry_student.reload form.grade_entry_students.delete(grade_entry_student) expect(grade_entry_student.tas).to match_array(tas)