diff --git a/spec/controllers/stages_controller_spec.rb b/spec/controllers/stages_controller_spec.rb index 8a66ced..0b30611 100644 --- a/spec/controllers/stages_controller_spec.rb +++ b/spec/controllers/stages_controller_spec.rb @@ -3,6 +3,35 @@ require 'rails_helper' RSpec.describe StagesController, type: :controller do + def create_finished_resolved_group_stage(group_count: 4, teams_per_group: 2) + tournament = create( + :tournament, + teams_count: group_count * teams_per_group, + playoff_teams_amount: group_count, + instant_finalists_amount: group_count, + intermediate_round_participants_amount: 0 + ) + groups = tournament.teams.each_slice(teams_per_group).to_a + stage = GroupStageService.generate_group_stage(groups) + tournament.stages << stage + tournament.save! + + stage.groups.each do |group| + group.matches.each do |match| + match.state = :finished + match.match_scores.each_with_index do |match_score, index| + match_score.points = index.zero? ? 1 : 0 + match_score.save! + end + match.save! + end + + UpdateGroupsGroupScoresAndSave.call(group: group) + end + + stage.reload + end + let(:stage) do create(:playoff_stage) end @@ -29,7 +58,7 @@ RSpec.describe StagesController, type: :controller do context 'group_stage with matches that are done' do let(:running_group_stage) do - create(:group_stage, match_factory: :finished_group_match) + create_finished_resolved_group_stage end it 'doesn\'t have any other stages besides it before update' do @@ -98,7 +127,7 @@ RSpec.describe StagesController, type: :controller do context 'already finished group stage' do let(:finished_group_stage) do - group_stage = create(:group_stage, match_factory: :finished_group_match) + group_stage = create_finished_resolved_group_stage group_stage.finished! group_stage.save! group_stage diff --git a/spec/controllers/statistics_controller_spec.rb b/spec/controllers/statistics_controller_spec.rb index bd105e1..b0af6ea 100644 --- a/spec/controllers/statistics_controller_spec.rb +++ b/spec/controllers/statistics_controller_spec.rb @@ -35,7 +35,8 @@ RSpec.describe StatisticsController, type: :controller do it 'returns a list containing all group scores' do get :index, params: { tournament_id: @tournament.to_param } - expect(deserialize_response(response)[:group_scores].length).to eq(GroupScore.count) + expected_count = @group_stage.groups.sum { |group| group.group_scores.size } + expect(deserialize_response(response)[:group_scores].length).to eq(expected_count) end it 'returns a most dominant group score' do diff --git a/spec/factories/matches.rb b/spec/factories/matches.rb index 9e10eb0..b05ca6e 100644 --- a/spec/factories/matches.rb +++ b/spec/factories/matches.rb @@ -63,6 +63,11 @@ FactoryBot.define do factory :finished_group_match do state { :finished } + after(:create) do |match| + match.match_scores.order(:id).each_with_index do |match_score, index| + match_score.update!(points: index.zero? ? 1 : 0) + end + end end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 196c94f..9667dc8 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -2,8 +2,8 @@ FactoryBot.define do factory :user do - username { Faker::Internet.unique.username } - email { Faker::Internet.unique.email } - password { Faker::Internet.password } + sequence(:username) { |n| "user#{n}" } + sequence(:email) { |n| "user#{n}@example.com" } + password { 'password123' } end end diff --git a/spec/services/group_stage_service_spec.rb b/spec/services/group_stage_service_spec.rb index c3d0cf6..1b3a46b 100644 --- a/spec/services/group_stage_service_spec.rb +++ b/spec/services/group_stage_service_spec.rb @@ -110,7 +110,7 @@ RSpec.describe GroupStageService do context 'with only undecided matches' do before do - @group = create(:group, match_factory: :running_group_match) + @group = create(:group, match_count: 1, match_factory: :running_group_match) @group.matches.each do |match| match.match_scores.each do |ms| ms.points = 42 @@ -143,7 +143,7 @@ RSpec.describe GroupStageService do context 'with only decided matches' do before do - @group = create(:group, match_factory: :running_group_match) + @group = create(:group, match_count: 1, 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 diff --git a/spec/services/statistics_service_spec.rb b/spec/services/statistics_service_spec.rb index ec6b3fa..e3b251b 100644 --- a/spec/services/statistics_service_spec.rb +++ b/spec/services/statistics_service_spec.rb @@ -5,6 +5,7 @@ RSpec.describe StatisticsService do # build tournament with predictable test data tournament = create(:tournament) group_stage = create(:group_stage) + @group_stage = group_stage group = group_stage.groups.first @most_dominant_score = create(:group_score, group_points: 100, @@ -48,7 +49,7 @@ RSpec.describe StatisticsService do describe '#group_scores' do it 'returns an array containing all group scores' do - expect(@service.group_scores.length).to eq(GroupScore.count) + expect(@service.group_scores.length).to eq(@group_stage.groups.sum { |group| group.group_scores.size }) end end end