<commit_before>require 'rails_helper'

RSpec.describe ArchivedPetition, type: :model do
  subject(:petition){ described_class.new }

  describe ".search" do
    let!(:petition_1) do
      FactoryGirl.create(:archived_petition, :closed, title: "Wombles are great", created_at: 1.year.ago)
    end

    let!(:petition_2) do
      FactoryGirl.create(:archived_petition, :closed, description: "The Wombles of Wimbledon", created_at: 2.years.ago)
    end

    it "searches based upon title" do
      expect(ArchivedPetition.search(q: "Wombles")).to include(petition_1)
    end

    it "searches based upon description" do
      expect(ArchivedPetition.search(q: "Wombles")).to include(petition_2)
    end

    it "sorts the results by the created_at timestamp" do
      expect(ArchivedPetition.search(q: "Petition").to_a).to eq([petition_2, petition_1])
    end
  end

  describe "#title" do
    it "defaults to nil" do
      expect(petition.title).to be_nil
    end

    it { is_expected.to validate_presence_of(:title) }
    it { is_expected.to validate_length_of(:title).is_at_most(150) }
  end

  describe "#description" do
    it "defaults to nil" do
      expect(petition.description).to be_nil
    end

    it { is_expected.to validate_presence_of(:description) }
    it { is_expected.to validate_length_of(:description).is_at_most(1000) }
  end

  describe "#response" do
    it "defaults to nil" do
      expect(petition.response).to be_nil
    end
  end

  describe "#state" do
    it "defaults to 'open'" do
      expect(petition.state).to eq("open")
    end

    it { is_expected.to validate_presence_of(:state) }
    it { is_expected.to validate_inclusion_of(:state).in_array(%w[open rejected]) }
  end

  describe "#reason_for_rejection" do
    it "defaults to nil" do
      expect(petition.reason_for_rejection).to be_nil
    end
  end

  describe "#opened_at" do
    it "defaults to nil" do
      expect(petition.opened_at).to be_nil
    end
  end

  describe "#closed_at" do
    it "defaults to nil" do
      expect(petition.closed_at).to be_nil
    end
  end

  describe "#signature_count" do
    it "defaults to zero" do
      expect(petition.signature_count).to be_zero
    end
  end

  describe "#open?" do
    context "when petition is in an open state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :open) }

      it "returns true" do
        expect(petition.open?).to eq(true)
      end
    end

    context "when petition is in a closed state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :closed) }

      it "returns false" do
        expect(petition.open?).to eq(false)
      end
    end

    context "when petition is in a rejected state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :rejected) }

      it "returns false" do
        expect(petition.open?).to eq(false)
      end
    end
  end

  describe "#closed?" do
    context "when petition is in an open state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :open) }

      it "returns false" do
        expect(petition.closed?).to eq(false)
      end
    end

    context "when petition is in a closed state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :closed) }

      it "returns true" do
        expect(petition.closed?).to eq(true)
      end
    end

    context "when petition is in a rejected state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :rejected) }

      it "returns false" do
        expect(petition.closed?).to eq(false)
      end
    end
  end

  describe "#rejected?" do
    context "when petition is in an open state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :open) }

      it "returns false" do
        expect(petition.rejected?).to eq(false)
      end
    end

    context "when petition is in a closed state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :closed) }

      it "returns false" do
        expect(petition.rejected?).to eq(false)
      end
    end

    context "when petition is in a rejected state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :rejected) }

      it "returns true" do
        expect(petition.rejected?).to eq(true)
      end
    end
  end

  describe "#by_most_signatures" do
    before do
      @p1 = FactoryGirl.create(:archived_petition, signature_count: 100)
      @p2 = FactoryGirl.create(:archived_petition, signature_count: 10)
      @p3 = FactoryGirl.create(:archived_petition, signature_count: 50)
    end

    it 'returns archived petitions ordered by highest number of signatures' do
      expect(ArchivedPetition.by_most_signatures).to match_array([@p1, @p3, @p2])
    end
  end
end<commit_msg>Fix archived petition model spec<commit_after>require 'rails_helper'

RSpec.describe ArchivedPetition, type: :model do
  subject(:petition){ described_class.new }

  describe ".search" do
    let!(:petition_1) do
      FactoryGirl.create(:archived_petition, :closed, title: "Wombles are great", created_at: 1.year.ago)
    end

    let!(:petition_2) do
      FactoryGirl.create(:archived_petition, :closed, description: "The Wombles of Wimbledon", created_at: 2.years.ago)
    end

    it "searches based upon title" do
      expect(ArchivedPetition.search(q: "Wombles")).to include(petition_1)
    end

    it "searches based upon description" do
      expect(ArchivedPetition.search(q: "Wombles")).to include(petition_2)
    end

    it "sorts the results by the created_at timestamp" do
      expect(ArchivedPetition.search(q: "Petition").to_a).to eq([petition_2, petition_1])
    end
  end

  describe ".by_most_signatures" do
    let!(:petition_1) { FactoryGirl.create(:archived_petition, signature_count: 100) }
    let!(:petition_2) { FactoryGirl.create(:archived_petition, signature_count: 10) }
    let!(:petition_3) { FactoryGirl.create(:archived_petition, signature_count: 50) }
    let(:petitions) { [petition_1, petition_3, petition_2] }

    it 'returns archived petitions ordered by highest number of signatures' do
      expect(ArchivedPetition.by_most_signatures).to eq(petitions)
    end
  end

  describe "#title" do
    it "defaults to nil" do
      expect(petition.title).to be_nil
    end

    it { is_expected.to validate_presence_of(:title) }
    it { is_expected.to validate_length_of(:title).is_at_most(150) }
  end

  describe "#description" do
    it "defaults to nil" do
      expect(petition.description).to be_nil
    end

    it { is_expected.to validate_presence_of(:description) }
    it { is_expected.to validate_length_of(:description).is_at_most(1000) }
  end

  describe "#response" do
    it "defaults to nil" do
      expect(petition.response).to be_nil
    end
  end

  describe "#state" do
    it "defaults to 'open'" do
      expect(petition.state).to eq("open")
    end

    it { is_expected.to validate_presence_of(:state) }
    it { is_expected.to validate_inclusion_of(:state).in_array(%w[open rejected]) }
  end

  describe "#reason_for_rejection" do
    it "defaults to nil" do
      expect(petition.reason_for_rejection).to be_nil
    end
  end

  describe "#opened_at" do
    it "defaults to nil" do
      expect(petition.opened_at).to be_nil
    end
  end

  describe "#closed_at" do
    it "defaults to nil" do
      expect(petition.closed_at).to be_nil
    end
  end

  describe "#signature_count" do
    it "defaults to zero" do
      expect(petition.signature_count).to be_zero
    end
  end

  describe "#open?" do
    context "when petition is in an open state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :open) }

      it "returns true" do
        expect(petition.open?).to eq(true)
      end
    end

    context "when petition is in a closed state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :closed) }

      it "returns false" do
        expect(petition.open?).to eq(false)
      end
    end

    context "when petition is in a rejected state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :rejected) }

      it "returns false" do
        expect(petition.open?).to eq(false)
      end
    end
  end

  describe "#closed?" do
    context "when petition is in an open state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :open) }

      it "returns false" do
        expect(petition.closed?).to eq(false)
      end
    end

    context "when petition is in a closed state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :closed) }

      it "returns true" do
        expect(petition.closed?).to eq(true)
      end
    end

    context "when petition is in a rejected state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :rejected) }

      it "returns false" do
        expect(petition.closed?).to eq(false)
      end
    end
  end

  describe "#rejected?" do
    context "when petition is in an open state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :open) }

      it "returns false" do
        expect(petition.rejected?).to eq(false)
      end
    end

    context "when petition is in a closed state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :closed) }

      it "returns false" do
        expect(petition.rejected?).to eq(false)
      end
    end

    context "when petition is in a rejected state" do
      subject(:petition) { FactoryGirl.build(:archived_petition, :rejected) }

      it "returns true" do
        expect(petition.rejected?).to eq(true)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10048994058167414, 'fraction_numerical': 0.005837589909308871, 'mean_word_length': 3.1282271944922546, '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': '2872651', 'n_tokens_mistral': 3169, 'n_tokens_neox': 2900, 'n_words': 828}