-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_seeds_spec.rb
More file actions
125 lines (103 loc) · 5.03 KB
/
test_seeds_spec.rb
File metadata and controls
125 lines (103 loc) · 5.03 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
# frozen_string_literal: true
require 'rails_helper'
require 'rake'
RSpec.describe 'test_seeds', type: :task do
let(:creator_id) { '583ba872-b16e-46e1-9f7d-df89d267550d' } # jane.doe@example.com
let(:teacher_id) { 'bbb9b8fd-f357-4238-983d-6f87b99bdbb2' } # john.doe@example.com
let(:student_1) { 'e52de409-9210-4e94-b08c-dd11439e07d9' } # student
let(:student_2) { '0d488bec-b10d-46d3-b6f3-4cddf5d90c71' } # student
let(:school_id) { 'e52de409-9210-4e94-b08c-dd11439e07d9' }
describe ':destroy' do
let(:task) { Rake::Task['test_seeds:destroy'] }
let(:school) { create(:school, creator_id:, id: school_id) }
before do
create(:role, user_id: creator_id, school:)
create(:student_role, user_id: student_1, school:)
create(:teacher_role, user_id: creator_id, school:)
school_class = create(:school_class, school_id: school.id, teacher_ids: [creator_id])
create(:class_student, student_id: student_1, school_class_id: school_class.id)
create(:lesson, school_id: school.id, user_id: creator_id)
end
it 'destroys all seed data' do
task.invoke
expect(Role.where(user_id: [creator_id, teacher_id, student_1, student_2])).not_to exist
expect(School.where(creator_id:)).not_to exist
expect(ClassStudent.where(student_id: student_1)).not_to exist
expect(SchoolClass.where(school_id: school.id)).not_to exist
expect(Lesson.where(school_id: school.id)).not_to exist
expect(Project.where(school_id: school.id)).not_to exist
end
end
describe ':seed_a_school_with_lessons_and_students' do
let(:task) { Rake::Task['test_seeds:create'] }
let(:seed_country_code) { nil }
before do
allow(Faker::Address).to receive(:country_code).and_return(seed_country_code) if seed_country_code
task.reenable
task.invoke
end
it 'creates a verified school' do
expect(School.find_by(creator_id:).verified_at).to be_truthy
end
it 'creates lessons with projects' do
school = School.find_by(creator_id:)
expect(SchoolClass.where(school_id: school.id)).to exist
lesson = Lesson.where(school_id: school.id)
expect(lesson.length).to eq(4)
expect(Project.where(lesson_id: lesson.pluck(:id)).length).to eq(4)
end
it 'assigns a teacher' do
school = School.find_by(creator_id:)
expect(Role.teacher.where(user_id: teacher_id, school_id: school.id)).to exist
end
it 'creates class with lessons for the owner' do
school_id = School.find_by(creator_id:).id
school_class = SchoolClass.joins(:teachers).find_by(school_id:, teachers: { teacher_id: creator_id })
expect(school_class).not_to be_nil
expect(Lesson.where(school_id:, school_class_id: school_class.id).length).to eq(2)
end
it 'creates a class teacher association for the owner' do
expect(ClassTeacher.where(teacher_id: creator_id).length).to eq(1)
end
it 'creates class with lessons for the teacher' do
school_id = School.find_by(creator_id:).id
school_class = SchoolClass.joins(:teachers).find_by(school_id:, teachers: { teacher_id: })
expect(school_class).not_to be_nil
expect(Lesson.where(school_id:, school_class_id: school_class.id).length).to eq(2)
end
it 'creates a class teacher association for the teacher' do
expect(ClassTeacher.where(teacher_id:).length).to eq(1)
end
it 'is idempotent' do
school = School.find_by!(creator_id:)
owner_class = SchoolClass.joins(:teachers).find_by!(school_id: school.id, teachers: { teacher_id: creator_id })
teacher_class = SchoolClass.joins(:teachers).find_by!(school_id: school.id, teachers: { teacher_id: })
expect do
task.reenable
task.invoke
end.not_to change { [SchoolClass.where(school_id: school.id).count, Lesson.where(school_id: school.id).count, Project.where(school_id: school.id).count] }
expect(owner_class.reload.lessons.count).to eq(2)
expect(teacher_class.reload.lessons.count).to eq(2)
end
it 'assigns students' do
school_id = School.find_by(creator_id:).id
school_class_id = SchoolClass.find_by(school_id:).id
expect(Role.student.where(user_id: student_1, school_id:)).to exist
expect(ClassStudent.where(student_id: student_1, school_class_id:)).to exist
expect(Role.student.where(user_id: student_2, school_id:)).to exist
expect(ClassStudent.where(student_id: student_2, school_class_id:)).to exist
end
context 'when the seeded school is in the US' do
let(:seed_country_code) { 'US' }
it 'creates a valid school and owner lessons' do
school = School.find_by(creator_id:)
school_class = SchoolClass.joins(:teachers).find_by(school_id: school.id, teachers: { teacher_id: creator_id })
expect(school).to be_valid
expect(school.district_nces_id).to match(/\A\d{7}\z/)
expect(school.district_name).to be_present
expect(school_class).not_to be_nil
expect(Lesson.where(school_id: school.id, school_class_id: school_class.id).length).to eq(2)
end
end
end
end