forked from expertiza/expertiza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
course_team_spec.rb
91 lines (91 loc) · 3.95 KB
/
course_team_spec.rb
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
describe 'CourseTeam' do
let(:course_team1) { build(:course_team, id: 1, name: 'no team') }
let(:user2) { build(:student, id: 2, name: 'no name') }
let(:participant) { build(:participant, user: user2) }
let(:course) { build(:course, id: 1, name: 'ECE517') }
let(:team_user) { build(:team_user, id: 1, user: user2) }
describe 'copy course team to assignment team' do
it 'should allow course team to be copied to assignment team' do
assignment = build(Assignment)
assignment.name = 'test'
assignment.save!
assignment_team = AssignmentTeam.new
assignment_team.save
course_team = CourseTeam.new
course_team.copy(assignment_team.id)
expect(AssignmentTeam.create_team_and_node(assignment_team.id))
expect(assignment_team.copy_members(assignment_team.id))
end
end
describe '#parent_model' do
it 'returns parent_model' do
course_team = CourseTeam.new
expect(course_team.parent_model).to eq('Course')
end
end
describe '#assignment_id' do
it 'returns nil since this team is not an assignment team' do
course_team = CourseTeam.new
expect(course_team.assignment_id).to be_nil
end
end
describe '#prototype' do
it 'creates a course team' do
expect(CourseTeam.prototype.class).to eq(CourseTeam)
end
end
describe '#add_participant' do
it 'adds a participant to the course when it is not already added' do
allow(CourseParticipant).to receive(:find_by).with(parent_id: 1, user_id: 2).and_return(nil)
allow(CourseParticipant).to receive(:create).with(parent_id: 1, user_id: 2, permission_granted: 0).and_return(participant)
expect(course_team1.add_participant(1, user2)).to eq(participant)
end
end
describe '#import' do
context 'when the course does not exist' do
it 'raises an import error' do
allow(Course).to receive(:find).with(1).and_return(nil)
expect { CourseTeam.import({}, 1, nil) }.to raise_error(ImportError)
end
end
context 'when the course does exist' do
it 'raises an error with empty row hash' do
allow(Course).to receive(:find).with(1).and_return(course)
expect { CourseTeam.import({}, 1, nil) }.to raise_error(ArgumentError)
end
end
end
describe '#export' do
it 'writes to a csv' do
allow(CourseTeam).to receive(:where).with(parent_id: 1).and_return([course_team1])
allow(TeamsParticipant).to receive(:where).with(team_id: 1).and_return([team_user])
allow(User).to receive(:find).with(user2.id).and_return(user2)
expect(CourseTeam.export([], 1, team_name: 'false')).to eq([['no team', 'no name']])
end
end
describe '#export_fields' do
it 'returns a list of headers' do
expect(CourseTeam.export_fields(team_name: 'false')).to eq(['Team Name', 'Team members', 'Course Name'])
end
end
describe '#add_member' do
context 'when the user is already on the team' do
it 'raises an error' do
allow(course_team1).to receive(:participant?).with(participant).and_return(true)
expect { course_team1.add_participant_to_team(participant) }.to raise_error('The user "no name" is already a member of the team, "no team"')
end
end
context 'when the user is not on the team' do
it 'adds user' do
node = TeamNode.new
allow(course_team1).to receive(:user?).with(user2).and_return(false)
allow(TeamsParticipant).to receive(:create).with(participant_id: participant.id, team_id: 1).and_return(team_user)
allow(TeamNode).to receive(:find_by).with(node_object_id: 1).and_return(node)
allow(course_team1).to receive(:add_participant).with(1, user2).and_return(participant)
allow(CourseParticipant).to receive(:find_by).with(parent_id: 1, user_id: user2.id).and_return(participant)
allow(course_team1).to receive(:parent_id).and_return(1)
expect(course_team1.add_participant_to_team(participant).node_object_id).to eq(team_user.id)
end
end
end
end