Only allow positive powers of two for playoff_teams_amount
This commit is contained in:
parent
e3e96c0c1d
commit
fc7409b18c
|
|
@ -10,6 +10,8 @@ class Tournament < ApplicationRecord
|
||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
validates :code, presence: true, uniqueness: true
|
validates :code, presence: true, uniqueness: true
|
||||||
|
|
||||||
|
validate :playoff_teams_amount_is_positive_power_of_two
|
||||||
|
|
||||||
alias_attribute :owner, :user
|
alias_attribute :owner, :user
|
||||||
|
|
||||||
after_initialize :generate_code
|
after_initialize :generate_code
|
||||||
|
|
@ -24,4 +26,11 @@ class Tournament < ApplicationRecord
|
||||||
break if errors['code'].blank?
|
break if errors['code'].blank?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def playoff_teams_amount_is_positive_power_of_two
|
||||||
|
return if (Utils.po2?(playoff_teams_amount) && playoff_teams_amount.positive?) || playoff_teams_amount.zero?
|
||||||
|
|
||||||
|
errors.add(:playoff_teams_amount_isnt_positive_power_of_two,
|
||||||
|
'playoff_teams_amount needs to be a positive power of two')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ class CreateSchema < ActiveRecord::Migration[5.2]
|
||||||
t.string :code, null: false, index: { unique: true }
|
t.string :code, null: false, index: { unique: true }
|
||||||
t.string :description
|
t.string :description
|
||||||
t.boolean :public, default: true
|
t.boolean :public, default: true
|
||||||
t.integer :playoff_teams_amount
|
t.integer :playoff_teams_amount, default: 0
|
||||||
t.integer :instant_finalists_amount, default: 0
|
t.integer :instant_finalists_amount, default: 0
|
||||||
t.integer :intermediate_round_participants_amount, default: 0
|
t.integer :intermediate_round_participants_amount, default: 0
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -194,6 +194,29 @@ RSpec.describe TournamentsController, type: :controller do
|
||||||
expect(@group_stage_tournament.playoff_teams_amount)
|
expect(@group_stage_tournament.playoff_teams_amount)
|
||||||
.to eq(create_group_tournament_data[:playoff_teams_amount])
|
.to eq(create_group_tournament_data[:playoff_teams_amount])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'playoff_teams_amount unacceptable' do
|
||||||
|
it 'is not a power of two' do
|
||||||
|
post :create, params: create_group_tournament_data.merge(playoff_teams_amount: 18)
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
expect(deserialize_response(response).values.first.first)
|
||||||
|
.to eq('playoff_teams_amount needs to be a positive power of two')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'isn\'t positive' do
|
||||||
|
post :create, params: create_group_tournament_data.merge(playoff_teams_amount: -16)
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
expect(deserialize_response(response).values.first.first)
|
||||||
|
.to eq('playoff_teams_amount needs to be a positive power of two')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'isn\'t positive nor a power of two' do
|
||||||
|
post :create, params: create_group_tournament_data.merge(playoff_teams_amount: -42)
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
expect(deserialize_response(response).values.first.first)
|
||||||
|
.to eq('playoff_teams_amount needs to be a positive power of two')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'renders a JSON response with the new tournament' do
|
it 'renders a JSON response with the new tournament' do
|
||||||
|
|
@ -234,12 +257,19 @@ RSpec.describe TournamentsController, type: :controller do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with invalid parameters' do
|
context 'with invalid parameters' do
|
||||||
|
shared_examples_for 'renders unprocessable entity response' do
|
||||||
it 'renders an unprocessable entity response' do
|
it 'renders an unprocessable entity response' do
|
||||||
put :create, params: { teams: [1, 2, 3] }
|
|
||||||
expect(response).to have_http_status(:unprocessable_entity)
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
put :create, params: context
|
||||||
|
end
|
||||||
|
|
||||||
|
context ''
|
||||||
|
end
|
||||||
|
|
||||||
context 'with empty team objects' do
|
context 'with empty team objects' do
|
||||||
it 'renders an unprocessable entity response' do
|
it 'renders an unprocessable entity response' do
|
||||||
data = create_group_tournament_data
|
data = create_group_tournament_data
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue