From 3d8abe449af3aa15f82245854987fc8b7ed8f21c Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Tue, 4 Jun 2019 14:38:21 +0200 Subject: [PATCH] Test update_group_scores method --- spec/services/group_stage_service_spec.rb | 68 +++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/spec/services/group_stage_service_spec.rb b/spec/services/group_stage_service_spec.rb index 1c10e53..8c6ae59 100644 --- a/spec/services/group_stage_service_spec.rb +++ b/spec/services/group_stage_service_spec.rb @@ -77,4 +77,72 @@ RSpec.describe GroupStageService do end end end + + describe '#update_group_scores' do + shared_examples_for 'only_return_group_scores' do + it 'only returns group_scores' do + @changed_group_scores.each do |gs| + expect(gs).to be_a(GroupScore) + end + end + end + + context 'with only undecided matches' do + before do + @group = create(:group, match_factory: :running_group_match) + @group.matches.each do |match| + match.match_scores.each do |ms| + ms.points = 42 + ms.save! + end + end + @changed_group_scores = GroupStageService.update_group_scores(@group) + end + + it 'assigns 1 point to every team' do + @changed_group_scores.map(&:group_points).each do |points| + expect(points).to be(1) + end + end + + it_should_behave_like 'only_return_group_scores' + + it 'returns correct values for received_points' do + @changed_group_scores.map(&:received_points).each do |points| + expect(points).to be(42) + end + end + + it 'returns correct values for scored_points' do + @changed_group_scores.map(&:scored_points).each do |points| + expect(points).to be(42) + end + end + end + + context 'with only decided matches' do + before do + @group = create(:group, match_factory: :running_group_match) + @group.matches.each_with_index do |match, i| + match.match_scores.each_with_index do |ms, j| + match_score_number = i + j + ms.points = match_score_number + ms.save! + end + end + @changed_group_scores = GroupStageService.update_group_scores(@group) + end + + it 'assigns the right amount of points' do + winning_teams = @changed_group_scores.select { |gs| gs.group_points == 3 } + losing_teams = @changed_group_scores.select { |gs| gs.group_points == 0 } + # Assure that half of the teams won and got 3 points + expect(winning_teams.size).to be(@changed_group_scores.size / 2) + # and half of them lost and got 0 + expect(losing_teams.size).to be(@changed_group_scores.size / 2) + end + + it_should_behave_like 'only_return_group_scores' + end + end end