From 0eb27efd2327cee4fbb260878cfe3689e39a0471 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Mon, 13 May 2019 11:07:13 +0200 Subject: [PATCH] Test populate_match_below method --- spec/services/playoff_stage_service_spec.rb | 69 +++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/spec/services/playoff_stage_service_spec.rb b/spec/services/playoff_stage_service_spec.rb index 46dfe55..ef2bcc0 100644 --- a/spec/services/playoff_stage_service_spec.rb +++ b/spec/services/playoff_stage_service_spec.rb @@ -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