Stabilize backend test fixtures
This commit is contained in:
parent
ce7b5c5ac7
commit
3e2400616b
|
|
@ -3,6 +3,35 @@
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe StagesController, type: :controller do
|
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
|
let(:stage) do
|
||||||
create(:playoff_stage)
|
create(:playoff_stage)
|
||||||
end
|
end
|
||||||
|
|
@ -29,7 +58,7 @@ RSpec.describe StagesController, type: :controller do
|
||||||
|
|
||||||
context 'group_stage with matches that are done' do
|
context 'group_stage with matches that are done' do
|
||||||
let(:running_group_stage) do
|
let(:running_group_stage) do
|
||||||
create(:group_stage, match_factory: :finished_group_match)
|
create_finished_resolved_group_stage
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'doesn\'t have any other stages besides it before update' do
|
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
|
context 'already finished group stage' do
|
||||||
let(: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.finished!
|
||||||
group_stage.save!
|
group_stage.save!
|
||||||
group_stage
|
group_stage
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,8 @@ RSpec.describe StatisticsController, type: :controller do
|
||||||
|
|
||||||
it 'returns a list containing all group scores' do
|
it 'returns a list containing all group scores' do
|
||||||
get :index, params: { tournament_id: @tournament.to_param }
|
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
|
end
|
||||||
|
|
||||||
it 'returns a most dominant group score' do
|
it 'returns a most dominant group score' do
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,11 @@ FactoryBot.define do
|
||||||
|
|
||||||
factory :finished_group_match do
|
factory :finished_group_match do
|
||||||
state { :finished }
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
factory :user do
|
factory :user do
|
||||||
username { Faker::Internet.unique.username }
|
sequence(:username) { |n| "user#{n}" }
|
||||||
email { Faker::Internet.unique.email }
|
sequence(:email) { |n| "user#{n}@example.com" }
|
||||||
password { Faker::Internet.password }
|
password { 'password123' }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,7 @@ RSpec.describe GroupStageService do
|
||||||
|
|
||||||
context 'with only undecided matches' do
|
context 'with only undecided matches' do
|
||||||
before 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|
|
@group.matches.each do |match|
|
||||||
match.match_scores.each do |ms|
|
match.match_scores.each do |ms|
|
||||||
ms.points = 42
|
ms.points = 42
|
||||||
|
|
@ -143,7 +143,7 @@ RSpec.describe GroupStageService do
|
||||||
|
|
||||||
context 'with only decided matches' do
|
context 'with only decided matches' do
|
||||||
before 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|
|
@group.matches.each_with_index do |match, i|
|
||||||
match.match_scores.each_with_index do |ms, j|
|
match.match_scores.each_with_index do |ms, j|
|
||||||
match_score_number = i + j
|
match_score_number = i + j
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ RSpec.describe StatisticsService do
|
||||||
# build tournament with predictable test data
|
# build tournament with predictable test data
|
||||||
tournament = create(:tournament)
|
tournament = create(:tournament)
|
||||||
group_stage = create(:group_stage)
|
group_stage = create(:group_stage)
|
||||||
|
@group_stage = group_stage
|
||||||
group = group_stage.groups.first
|
group = group_stage.groups.first
|
||||||
@most_dominant_score = create(:group_score,
|
@most_dominant_score = create(:group_score,
|
||||||
group_points: 100,
|
group_points: 100,
|
||||||
|
|
@ -48,7 +49,7 @@ RSpec.describe StatisticsService do
|
||||||
|
|
||||||
describe '#group_scores' do
|
describe '#group_scores' do
|
||||||
it 'returns an array containing all 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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue