<commit_before># frozen_string_literal: true

require 'rails_helper'

RSpec.describe CasesController, type: :controller do
  # Stubbing out make_undo_link for all specs
  before do
    allow_any_instance_of(CasesController).to receive(
      :make_undo_link
    ).and_return('/cases/1')
  end

  describe '#index' do
    describe 'on success' do
      let(:cases) { FactoryBot.create_list(:case, 20) }
      before(:each) { get :index }
      it 'assigns the first 12 cases to @cases' do
        expect(assigns(:cases)).to match_array cases[0..11]
      end
      it 'successfully returns the cases#index page' do
        expect(response).to be_success
      end
    end
    describe 'on failure' do
      before do
        def controller.index
          raise ActionController::InvalidAuthenticityToken
        end
      end
      it 'does not raise an error' do
        expect { get :index }.not_to raise_error
      end
      it 'redirects to the home page' do
        expect(get(:index)).to redirect_to('/')
      end
    end
  end
end

RSpec.describe CasesController, type: :controller do
  # Stubbing out make_undo_link for all specs
  before do
    allow_any_instance_of(CasesController).to receive(
      :make_undo_link
    ).and_return('/cases/1')
  end
  let(:cases) { FactoryBot.create_list(:case, 20) }

  describe '#show' do
    context 'when requested case exists' do
      let(:this_case) { cases.first }
      before(:each) { get :show, id: this_case.id }
      it 'success' do
        expect(response).to be_success
      end
      it 'assigns it to @case' do
        expect(assigns(:this_case)).to eq this_case
      end
    end
    context 'when requested case does not exists' do
      it 'throws ActiveRecord::RecordNotFound' do
        expect { get :show, id: -1 }.to raise_exception ActiveRecord::RecordNotFound
      end
    end
  end
end

RSpec.describe CasesController, type: :controller do
  # Stubbing out make_undo_link for all specs
  before do
    allow_any_instance_of(CasesController).to receive(
      :make_undo_link
    ).and_return('/cases/1')
  end
  let(:cases) { FactoryBot.create_list(:case, 20) }
  describe '#create' do
    login_user
    context 'when valid' do
      let(:case_attrs) { FactoryBot.attributes_for(:case, state_id: 33) }
      let(:subject_attrs) { FactoryBot.attributes_for(:subject) }
      it 'success' do
        allow_any_instance_of(Case).to receive(:full_address).and_return('Albany NY')
        case_attrs['subjects_attributes'] = { '0' => subject_attrs }
        post :create, 'case': case_attrs
        expect(response).to redirect_to(case_path(Case.last))
      end
      it 'saves and assigns new case to @case' do
        allow_any_instance_of(Case).to receive(:full_address).and_return('Albany NY')
        case_attrs['subjects_attributes'] = { '0' => subject_attrs }
        post :create, 'case': case_attrs
        expect(assigns(:this_case)).to be_a_kind_of(Case)
        expect(assigns(:this_case)).to be_persisted
      end
    end

    context 'when invalid' do
      let(:case_attrs) { attributes_for(:invalid_case) }
      it 'fails' do
        allow_any_instance_of(Case).to receive(:full_address).and_return(' Albany NY ')
        post :create, 'case': case_attrs
        expect(response).to render_template(:new)
      end
    end
  end
end

RSpec.describe CasesController, type: :controller do
  # Stubbing out make_undo_link for all specs
  before do
    allow_any_instance_of(CasesController).to receive(<commit_msg>Added expectations checking for state existence<commit_after># frozen_string_literal: true

require 'rails_helper'

RSpec.describe CasesController, type: :controller do
  # Stubbing out make_undo_link for all specs
  before do
    allow_any_instance_of(CasesController).to receive(
      :make_undo_link
    ).and_return('/cases/1')
  end

  describe '#index' do
    describe 'on success' do
      let(:cases) { FactoryBot.create_list(:case, 20) }
      before(:each) { get :index }
      it 'assigns the first 12 cases to @cases' do
        expect(assigns(:cases)).to match_array cases[0..11]
      end
      it 'successfully returns the cases#index page' do
        expect(response).to be_success
      end
    end
    describe 'on failure' do
      before do
        def controller.index
          raise ActionController::InvalidAuthenticityToken
        end
      end
      it 'does not raise an error' do
        expect { get :index }.not_to raise_error
      end
      it 'redirects to the home page' do
        expect(get(:index)).to redirect_to('/')
      end
    end
  end
end

RSpec.describe CasesController, type: :controller do
  # Stubbing out make_undo_link for all specs
  before do
    allow_any_instance_of(CasesController).to receive(
      :make_undo_link
    ).and_return('/cases/1')
  end
  let(:cases) { FactoryBot.create_list(:case, 20) }

  describe '#show' do
    context 'when requested case exists' do
      let(:this_case) { cases.first }
      before(:each) { get :show, id: this_case.id }
      it 'success' do
        expect(response).to be_success
      end
      it 'assigns it to @case' do
        expect(assigns(:this_case)).to eq this_case
      end
    end
    context 'when requested case does not exists' do
      it 'throws ActiveRecord::RecordNotFound' do
        expect { get :show, id: -1 }.to raise_exception ActiveRecord::RecordNotFound
      end
    end
  end
end

RSpec.describe CasesController, type: :controller do
  # Stubbing out make_undo_link for all specs
  before do
    allow_any_instance_of(CasesController).to receive(
      :make_undo_link
    ).and_return('/cases/1')
  end
  let(:cases) { FactoryBot.create_list(:case, 20) }
  
  describe '#create' do
    login_user
    context 'when valid' do
      let(:case_attrs) { FactoryBot.attributes_for(:case, state_id: 33) }
      let(:subject_attrs) { FactoryBot.attributes_for(:subject) }
      it 'saves the new case & redirects to show the case created' do
        allow_any_instance_of(Case).to receive(:full_address).and_return('Albany NY')
        case_attrs['subjects_attributes'] = { '0' => subject_attrs }
        post :create, 'case': case_attrs
        expect(response).to redirect_to(case_path(Case.last))
      end
      it 'saves and assigns new case to @case' do
        allow_any_instance_of(Case).to receive(:full_address).and_return('Albany NY')
        case_attrs['subjects_attributes'] = { '0' => subject_attrs }
        post :create, 'case': case_attrs
        expect(assigns(:this_case)).to be_a_kind_of(Case)
        expect(assigns(:this_case)).to be_persisted
      end
    end

    context 'when invalid' do
      let(:case_attrs) { attributes_for(:invalid_case) }
      it 'fails' do
        allow_any_instance_of(Case).to receive(:full_address).and_return(' Albany NY ')
        post :create, 'case': case_attrs
        expect(assigns(:categories)).to match_array([])
        expect(assigns(:states)).to match_array([])
        expect(response).to render_template(:new)
      end
    end
  end
end

RSpec.describe CasesController, type: :controller do
  # Stubbing out make_undo_link for all specs
  before do
    allow_any_instance_of(CasesController).to receive(
{'fraction_non_alphanumeric': 0.09818080822108041, 'fraction_numerical': 0.005277044854881266, 'mean_word_length': 3.0666290231507625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4320502', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2192, 'n_words': 597}