diff --git a/app/models/match_score.rb b/app/models/match_score.rb index 408952d..b2a3f0c 100644 --- a/app/models/match_score.rb +++ b/app/models/match_score.rb @@ -4,5 +4,5 @@ class MatchScore < ApplicationRecord belongs_to :match belongs_to :team - delegate :owner, to: :team + delegate :owner, to: :match end diff --git a/spec/controllers/statistics_controller_spec.rb b/spec/controllers/statistics_controller_spec.rb index 36673b2..d516098 100644 --- a/spec/controllers/statistics_controller_spec.rb +++ b/spec/controllers/statistics_controller_spec.rb @@ -13,7 +13,7 @@ RSpec.describe StatisticsController, type: :controller do context 'tournament with a group stage' do before do - @tournament = create(:group_stage_tournament) + @tournament = create(:group_stage_only_tournament) @group_stage = @tournament.stages.find_by(level: -1) @most_dominant_score = create(:group_score, team: @tournament.teams.first, diff --git a/spec/controllers/teams_controller_spec.rb b/spec/controllers/teams_controller_spec.rb index 7bcbdb8..8dc9eb1 100644 --- a/spec/controllers/teams_controller_spec.rb +++ b/spec/controllers/teams_controller_spec.rb @@ -4,7 +4,7 @@ require 'rails_helper' RSpec.describe TeamsController, type: :controller do before do - @team = create(:team) + @team = create(:tournament).teams.first @owner = @team.owner end diff --git a/spec/controllers/tournaments_controller_spec.rb b/spec/controllers/tournaments_controller_spec.rb index da27ca7..7231e60 100644 --- a/spec/controllers/tournaments_controller_spec.rb +++ b/spec/controllers/tournaments_controller_spec.rb @@ -8,8 +8,8 @@ RSpec.describe TournamentsController, type: :controller do @user = @tournament.owner @another_user = create(:user) @private_tournament = create(:tournament, user: @another_user, public: false) - @teams = create_list(:detached_team, 4) - @teams16 = create_list(:detached_team, 16) + @teams = create_list(:team, 4) + @teams16 = create_list(:team, 16) @groups = create_list(:group, 4) end diff --git a/spec/factories/matches.rb b/spec/factories/matches.rb index 3f0a89c..030c87c 100644 --- a/spec/factories/matches.rb +++ b/spec/factories/matches.rb @@ -3,6 +3,7 @@ FactoryBot.define do factory :playoff_match, aliases: [:match], class: Match do stage + position { 0 } factory :running_playoff_match do transient do match_scores_count { 2 } @@ -10,12 +11,29 @@ FactoryBot.define do after(:create) do |match, evaluator| match.match_scores = create_list(:match_score, evaluator.match_scores_count) end - state { 3 } + state { :in_progress } + end + + factory :empty_prepared_playoff_match do + state { :not_started } + end + + factory :decided_playoff_match do + transient do + match_scores_count { 2 } + end + after(:create) do |match, evaluator| + match.match_scores = create_list(:match_score, evaluator.match_scores_count, points: 37) + # random number generated by blapplications + match.match_scores.first.points += 1 + end + state { :team1_won } end end factory :group_match, class: Match do group + position { 0 } factory :running_group_match do transient do match_scores_count { 2 } @@ -23,7 +41,17 @@ FactoryBot.define do after(:create) do |match, evaluator| match.match_scores = create_list(:match_score, evaluator.match_scores_count) end - state { 3 } + state { :in_progress } + end + + factory :undecided_group_match do + transient do + match_scores_count { 2 } + end + after(:create) do |match, evaluator| + match.match_scores = create_list(:match_score, evaluator.match_scores_count, points: 3) + end + state { :team1_won } end end end diff --git a/spec/factories/stages.rb b/spec/factories/stages.rb index e8dce2d..b5736cd 100644 --- a/spec/factories/stages.rb +++ b/spec/factories/stages.rb @@ -16,10 +16,18 @@ FactoryBot.define do factory :playoff_stage do level { rand(10) } transient do + match_type { :running_playoff_match } match_count { 4 } end after(:create) do |stage, evaluator| - stage.matches = create_list(:match, evaluator.match_count) + # match_count -1 automatically generates 2 ^ stage.level matches + # (as this would be the amount of stages present in the real world) + stage.matches = create_list(evaluator.match_type, + evaluator.match_count == -1 ? 2**stage.level : evaluator.match_count) + stage.matches.each_with_index do |match, i| + match.position = i + end + stage.save! end end end diff --git a/spec/factories/teams.rb b/spec/factories/teams.rb index b33adc4..234f71c 100644 --- a/spec/factories/teams.rb +++ b/spec/factories/teams.rb @@ -3,10 +3,5 @@ FactoryBot.define do factory :team do name { Faker::Creature::Dog.name } - tournament - end - - factory :detached_team, class: Team do - name { Faker::Creature::Dog.name } end end diff --git a/spec/factories/tournaments.rb b/spec/factories/tournaments.rb index 6924332..6d7098e 100644 --- a/spec/factories/tournaments.rb +++ b/spec/factories/tournaments.rb @@ -1,29 +1,55 @@ # frozen_string_literal: true FactoryBot.define do - factory :tournament do + factory :tournament, aliases: [:stageless_tournament] do name { Faker::Creature::Dog.name } description { Faker::Lorem.sentence } user transient do - teams_count { 16 } + teams_count { 8 } end after(:create) do |tournament, evaluator| tournament.teams = create_list(:team, evaluator.teams_count, tournament: tournament) end + factory :group_stage_only_tournament do + transient do + group_count { 2 } + end + after(:create) do |tournament, evaluator| + tournament.stages << create(:group_stage, group_count: evaluator.group_count) + end + end + factory :stage_tournament do transient do stage_count { 1 } end after(:create) do |tournament, evaluator| - tournament.stages = create_list(:stage, evaluator.stage_count) + # this is basically a manual create_list as we need to count up the level of the stage + (1..evaluator.stage_count).each do |level| + tournament.stages << create( + :playoff_stage, + level: level, + match_count: -1, + match_type: evaluator.stage_count ? :running_playoff_match : :empty_prepared_playoff_match + ) + end + end + + factory :group_stage_tournament do + after(:create) do |tournament, _evaluator| + tournament.stages << create(:group_stage) + end end end - factory :group_stage_tournament do - after(:create) do |tournament, _evaluator| - tournament.stages = create_list(:group_stage, 1) + factory :dummy_stage_tournament do + transient do + stage_count { 3 } + end + after(:create) do |tournament, evaluator| + tournament.stages.concat create_list(:stage, evaluator.stage_count) end end end