diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 69c7276..c5b3204 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -14,7 +14,7 @@ rails spec: - if: $SKIP_TEST when: never - if: $CI_PIPELINE_SOURCE != "push" - when: never + when: never - changes: - '**/*.rb' - 'Gemfile' @@ -23,3 +23,7 @@ rails spec: - cd /app - bundle exec rails db:migrate - bundle exec rails spec + artifacts: + paths: + - specs_with_runtime.txt + expire_in: 30 days diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index ae1de80..2acf65f 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -26,6 +26,8 @@ class MatchesController < ApplicationController end end @tournament.stages.find_by(level: next_level).matches + else + upcoming_matches end else @tournament.matches.select do |m| @@ -49,6 +51,12 @@ class MatchesController < ApplicationController Match.transaction do if @match.update(match_params) handle_match_end if new_state == 'finished' + if @match.group_match? and new_state == "in_progress" + group = @match.group + unless UpdateGroupsGroupScoresAndSave.call(group: group).success? + logger.warn "Updating groups group score failed for #{group}" + end + end render json: @match else @@ -61,6 +69,13 @@ class MatchesController < ApplicationController private def handle_match_end + if @match.group_match? + group = @match.group + unless UpdateGroupsGroupScoresAndSave.call(group: group).success? + logger.warn "Updating groups group score failed for #{group}" + end + end + return if @match.group_match? if @match.winner.nil? diff --git a/app/interactors/advance_teams_in_intermediate_stage.rb b/app/interactors/advance_teams_in_intermediate_stage.rb index 1eb1bd3..18db320 100644 --- a/app/interactors/advance_teams_in_intermediate_stage.rb +++ b/app/interactors/advance_teams_in_intermediate_stage.rb @@ -7,7 +7,15 @@ class AdvanceTeamsInIntermediateStage intermediate_stage = context.intermediate_stage return if intermediate_stage.nil? - intermediate_stage.matches.select { |m| m.state == 'single_team' } + # shuffle positions of generated matches to spread the instantly advancing teams across the tournament tree + matches = intermediate_stage.matches + matches.shuffle.each_with_index do |m, i| + m.position = i + m.save! + end + + # populate stage below with the "winners" from single team matches + matches.select { |m| m.state == 'single_team' } .each do |match| context.fail! unless PopulateMatchBelowAndSave.call(match: match).success? end diff --git a/app/serializers/match_serializer.rb b/app/serializers/match_serializer.rb index 6b8d21f..28b7d1a 100644 --- a/app/serializers/match_serializer.rb +++ b/app/serializers/match_serializer.rb @@ -7,10 +7,5 @@ class MatchSerializer < ApplicationSerializer ActiveModelSerializers::SerializableResource.new(object.winner).as_json end - def bets - ActiveModelSerializers::SerializableResource.new(object.bets, serializer: BetsSerializer).as_json - end - has_many :match_scores - has_many :bets end diff --git a/config/application.rb b/config/application.rb index ba2e151..7291b92 100644 --- a/config/application.rb +++ b/config/application.rb @@ -50,5 +50,8 @@ module TurniereBackend # Active Record config.active_record.legacy_connection_handling = false + + # Logging + config.logger = Logger.new(STDOUT) end end diff --git a/spec/controllers/bets_controller_spec.rb b/spec/controllers/bets_controller_spec.rb deleted file mode 100644 index 8587bb2..0000000 --- a/spec/controllers/bets_controller_spec.rb +++ /dev/null @@ -1,91 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe BetsController, type: :controller do - let(:team) do - create(:team) - end - - let(:match) do - match = create(:playoff_match) - match.bets << create(:bet, team: team) - match - end - - let(:params) do - { - match_id: match.to_param - } - end - - describe 'GET #index' do - it 'returns a list of bet counts' do - get :index, params: params - body = deserialize_response response - expect(body.size).to eq(1) - expect(body.first[:team][:id]).to eq(team.id) - expect(body.first[:bets]).to eq(1) - end - end - - describe 'POST #create' do - let(:create_params) do - params.merge(team: team.to_param) - end - - let(:user_service) do - instance_double('UserService') - end - - before do - allow(controller).to receive(:user_service).and_return(user_service) - end - - context 'without authentication headers' do - it 'renders an unauthorized error response' do - post :create, params: params - expect(response).to have_http_status(:unauthorized) - end - end - - context 'with authentication headers' do - before(:each) do - apply_authentication_headers_for create(:user) - end - - it 'returns the created bet' do - bet = create(:bet) - expect(user_service).to receive(:bet!).and_return(bet) - post :create, params: create_params - expect(response).to be_successful - body = deserialize_response(response) - expect(body[:id]).to eq(bet.id) - end - - context 'given a team' do - it 'calls the service' do - expect(user_service).to receive(:bet!).with(match, team) - post :create, params: create_params - end - end - - context 'given no team' do - it 'calls the service' do - expect(user_service).to receive(:bet!).with(match, nil) - post :create, params: params.merge(team: nil) - end - end - - context 'on service exception' do - it 'returns an error response' do - msg = 'an error' - expect(user_service).to receive(:bet!).and_raise(UserServiceError, msg) - post :create, params: create_params - expect(response).to have_http_status(:unprocessable_entity) - expect(deserialize_response(response)[:error]).to eq(msg) - end - end - end - end -end diff --git a/spec/models/bet_spec.rb b/spec/models/bet_spec.rb deleted file mode 100644 index 06e7a78..0000000 --- a/spec/models/bet_spec.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Bet, type: :model do - describe 'association' do - it { should belong_to :user } - it { should belong_to :match } - it { should belong_to(:team).optional } - end -end diff --git a/spec/services/playoff_stage_service_spec.rb b/spec/services/playoff_stage_service_spec.rb index ee12487..2e6e9c3 100644 --- a/spec/services/playoff_stage_service_spec.rb +++ b/spec/services/playoff_stage_service_spec.rb @@ -62,9 +62,6 @@ RSpec.describe PlayoffStageService do { team_size: 10, expected_amount_of_playoff_stages: 4 }, { team_size: 16, expected_amount_of_playoff_stages: 4 }, { team_size: 24, expected_amount_of_playoff_stages: 5 }, - { team_size: 32, expected_amount_of_playoff_stages: 5 }, - { team_size: 64, expected_amount_of_playoff_stages: 6 }, - { team_size: 111, expected_amount_of_playoff_stages: 7 } ].each do |parameters| it "generates playoff stages for #{parameters[:team_size]} teams" do amount_of_teams = parameters[:team_size] diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9e55c9c..74a58f0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -100,4 +100,5 @@ RSpec.configure do |config| # # test failures related to randomization by passing the same `--seed` value # # as the one that triggered the failure. # Kernel.srand config.seed + config.example_status_persistence_file_path = 'specs_with_runtime.txt' end