diff --git a/app/services/match_service.rb b/app/services/match_service.rb index adf2995..9d3cb50 100644 --- a/app/services/match_service.rb +++ b/app/services/match_service.rb @@ -45,12 +45,14 @@ class MatchService end # the start point is to compensate for all the teams that are already within a "normal" match - startpoint = matches.size + i = team_offset = matches.size until matches.size >= needed_games # while we do not have enough matches in general we need to fill the array with "single team" matches - i = matches.size + startpoint - match = Match.new state: :single_team, position: i, match_scores: [MatchScore.create(team: teams[i])] + match = Match.new state: :single_team, + position: i, + match_scores: [MatchScore.create(team: teams[i + team_offset])] matches << match + i += 1 end matches end diff --git a/spec/services/match_service_spec.rb b/spec/services/match_service_spec.rb index 21ada52..726fc55 100644 --- a/spec/services/match_service_spec.rb +++ b/spec/services/match_service_spec.rb @@ -117,5 +117,16 @@ RSpec.describe MatchService do it 'raises an exception for for 0 teams' do expect { MatchService.generate_matches([]) }. to raise_error 'Cannot generate Matches without teams' end + + it 'generates matches with consecutive positions' do + MatchService.generate_matches(create_list(:team, 7)).sort_by(&:position).each_with_index do |match, i| + expect(match.position).to eq(i) + end + end + + it 'places all given teams into the matches exactly once' do + teams = create_list(:team, 11) + expect(MatchService.generate_matches(teams).map(&:teams).flatten).to match_array(teams) + end end end