-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathschool_class_spec.rb
More file actions
288 lines (230 loc) · 10.6 KB
/
Copy pathschool_class_spec.rb
File metadata and controls
288 lines (230 loc) · 10.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
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe SchoolClass, :versioning do
before do
stub_user_info_api_for_users([teacher.id, second_teacher.id], users: [teacher, second_teacher])
end
let(:student) { create(:student, school:) }
let(:teacher) { create(:teacher, school:, name: 'School Teacher') }
let(:second_teacher) { create(:teacher, school:, name: 'Second Teacher') }
let(:school) { create(:school) }
describe 'associations' do
it { is_expected.to belong_to(:school) }
it { is_expected.to have_many(:students).dependent(:destroy) }
it { is_expected.to have_many(:teachers).dependent(:destroy) }
it { is_expected.to have_many(:lessons).dependent(:nullify) }
end
describe 'nested attributes' do
it 'accepts nested attributes for teachers' do
school_class_attributes = attributes_for(:school_class).merge(
teachers_attributes: [
{ teacher_id: teacher.id },
{ teacher_id: second_teacher.id }
]
)
school_class = described_class.new(school_class_attributes)
expect(school_class.teachers.map(&:teacher_id)).to eq([teacher.id, second_teacher.id])
end
end
describe 'validations' do
subject(:school_class) { build(:school_class, teacher_ids: [teacher.id, second_teacher.id], school:) }
it 'has a valid default factory' do
expect(school_class).to be_valid
end
it 'can save the default factory' do
expect { school_class.save! }.not_to raise_error
end
it 'requires a school' do
school_class.school = nil
expect(school_class).not_to be_valid
end
it 'requires teacher_ids' do
school_class_without_teacher = build(:school_class, teacher_ids: [], school:)
expect(school_class_without_teacher).not_to be_valid
end
it 'requires UUID teacher_ids' do
school_class_with_invalid_teacher = build(:school_class, teacher_ids: ['invalid'], school:)
expect(school_class_with_invalid_teacher).not_to be_valid
end
it 'requires a name' do
school_class.name = ' '
expect(school_class).not_to be_valid
end
it 'assigns class code before validating' do
school_class.code = nil
school_class.valid?
expect(school_class.code).to match(/\d\d-\d\d-\d\d/)
end
it 'requires a unique class code within the same school' do
school_class.save!
school_class_with_duplicate_code = build(:school_class, school: school_class.school, code: school_class.code)
school_class_with_duplicate_code.valid?
expect(school_class_with_duplicate_code.errors[:code]).to include('has already been taken')
end
it 'permits a duplicate class code in a different school' do
school_class.save!
school_class_with_duplicate_code = build(:school_class, school: build(:school), code: school_class.code)
expect(school_class_with_duplicate_code).to be_valid
end
it 'requires a valid class code format' do
school_class.code = 'invalid'
expect(school_class).not_to be_valid
end
it 'accepts a valid class code format' do
school_class.code = '12-34-56'
expect(school_class).to be_valid
end
it 'does not allow the class code to be changed' do
school_class.code = '12-34-56'
school_class.save!
school_class.code = '65-43-21'
expect(school_class).not_to be_valid
end
it 'allows import_origin to be google_classroom' do
school_class.import_origin = :google_classroom
school_class.import_id = 'classroom_123'
expect(school_class).to be_valid
end
it 'does not allow invalid import_origin values' do
school_class.import_origin = 'bad_origin'
expect(school_class).not_to be_valid
expect(school_class.errors[:import_origin]).to include('is not included in the list')
end
it 'allows import_id to be nil when import_origin is nil' do
school_class.import_origin = nil
school_class.import_id = nil
expect(school_class).to be_valid
end
it 'requires unique import_id within the same school and import_origin' do
school_class.import_origin = :google_classroom
school_class.import_id = 'classroom_123'
school_class.save!
duplicate_school_class = build(:school_class,
teacher_ids: [teacher.id],
school: school_class.school,
import_origin: :google_classroom,
import_id: 'classroom_123')
expect(duplicate_school_class).not_to be_valid
expect(duplicate_school_class.errors[:import_id]).to include('has already been taken')
end
it 'permits duplicate import_id in different schools' do
school_class.import_origin = :google_classroom
school_class.import_id = 'classroom_123'
school_class.save!
different_school_class = build(:school_class,
teacher_ids: [teacher.id],
school: create(:school),
import_origin: :google_classroom,
import_id: 'classroom_123')
expect(different_school_class).to be_valid
end
end
describe '.teachers' do
it 'returns User instances for the current scope' do
create(:school_class, teacher_ids: [teacher.id, second_teacher.id], school:)
teacher = described_class.all.teachers.first
expect(teacher.name).to eq('School Teacher')
end
it 'ignores teachers where no profile account exists' do
stub_user_info_api_for_unknown_users(user_id: teacher.id)
create(:school_class, school:, teacher_ids: [teacher.id])
teacher = described_class.all.teachers.first
expect(teacher).to be_nil
end
it 'ignores teachers not included in the current scope' do
create(:school_class, teacher_ids: [teacher.id], school:)
teacher = described_class.none.teachers.first
expect(teacher).to be_nil
end
end
describe '.with_teachers' do
it 'returns an array of class teachers paired with their User instance' do
school_class = create(:school_class, teacher_ids: [teacher.id, second_teacher.id], school:)
pair = described_class.all.with_teachers.first
teacher = described_class.all.teachers.first
expect(pair).to eq([school_class, [teacher, second_teacher]])
end
it 'returns nil values for teachers where no profile account exists' do
stub_user_info_api_for_unknown_users(user_id: teacher.id)
school_class = create(:school_class, school:, teacher_ids: [teacher.id])
pair = described_class.all.with_teachers.first
expect(pair).to eq([school_class, [nil]])
end
it 'ignores teachers not included in the current scope' do
create(:school_class, teacher_ids: [teacher.id, second_teacher.id], school:)
pair = described_class.none.with_teachers.first
expect(pair).to be_nil
end
end
describe '#with_teachers' do
it 'returns the class teachers paired with their User instances' do
school_class = create(:school_class, teacher_ids: [teacher.id, second_teacher.id], school:)
school_class_with_teachers = school_class.with_teachers
expect(school_class_with_teachers).to eq([school_class, [teacher, second_teacher]])
end
it 'skips user if the teacher has no profile account' do
stub_user_info_api_for_unknown_users(user_id: teacher.id)
stub_user_info_api_for(second_teacher)
school_class = create(:school_class, school:, teacher_ids: [teacher.id, second_teacher.id])
school_class_with_teachers = school_class.with_teachers
expect(school_class_with_teachers).to eq([school_class, [second_teacher]])
end
end
describe '#teacher_ids' do
it 'returns an array of teacher ids' do
school_class = create(:school_class, teacher_ids: [teacher.id, second_teacher.id], school:)
expect(school_class.teacher_ids).to eq([teacher.id, second_teacher.id])
end
end
describe '#assign_class_code' do
it 'assigns a class code if not already present' do
school_class = build(:school_class, code: nil, school:)
school_class.assign_class_code
expect(school_class.code).to match(/\d\d-\d\d-\d\d/)
end
it 'does not assign a class code if already present' do
school_class = build(:school_class, code: '12-34-56', school:)
school_class.assign_class_code
expect(school_class.code).to eq('12-34-56')
end
it 'retries 5 times if the school code is not unique within the school' do
school_class = create(:school_class, code: '12-34-56', school:)
allow(ForEducationCodeGenerator).to receive(:generate).and_return(*([school_class.code] * 4), '00-00-00')
another_school_class = create(:school_class, school: school_class.school, code: nil)
another_school_class.assign_class_code
expect(another_school_class.code).to eq('00-00-00')
end
it 'raises adds error if unique code cannot be generated in 5 retries' do
school_class = create(:school_class, code: '12-34-56', school:)
allow(ForEducationCodeGenerator).to receive(:generate).and_return(*([school_class.code] * 5))
another_school_class = build(:school_class, school: school_class.school, code: nil)
another_school_class.assign_class_code
expect(another_school_class.errors[:code]).to include('could not be generated')
end
it 'does not add error if class code shared by class from another school' do
school_class = create(:school_class, code: '12-34-56', school:)
allow(ForEducationCodeGenerator).to receive(:generate).and_return(school_class.code)
another_school_class = build(:school_class, school: build(:school), code: nil)
another_school_class.assign_class_code
expect(another_school_class.errors[:code]).to be_empty
end
end
describe '#submitted_projects_count' do
it 'returns 0 if there are no lessons' do
school_class = create(:school_class, teacher_ids: [teacher.id], school:)
expect(school_class.submitted_projects_count).to eq(0)
end
it 'returns the sum of submitted counts from all lessons' do
school_class = create(:school_class, teacher_ids: [teacher.id], school:)
create(:lesson, school_class:, user_id: teacher.id, submitted_projects_count: 5)
create(:lesson, school_class:, user_id: teacher.id, submitted_projects_count: 3)
expect(school_class.submitted_projects_count).to eq(8)
end
end
describe 'auditing' do
subject(:school_class) { create(:school_class, teacher_ids: [teacher.id], school:) }
it 'enables auditing' do
expect(school_class.versions.length).to(eq(1))
end
end
end