|
7 | 7 | let(:school) { create(:school) }
|
8 | 8 | let(:students) { create_list(:student, 3, school:) }
|
9 | 9 | let(:teacher) { create(:teacher, school:) }
|
| 10 | + let(:teachers) { [create(:teacher, school:), create(:teacher, school:)] } |
10 | 11 |
|
11 | 12 | let(:student_ids) { students.map(&:id) }
|
12 | 13 |
|
13 | 14 | it 'returns a successful operation response' do
|
14 |
| - response = described_class.call(school_class:, students:) |
| 15 | + response = described_class.call(school_class:, students:, teachers:) |
15 | 16 | expect(response.success?).to be(true)
|
16 | 17 | end
|
17 | 18 |
|
18 | 19 | it 'creates class students' do
|
19 |
| - expect { described_class.call(school_class:, students:) }.to change(ClassStudent, :count).by(3) |
| 20 | + expect { described_class.call(school_class:, students:, teachers:) }.to change(ClassStudent, :count).by(3) |
| 21 | + end |
| 22 | + |
| 23 | + it 'creates class teachers' do |
| 24 | + expect { described_class.call(school_class:, students:, teachers:) }.to change(ClassTeacher, :count).by(2) |
20 | 25 | end
|
21 | 26 |
|
22 | 27 | it 'returns a class members JSON array' do
|
23 |
| - response = described_class.call(school_class:, students:) |
24 |
| - expect(response[:class_members].size).to eq(3) |
| 28 | + response = described_class.call(school_class:, students:, teachers:) |
| 29 | + expect(response[:class_members].size).to eq(5) |
25 | 30 | end
|
26 | 31 |
|
27 | 32 | it 'returns class students in the operation response' do
|
28 |
| - response = described_class.call(school_class:, students:) |
29 |
| - expect(response[:class_members]).to all(be_a(ClassStudent)) |
| 33 | + response = described_class.call(school_class:, students:, teachers:) |
| 34 | + class_students_count = response[:class_members].count { |member| member.is_a?(ClassStudent) } |
| 35 | + expect(class_students_count).to eq(3) |
| 36 | + end |
| 37 | + |
| 38 | + it 'returns class teachers in the operation response' do |
| 39 | + response = described_class.call(school_class:, students:, teachers:) |
| 40 | + class_teachers_count = response[:class_members].count { |member| member.is_a?(ClassTeacher) } |
| 41 | + expect(class_teachers_count).to eq(2) |
30 | 42 | end
|
31 | 43 |
|
32 | 44 | it 'assigns the school_class' do
|
|
35 | 47 | end
|
36 | 48 |
|
37 | 49 | it 'assigns the student_id' do
|
38 |
| - response = described_class.call(school_class:, students:) |
39 |
| - expect(response[:class_members].map(&:student_id)).to match_array(student_ids) |
| 50 | + response = described_class.call(school_class:, students:, teachers:) |
| 51 | + response_students = response[:class_members].select { |member| member.is_a?(ClassStudent) } |
| 52 | + expect(response_students.map(&:student_id)).to match_array(student_ids) |
| 53 | + end |
| 54 | + |
| 55 | + it 'assigns the teacher_id' do |
| 56 | + teacher_ids = teachers.map(&:id) |
| 57 | + response = described_class.call(school_class:, students:, teachers:) |
| 58 | + response_teachers = response[:class_members].select { |member| member.is_a?(ClassTeacher) } |
| 59 | + expect(response_teachers.map(&:teacher_id)).to match_array(teacher_ids) |
40 | 60 | end
|
41 | 61 |
|
42 | 62 | context 'when creations fail' do
|
43 | 63 | before do
|
44 | 64 | allow(Sentry).to receive(:capture_exception)
|
45 | 65 | end
|
46 | 66 |
|
47 |
| - context 'with malformed students' do |
| 67 | + context 'with malformed members' do |
48 | 68 | let(:students) { nil }
|
| 69 | + let(:teachers) { nil } |
| 70 | + |
| 71 | + it 'does not create a class student' do |
| 72 | + expect { described_class.call(school_class:, students:, teachers:) }.not_to change(ClassStudent, :count) |
| 73 | + end |
49 | 74 |
|
50 |
| - it 'does not create a class member' do |
51 |
| - expect { described_class.call(school_class:, students:) }.not_to change(ClassStudent, :count) |
| 75 | + it 'does not create a class teacher' do |
| 76 | + expect { described_class.call(school_class:, students:, teachers:) }.not_to change(ClassTeacher, :count) |
52 | 77 | end
|
53 | 78 |
|
54 | 79 | it 'returns a failed operation response' do
|
55 |
| - response = described_class.call(school_class:, students:) |
| 80 | + response = described_class.call(school_class:, students:, teachers:) |
56 | 81 | expect(response.failure?).to be(true)
|
57 | 82 | end
|
58 | 83 |
|
59 | 84 | it 'returns the error message in the operation response' do
|
60 |
| - response = described_class.call(school_class:, students:) |
61 |
| - expect(response[:error]).to match(/No valid students provided/) |
| 85 | + response = described_class.call(school_class:, students:, teachers:) |
| 86 | + expect(response[:error]).to match(/No valid school members provided/) |
62 | 87 | end
|
63 | 88 |
|
64 | 89 | it 'sent the exception to Sentry' do
|
65 |
| - described_class.call(school_class:, students:) |
| 90 | + described_class.call(school_class:, students:, teachers:) |
66 | 91 | expect(Sentry).to have_received(:capture_exception).with(kind_of(StandardError))
|
67 | 92 | end
|
68 | 93 | end
|
69 | 94 |
|
70 | 95 | context 'with a student from a different school' do
|
71 |
| - let(:different_school) { create(:school) } |
72 |
| - let(:different_school_student) { create(:student, school: different_school) } |
| 96 | + let(:different_school_student) { create(:student, school: create(:school)) } |
73 | 97 |
|
74 | 98 | context 'with non existent students' do
|
75 | 99 | let(:students) { [different_school_student] }
|
|
103 | 127 | let(:new_students) { students + [different_school_student] }
|
104 | 128 |
|
105 | 129 | it 'returns a successful operation response' do
|
106 |
| - response = described_class.call(school_class:, students: new_students) |
| 130 | + response = described_class.call(school_class:, students: new_students, teachers:) |
107 | 131 | expect(response.success?).to be(true)
|
108 | 132 | end
|
109 | 133 |
|
110 | 134 | it 'returns a class members JSON array' do
|
111 |
| - response = described_class.call(school_class:, students: new_students) |
112 |
| - expect(response[:class_members].size).to eq(3) |
| 135 | + response = described_class.call(school_class:, students: new_students, teachers:) |
| 136 | + expect(response[:class_members].size).to eq(5) |
113 | 137 | end
|
114 | 138 |
|
115 |
| - it 'returns class members in the operation response' do |
116 |
| - response = described_class.call(school_class:, students: new_students) |
117 |
| - expect(response[:class_members]).to all(be_a(ClassStudent)) |
| 139 | + it 'returns class students in the operation response' do |
| 140 | + response = described_class.call(school_class:, students: new_students, teachers:) |
| 141 | + class_students_count = response[:class_members].count { |member| member.is_a?(ClassStudent) } |
| 142 | + expect(class_students_count).to eq(3) |
| 143 | + end |
| 144 | + |
| 145 | + it 'returns class teachers in the operation response' do |
| 146 | + response = described_class.call(school_class:, students: new_students, teachers:) |
| 147 | + class_teachers_count = response[:class_members].count { |member| member.is_a?(ClassTeacher) } |
| 148 | + expect(class_teachers_count).to eq(2) |
118 | 149 | end
|
119 | 150 |
|
120 | 151 | it 'assigns the school_class' do
|
121 |
| - response = described_class.call(school_class:, students: new_students) |
| 152 | + response = described_class.call(school_class:, students: new_students, teachers:) |
122 | 153 | expect(response[:class_members]).to all(have_attributes(school_class:))
|
123 | 154 | end
|
124 | 155 |
|
125 | 156 | it 'assigns the successful students' do
|
126 |
| - response = described_class.call(school_class:, students: new_students) |
127 |
| - expect(response[:class_members].map(&:student_id)).to match_array(student_ids) |
| 157 | + response = described_class.call(school_class:, students: new_students, teachers:) |
| 158 | + response_students = response[:class_members].select { |member| member.is_a?(ClassStudent) } |
| 159 | + expect(response_students.map(&:student_id)).to match_array(student_ids) |
| 160 | + end |
| 161 | + |
| 162 | + it 'assigns the successful teachers' do |
| 163 | + teacher_ids = teachers.map(&:id) |
| 164 | + response = described_class.call(school_class:, students: new_students, teachers:) |
| 165 | + response_teachers = response[:class_members].select { |member| member.is_a?(ClassTeacher) } |
| 166 | + expect(response_teachers.map(&:teacher_id)).to match_array(teacher_ids) |
128 | 167 | end
|
129 | 168 |
|
130 | 169 | it 'returns the error messages in the operation response' do
|
131 |
| - response = described_class.call(school_class:, students: new_students) |
| 170 | + response = described_class.call(school_class:, students: new_students, teachers:) |
132 | 171 | expect(response[:errors][different_school_student.id]).to eq("Error creating class member for student_id #{different_school_student.id}: Student '#{different_school_student.id}' does not have the 'school-student' role for organisation '#{school.id}'")
|
133 | 172 | end
|
134 | 173 |
|
135 | 174 | it 'sent the exception to Sentry' do
|
136 |
| - described_class.call(school_class:, students: new_students) |
| 175 | + described_class.call(school_class:, students: new_students, teachers:) |
137 | 176 | expect(Sentry).to have_received(:capture_exception).with(kind_of(StandardError))
|
138 | 177 | end
|
139 | 178 | end
|
|
0 commit comments