Merge pull request #37 from turniere/ticket/TURNIERE-196

Factory Overhaul
This commit is contained in:
Daniel Schädler 2019-05-22 10:03:32 +02:00 committed by GitHub
commit 0c9c0bd526
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 76 additions and 19 deletions

View File

@ -4,5 +4,5 @@ class MatchScore < ApplicationRecord
belongs_to :match
belongs_to :team
delegate :owner, to: :team
delegate :owner, to: :match
end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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_list(:group_stage, 1)
tournament.stages << create(:group_stage)
end
end
end
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