Test populate_match_below method

This commit is contained in:
Daniel Schädler 2019-05-13 11:07:13 +02:00
parent c758ddfe2b
commit 0eb27efd23
1 changed files with 69 additions and 0 deletions

View File

@ -80,4 +80,73 @@ RSpec.describe PlayoffStageService do
end
end
end
describe '# populate_match_below' do
before :each do
@tournament = create(:stage_tournament, stage_count: 2)
@match = @tournament.stages.find { |s| s.level == 2 }.matches.first
@match.state = :finished
@match.match_scores.each_with_index { |ms, i| ms.points = i}
@match.save
@companion_match = @tournament.stages.find { |s| s.level == 2 }.matches.second
@companion_match.match_scores.each_with_index { |ms, i| ms.points = i}
@companion_match.save
@match_to_find = @tournament.stages.find { |s| s.level == 1 }.matches.first
end
context 'match below has no match_scores' do
it 'finds the correct match and adds two new match_scores to it' do
@match_to_find.match_scores = []
@match_to_find.save
PlayoffStageService.populate_match_below(@match)
@match_to_find.reload
expect(@match_to_find.teams).to match_array(@match.winner)
end
end
context 'match below has one match_score with the winning team' do
it 'finds the correct match and adds no match_score' do
@match_to_find.match_scores = create_list(:match_score, 1, team: @match.winner)
@match_to_find.save
PlayoffStageService.populate_match_below(@match)
@match_to_find.reload
expect(@match_to_find.teams).to match_array(@match.winner)
end
end
context 'match below has one match_score with an unknown team' do
it 'finds the correct match and replaces the match_score' do
@match_to_find.match_scores = create_list(:match_score, 1, team: create(:team), points: 1337)
@match_to_find.save
PlayoffStageService.populate_match_below(@match)
@match_to_find.reload
expect(@match_to_find.teams).to match_array(@match.winner)
expect(@match_to_find.match_scores.first.points).to_not be(1337)
end
end
context 'match below has one match_score with the correct team' do
it 'finds the correct match and replaces nothing' do
@match_to_find.match_scores = create_list(:match_score, 1, team: @match.winner, points: 42)
@match_to_find.save
PlayoffStageService.populate_match_below(@match)
@match_to_find.reload
expect(@match_to_find.teams).to match_array(@match.winner)
expect(@match_to_find.match_scores.first.points).to be(42)
end
end
context 'match below has two match_scores with the correct teams' do
it 'finds the correct match and replaces nothing' do
@companion_match.state = :finished
@companion_match.save
@match_to_find.match_scores = [create(:match_score, team: @match.winner),
create(:match_score, team: @companion_match.winner)]
@match_to_find.save
PlayoffStageService.populate_match_below(@match)
@match_to_find.reload
expect(@match_to_find.teams).to match_array([@match.winner, @companion_match.winner])
end
end
end
end