<commit_before>require 'rails_helper'

describe Leagues::MatchesController do
  before(:all) do
    @map = create(:map)
    @map2 = create(:map)

    @league = create(:league, matches_submittable: true, allow_round_draws: false)
    @div = create(:league_division, league: @league)
    @div2 = create(:league_division, league: @league)

    @team1 = create(:league_roster, division: @div)
    @team2 = create(:league_roster, division: @div)
    @team3 = create(:league_roster, division: @div2)

    @admin = create(:user)
    @admin.grant(:edit, @league)

    @captain1 = create(:user)
    @captain1.grant(:edit, @team1.team)

    @captain2 = create(:user)
    @captain2.grant(:edit, @team2.team)

    @user = create(:user)
  end

  describe 'GET #index' do
    it 'succeeds for authorized user' do
      sign_in @admin

      get :index, params: { league_id: @league.id }

      expect(response).to have_http_status(:success)
    end
  end

  describe 'GET #new' do
    it 'succeeds for authorized user' do
      sign_in @admin

      get :new, params: { league_id: @league.id }

      expect(response).to have_http_status(:success)
    end
  end

  describe 'POST #create' do
    it 'succeeds for authorized user' do
      sign_in @admin

      post :create, params: {
        league_id: @league.id, division_id: @div.id, match: {
          home_team_id: @team1.id, away_team_id: @team2.id, round_name: 'foo',
          round_number: 3, notice: 'B',
          rounds_attributes: [
            { map_id: @map.id }
          ]
        }
      }

      @league.reload
      match = @league.matches.first
      expect(match).to_not be nil
      expect(match.home_team).to eq(@team1)
      expect(match.away_team).to eq(@team2)
      expect(match.round_name).to eq('foo')
      expect(match.round_number).to eq(3)
      expect(match.notice).to eq('B')
      expect(match.rounds.count).to eq(1)
      round = match.rounds.first
      expect(round.map).to eq(@map)
      (@team1.users + @team2.users).each do |user|
        expect(user.notifications).to_not be_empty
      end
    end

    it 'fails with invalid data' do
      sign_in @admin

      post :create, params: {
        league_id: @league.id, division_id: @div.id, match: {
          home_team_id: @team1.id, away_team_id: @team1.id,
        }
      }

      expect(League::Match.all).to be_empty
      expect(response).to have_http_status(:success)
    end

    it 'redirects for unauthorized user' do
      sign_in @user

      post :create, params: { league_id: @league.id }

      expect(response).to redirect_to(league_path(@league))
    end
<commit_msg>Expanded Leagues::MatchesController tests to cover creation of bye matches<commit_after>require 'rails_helper'

describe Leagues::MatchesController do
  before(:all) do
    @map = create(:map)
    @map2 = create(:map)

    @league = create(:league, matches_submittable: true, allow_round_draws: false)
    @div = create(:league_division, league: @league)
    @div2 = create(:league_division, league: @league)

    @team1 = create(:league_roster, division: @div)
    @team2 = create(:league_roster, division: @div)
    @team3 = create(:league_roster, division: @div2)

    @admin = create(:user)
    @admin.grant(:edit, @league)

    @captain1 = create(:user)
    @captain1.grant(:edit, @team1.team)

    @captain2 = create(:user)
    @captain2.grant(:edit, @team2.team)

    @user = create(:user)
  end

  describe 'GET #index' do
    it 'succeeds for authorized user' do
      sign_in @admin

      get :index, params: { league_id: @league.id }

      expect(response).to have_http_status(:success)
    end
  end

  describe 'GET #new' do
    it 'succeeds for authorized user' do
      sign_in @admin

      get :new, params: { league_id: @league.id }

      expect(response).to have_http_status(:success)
    end
  end

  describe 'POST #create' do
    it 'succeeds for authorized user' do
      sign_in @admin

      post :create, params: {
        league_id: @league.id, division_id: @div.id, match: {
          home_team_id: @team1.id, away_team_id: @team2.id, round_name: 'foo',
          round_number: 3, notice: 'B',
          rounds_attributes: [
            { map_id: @map.id }
          ]
        }
      }

      @league.reload
      match = @league.matches.first
      expect(match).to_not be nil
      expect(match.home_team).to eq(@team1)
      expect(match.away_team).to eq(@team2)
      expect(match.round_name).to eq('foo')
      expect(match.round_number).to eq(3)
      expect(match.notice).to eq('B')
      expect(match.rounds.count).to eq(1)
      round = match.rounds.first
      expect(round.map).to eq(@map)
      (@team1.users + @team2.users).each do |user|
        expect(user.notifications).to_not be_empty
      end
    end

    it 'succeeds for authorized user with bye match' do
      sign_in @admin

      post :create, params: {
        league_id: @league.id, division_id: @div.id, match: {
          home_team_id: @team1.id, away_team_id: nil, round_name: 'foo',
          round_number: 3, notice: 'B',
        }
      }

      @league.reload
      match = @league.matches.first
      expect(match).to_not be nil
      expect(match.bye?).to be true
      expect(match.home_team).to eq(@team1)
      expect(match.away_team).to be nil
      expect(match.round_name).to eq('foo')
      expect(match.round_number).to eq(3)
      expect(match.notice).to eq('B')
      expect(match.rounds).to be_empty
      @team1.users.each do |user|
        expect(user.notifications).to_not be_empty
      end
      @team2.users.each do |user|
        expect(user.notifications).to be_empty
      end
    end

    it 'fails with invalid data' do
      sign_in @admin

      post :create, params: {
        league_id: @league.id, division_id: @div.id, match: {
          home_team_id: @team1.id, away_team_id: @team1.id,
        }
      }

      expect(League::Match.all).to be_empty
      expect(response).to have_http_status(:success)
    end

    it 'redirects for unauthorized user' do
      sign_in @user

      post :create, params: { league_id: @league.id }

      expect(response).to redirect_to(league_path(@league))
    end

{'fraction_non_alphanumeric': 0.1255678131083712, 'fraction_numerical': 0.008436080467229072, 'mean_word_length': 2.696043165467626, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '366797', 'n_tokens_mistral': 2174, 'n_tokens_neox': 2043, 'n_words': 468}