Stabilize backend test fixtures

This commit is contained in:
Daniel Schädler 2026-04-15 12:29:32 +02:00
parent ce7b5c5ac7
commit 3e2400616b
6 changed files with 45 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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