Merge branch 'performance' into 'master'

Remove bets & some useless tests

See merge request turniere/turniere-backend!25
This commit is contained in:
Jonas Seydel 2022-07-01 22:31:13 +00:00
commit 234f3ec088
9 changed files with 33 additions and 112 deletions

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -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

View File

@ -50,5 +50,8 @@ module TurniereBackend
# Active Record
config.active_record.legacy_connection_handling = false
# Logging
config.logger = Logger.new(STDOUT)
end
end

View File

@ -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

View File

@ -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

View File

@ -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]

View File

@ -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