<commit_before>require 'rails_helper'

describe HarvestsController do
  login_member

  def valid_attributes
    {
      owner_id:      subject.current_member.id,
      crop_id:       FactoryBot.create(:crop).id,
      plant_part_id: FactoryBot.create(:plant_part).id,
      harvested_at:  '2017-01-01'
    }
  end

  describe "GET index" do
    let(:member1) { FactoryBot.create(:member) }
    let(:member2) { FactoryBot.create(:member) }
    let(:tomato) { FactoryBot.create(:tomato) }
    let(:maize) { FactoryBot.create(:maize) }
    let(:harvest1) { FactoryBot.create(:harvest, owner_id: member1.id, crop_id: tomato.id) }
    let(:harvest2) { FactoryBot.create(:harvest, owner_id: member2.id, crop_id: maize.id) }

    describe "assigns all harvests as @harvests" do
      before { get :index, params: {} }
      it { assigns(:harvests).should =~ [harvest1, harvest2] }
    end

    describe "picks up owner from params and shows owner's harvests only" do
      before { get :index, params: { owner: member1.slug } }
      it { expect(assigns(:owner)).to eq member1 }
      it { expect(assigns(:harvests)).to eq [harvest1] }
    end

    describe "picks up crop from params and shows the harvests for the crop only" do
      before { get :index, params: { crop: maize.name } }
      it { expect(assigns(:crop)).to eq maize }
      it { expect(assigns(:harvests)).to eq [harvest2] }
    end

    describe "generates a csv" do
      before { get :index, format: "csv" }

      it { expect(response.status).to eq 200 }
    end
  end

  describe "GET show" do
    let(:harvest) { Harvest.create! valid_attributes }

    describe "assigns the requested harvest as @harvest" do
      before { get :show, params: { id: harvest.to_param } }
      it { expect(assigns(:harvest)).to eq(harvest) }
    end
  end

  describe "GET new" do
    before { get :new, params: {} }

    describe "assigns a new harvest as @harvest" do
      it { expect(assigns(:harvest)).to be_a_new(Harvest) }
    end

    describe "sets the date of the harvest to today" do
      it { expect(assigns(:harvest).harvested_at).to eq(Time.zone.today) }
    end
  end

  describe "GET edit" do
    let(:harvest) { Harvest.create! valid_attributes }

    describe "assigns the requested harvest as @harvest" do
      before { get :edit, params: { id: harvest.to_param } }
      it { expect(assigns(:harvest)).to eq(harvest) }
    end
  end

  describe "POST create" do
    describe "with valid params" do
      it "creates a new Harvest" do
        expect do
          post :create, params: { harvest: valid_attributes }
        end.to change(Harvest, :count).by(1)
      end

      it "assigns a newly created harvest as @harvest" do
        post :create, params: { harvest: valid_attributes }
        assigns(:harvest).should be_a(Harvest)
        assigns(:harvest).should be_persisted
      end

      it "redirects to the created harvest" do
        post :create, params: { harvest: valid_attributes }
        response.should redirect_to(Harvest.last)
      end

      describe "links to planting" do
        let(:planting) { FactoryBot.create(:planting, owner_id: member.id, garden: member.gardens.first) }
        before { post :create, params: { harvest: valid_attributes.merge(planting_id: planting.id) } }
        it { expect(Harvest.last.planting.id).to eq(planting.id) }
      end
    end

    describe "with invalid params" do
      it "assigns a newly created but unsaved harvest as @harvest" do
        # Trigger the behavior that occurs when invalid params are submitted
        Harvest.any_instance.stub(:save).and_return(false)
        post :create, params: { harvest: { "crop_id" => "invalid value" } }
        assigns(:harvest).should be_a_new(Harvest)
      end

      it "re-renders the 'new' template" do
        # Trigger the behavior that occurs when invalid params are submitted
        post :create, params: { harvest: { "crop_id" => "invalid value" } }
        response.should render_template("new")
      end
    end

    describe "not my planting" do
      let(:not_my_planting) { FactoryBot.create(:planting) }
      let(:harvest) { FactoryBot.create(:harvest) }

      describe "does not save planting_id" do
        before do
          allow(Harvest).to receive(:new).and_return(harvest)
          post :create, params: { harvest: valid_attributes.merge(planting_id: not_my_planting.id) }
        end

        it { expect(harvest.planting_id).not_to eq(not_my_planting.id) }
      end
    end
  end

  describe "PUT update" do
    describe "with valid params" do
      it "updates the requested harvest" do
        harvest = FactoryBot.create :harvest, valid_attributes
        new_crop = FactoryBot.create :crop
        expect do
          put :update, params: { id: harvest.to_param, harvest: { crop_id: new_crop.id } }
          harvest.reload
        end.to change(harvest, :crop_id).to(new_crop.id)
      end

      it "assigns the requested harvest as @harvest" do
        harvest = Harvest.create! valid_attributes
        put :update, params: { id: harvest.to_param, harvest: valid_attributes }
        assigns(:harvest).should eq(harvest)
      end

      it "redirects to the harvest" do
        harvest = Harvest.create! valid_attributes
        put :update, params: { id: harvest.to_param, harvest: valid_attributes }
        response.should redirect_to(harvest)
      end
    end

    describe "with invalid params" do
      it "assigns the harvest as @harvest" do
        harvest = Harvest.create! valid_attributes
        # Trigger the behavior that occurs when invalid params are submitted
        Harvest.any_instance.stub(:save).and_return(false)
        put :update, params: { id: harvest.to_param, harvest: { "crop_id" => "invalid value" } }
        assigns(:harvest).should eq(harvest)
      end

      it "re-renders the 'edit' template" do
        harvest = Harvest.create! valid_attributes
        put :update, params: { id: harvest.to_param, harvest: { "crop_id" => "invalid value" } }
        response.should render_template("edit")
      end
    end

    describe "not my planting" do
      let(:not_my_planting) { FactoryBot.create(:planting) }
      let(:harvest) { FactoryBot.create(:harvest) }

      describe "does not save planting_id" do
        before do
          put :update, params: { id:      harvest.to_param,
                                 harvest: valid_attributes.merge(planting_id: not_my_planting.id) }
        end

        it { expect(harvest.planting_id).to eq(nil) }
      end
    end
  end

  describe "DELETE destroy" do
    it "destroys the requested harvest" do
      harvest = Harvest.create! valid_attributes
      expect do
        delete :destroy, params: { id: harvest.to_param }
      end.to change(Harvest, :count).by(-1)
    end

    it "redirects to the harvests list" do
      harvest = Harvest.create! valid_attributes
      delete :destroy, params: { id: harvest.to_param }
      response.should redirect_to(harvests_url)
    end
  end
end
<commit_msg>Update harvests controller spec for new routes<commit_after>require 'rails_helper'

describe HarvestsController do
  login_member

  def valid_attributes
    {
      owner_id:      subject.current_member.id,
      crop_id:       FactoryBot.create(:crop).id,
      plant_part_id: FactoryBot.create(:plant_part).id,
      harvested_at:  '2017-01-01'
    }
  end

  describe "GET index" do
    let!(:member1) { FactoryBot.create(:member) }
    let(:member2) { FactoryBot.create(:member) }
    let(:tomato) { FactoryBot.create(:tomato) }
    let(:maize) { FactoryBot.create(:maize) }
    let!(:harvest1) { FactoryBot.create(:harvest, owner_id: member1.id, crop_id: tomato.id) }
    let!(:harvest2) { FactoryBot.create(:harvest, owner_id: member2.id, crop_id: maize.id) }

    describe "assigns all harvests as @harvests" do
      before { get :index, params: {} }
      it { expect(assigns(:harvests)).to eq [harvest1, harvest2] }
    end

    describe "picks up owner from params and shows owner's harvests only" do
      before { get :index, params: { member_slug: member1.slug } }
      it { expect(assigns(:owner)).to eq member1 }
      it { expect(assigns(:harvests)).to eq [harvest1] }
    end

    describe "picks up crop from params and shows the harvests for the crop only" do
      before { get :index, params: { crop_slug: maize.name } }
      it { expect(assigns(:crop)).to eq maize }
      it { expect(assigns(:harvests)).to eq [harvest2] }
    end

    describe "generates a csv" do
      before { get :index, format: "csv" }

      it { expect(response.status).to eq 200 }
    end
  end

  describe "GET show" do
    let(:harvest) { Harvest.create! valid_attributes }

    describe "assigns the requested harvest as @harvest" do
      before { get :show, params: { id: harvest.to_param } }
      it { expect(assigns(:harvest)).to eq(harvest) }
    end
  end

  describe "GET new" do
    before { get :new, params: {} }

    describe "assigns a new harvest as @harvest" do
      it { expect(assigns(:harvest)).to be_a_new(Harvest) }
    end

    describe "sets the date of the harvest to today" do
      it { expect(assigns(:harvest).harvested_at).to eq(Time.zone.today) }
    end
  end

  describe "GET edit" do
    let(:harvest) { Harvest.create! valid_attributes }

    describe "assigns the requested harvest as @harvest" do
      before { get :edit, params: { id: harvest.to_param } }
      it { expect(assigns(:harvest)).to eq(harvest) }
    end
  end

  describe "POST create" do
    describe "with valid params" do
      it "creates a new Harvest" do
        expect do
          post :create, params: { harvest: valid_attributes }
        end.to change(Harvest, :count).by(1)
      end

      describe "assigns a newly created harvest as @harvest" do
        before { post :create, params: { harvest: valid_attributes } }
        it { expect(assigns(:harvest)).to be_a(Harvest) }
        it { expect(assigns(:harvest)).to be_persisted }
      end

      describe "redirects to the created harvest" do
        before { post :create, params: { harvest: valid_attributes } }
        it { expect(response).to redirect_to(Harvest.last) }
      end

      describe "links to planting" do
        let(:planting) { FactoryBot.create(:planting, owner_id: member.id, garden: member.gardens.first) }
        before { post :create, params: { harvest: valid_attributes.merge(planting_id: planting.id) } }
        it { expect(Harvest.last.planting.id).to eq(planting.id) }
      end
    end

    describe "with invalid params" do
      it "assigns a newly created but unsaved harvest as @harvest" do
        # Trigger the behavior that occurs when invalid params are submitted
        Harvest.any_instance.stub(:save).and_return(false)
        post :create, params: { harvest: { "crop_id" => "invalid value" } }
        expect(assigns(:harvest)).to be_a_new(Harvest)
      end

      describe "re-renders the 'new' template" do
        # Trigger the behavior that occurs when invalid params are submitted
        before { post :create, params: { harvest: { "crop_id" => "invalid value" } } }
        it { expect(response).to render_template("new") }
      end
    end

    describe "not my planting" do
      let(:not_my_planting) { FactoryBot.create(:planting) }
      let(:harvest) { FactoryBot.create(:harvest) }

      describe "does not save planting_id" do
        before do
          allow(Harvest).to receive(:new).and_return(harvest)
          post :create, params: { harvest: valid_attributes.merge(planting_id: not_my_planting.id) }
        end

        it { expect(harvest.planting_id).not_to eq(not_my_planting.id) }
      end
    end
  end

  describe "PUT update" do
    describe "with valid params" do
      let(:harvest) { Harvest.create! valid_attributes }
      it "updates the requested harvest" do
        new_crop = FactoryBot.create :crop
        expect do
          put :update, params: { id: harvest.to_param, harvest: { crop_id: new_crop.id } }
          harvest.reload
        end.to change(harvest, :crop_id).to(new_crop.id)
      end

      describe "assigns the requested harvest as @harvest" do
        before { put :update, params: { id: harvest.to_param, harvest: valid_attributes } }
        it { expect(assigns(:harvest)).to eq(harvest) }
      end

      describe "redirects to the harvest" do
        before { put :update, params: { id: harvest.to_param, harvest: valid_attributes } }
        it { expect(response).to redirect_to(harvest) }
      end
    end

    describe "with invalid params" do
      it "assigns the harvest as @harvest" do
        harvest = Harvest.create! valid_attributes
        # Trigger the behavior that occurs when invalid params are submitted
        Harvest.any_instance.stub(:save).and_return(false)
        put :update, params: { id: harvest.to_param, harvest: { "crop_id" => "invalid value" } }
        expect(assigns(:harvest)).to eq(harvest)
      end

      it "re-renders the 'edit' template" do
        harvest = Harvest.create! valid_attributes
        put :update, params: { id: harvest.to_param, harvest: { "crop_id" => "invalid value" } }
        expect(response).to render_template("edit")
      end
    end

    describe "not my planting" do
      let(:not_my_planting) { FactoryBot.create(:planting) }
      let(:harvest) { FactoryBot.create(:harvest) }

      describe "does not save planting_id" do
        before do
          put :update, params: { id:      harvest.to_param,
                                 harvest: valid_attributes.merge(planting_id: not_my_planting.id) }
        end

        it { expect(harvest.planting_id).to eq(nil) }
      end
    end
  end

  describe "DELETE destroy" do
    it "destroys the requested harvest" do
      harvest = Harvest.create! valid_attributes
      expect do
        delete :destroy, params: { id: harvest.to_param }
      end.to change(Harvest, :count).by(-1)
    end

    it "redirects to the harvests list" do
      harvest = Harvest.create! valid_attributes
      delete :destroy, params: { id: harvest.to_param }
      expect(response).to redirect_to(harvests_url)
    end
  end
end

{'fraction_non_alphanumeric': 0.10495049504950495, 'fraction_numerical': 0.003536067892503536, 'mean_word_length': 2.754912373871482, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4143177', 'n_tokens_mistral': 4426, 'n_tokens_neox': 4330, 'n_words': 1231}