diff --git a/app/models/match.rb b/app/models/match.rb index 0c5e676..df8e8b7 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -31,6 +31,7 @@ class Match < ApplicationRecord private def evaluate_winner + return nil unless finished? return nil if match_scores.first.points == match_scores.second.points match_scores.max_by(&:points).team diff --git a/app/services/playoff_stage_service.rb b/app/services/playoff_stage_service.rb index ce75551..08a5394 100644 --- a/app/services/playoff_stage_service.rb +++ b/app/services/playoff_stage_service.rb @@ -114,18 +114,14 @@ class PlayoffStageService match_scores = winners.map { |winner| MatchScore.new(team: winner) } when 1 # when 1 match_score is present, we need to check which team is contained within and add the other team as well - team = nil - if match_scores.first.team == winners.first - team = winners.second + match_scores.push MatchScore.new(team: winners.second) elsif match_scores.first.team == winners.second - team = winners.first + match_scores.push MatchScore.new(team: winners.first) else match_scores.first.team = winners.first - team = winners.second + match_scores.push MatchScore.new(team: winners.second) end - - match_scores.concat MatchScore.new(team: team) when 2 # when 2 match_scores are present, the teams just get overwritten match_scores.first.team = winners.first @@ -136,12 +132,6 @@ class PlayoffStageService def self.get_winners_of(companion_match, current_match) matches = [current_match, companion_match].sort_by(&:position) - if companion_match.finished? - matches.map(&:winner) - else - matches.map do |m| - m == current_match ? m.winner : nil - end - end + matches.map(&:winner) end end diff --git a/db/seeds.rb b/db/seeds.rb index 5944c54..f8dff95 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,26 +1,5 @@ # frozen_string_literal: true -require 'faker' +require 'factorybot' -user = User.create! username: Faker::Internet.username, email: Faker::Internet.email, password: Faker::Internet.password -tournament = user.tournaments.create! name: Faker::Creature::Dog.name - -@teams = [] -16.times do - team = tournament.teams.create! name: Faker::HarryPotter.character - @teams << team -end -4.times do |i| - stage = tournament.stages.create! - stage.level = i - matches_amount = 2**i - matches_amount.times do |j| - match = stage.matches.create! - match.match_scores.create! team: @teams.sample, points: rand(10) - match.match_scores.create! team: @teams.sample, points: rand(10) - match.position = j - match.state = rand(7) - match.save! - end - stage.save! -end +create(:tournament) diff --git a/spec/controllers/matches_controller_spec.rb b/spec/controllers/matches_controller_spec.rb index b5116bd..63ee351 100644 --- a/spec/controllers/matches_controller_spec.rb +++ b/spec/controllers/matches_controller_spec.rb @@ -92,7 +92,8 @@ RSpec.describe MatchesController, type: :controller do end it 'fills the match below' do - match_below = @tournament.stages.find { |s| s.level == 2 }.matches.find { |m| m.position == @running_playoff_match.position / 2 }.reload + match_below = @tournament.stages.find { |s| s.level == 2 } + .find { |m| m.position == @running_playoff_match.position / 2 }.reload expect(match_below.teams).to include(@running_playoff_match.winner) end end diff --git a/spec/factories/tournaments.rb b/spec/factories/tournaments.rb index 22cf099..610d88a 100644 --- a/spec/factories/tournaments.rb +++ b/spec/factories/tournaments.rb @@ -28,13 +28,17 @@ FactoryBot.define do after(:create) do |tournament, evaluator| # this is basically a manual create_list as we need to count up the level of the stage levels = 1..evaluator.stage_count - tournament.stages.concat levels - .map { |level| - create(:playoff_stage, - level: level, - match_count: -1, - match_type: level == evaluator.stage_count ? :running_playoff_match : :empty_prepared_playoff_match) - } + + tournament.stages.concat(levels.map do |level| + create(:playoff_stage, + level: level, + match_count: -1, + match_type: if evaluator.stage_count + :running_playoff_match + else + :empty_prepared_playoff_match + end) + end) end factory :group_stage_tournament do diff --git a/spec/interactors/short_test.rb b/spec/interactors/short_test.rb new file mode 100644 index 0000000..fef43b5 --- /dev/null +++ b/spec/interactors/short_test.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +RSpec.describe AddGroupStageToTournament do + before do + # @empty_tournament = create(:stage_tournament, stage_count: 0) + end + + context 'Hello, this' do + context 'is a dummy and it' do + it 'succeeds' do + expect(true).to be(true) + end + end + end +end diff --git a/spec/services/playoff_stage_service_spec.rb b/spec/services/playoff_stage_service_spec.rb index 46dfe55..59cfe8f 100644 --- a/spec/services/playoff_stage_service_spec.rb +++ b/spec/services/playoff_stage_service_spec.rb @@ -80,4 +80,45 @@ RSpec.describe PlayoffStageService do end end end + + describe '# populate_match_below' do + before do + @tournament = create(:stage_tournament, stage_count: 2) + @match = @tournament.stages.find { |s| s.level == 2 }.matches.first + @match.state = :finished + @match.save + @companion_match = @tournament.stages.find { |s| s.level == 2 }.matches.second + @match_to_find = @tournament.stages.find { |s| s.level == 1 }.matches.first + end + + context 'match below has no match_scores' do + it 'finds the correct match and adds two new match_scores to it' do + @match_to_find.match_scores = [] + @match_to_find.save + PlayoffStageService.populate_match_below(@match) + @match_to_find.reload + expect(@match_to_find.teams).to match_array(@match.winner) + end + end + + context 'match below has one match_score with the winning team' do + it 'finds the correct match and adds no match_score' do + @match_to_find.match_scores = create_list(:match_score, 1, team: @match.winner) + @match_to_find.save + PlayoffStageService.populate_match_below(@match) + @match_to_find.reload + expect(@match_to_find.teams).to match_array(@match.winner) + end + end + + context 'match below has one match_score with an unknown team' ,focus: true do + it 'finds the correct match and adds no match_score' do + @match_to_find.match_scores = create_list(:match_score, 1, team: create(:team)) + @match_to_find.save + PlayoffStageService.populate_match_below(@match) + @match_to_find.reload + expect(@match_to_find.teams).to match_array(@match.winner) + end + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9e55c9c..a34a97d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,8 +23,10 @@ RSpec.configure do |config| # assertion/expectation library such as wrong or the stdlib/minitest # assertions if you prefer. + # config.before(:suite) { FactoryBot.lint } + # only runs tests with " , focus: true " - # config.filter_run focus: true + config.filter_run focus: true config.expect_with :rspec do |expectations| # This option will default to `true` in RSpec 4. It makes the `description`