<commit_before>require 'rails_helper'

describe Budget do

  let(:budget) { create(:budget) }

  it_behaves_like "sluggable"

  describe "name" do
    before do
      create(:budget, name: 'object name')
    end

    it "is validated for uniqueness" do
      expect(build(:budget, name: 'object name')).not_to be_valid
    end
  end

  describe "description" do
    describe "Without Budget::Phase associated" do
      before do
        budget.phases.destroy_all
      end

      it "changes depending on the phase, falling back to budget description attributes" do
        Budget::Phase::PHASE_KINDS.each do |phase_kind|
          budget.phase = phase_kind
          expect(budget.description).to eq(budget.send("description_#{phase_kind}"))
          expect(budget.description).to be_html_safe
        end
      end
    end

    describe "With associated Budget::Phases" do
      before do
        budget.phases.each do |phase|
          phase.description = phase.kind.humanize
          phase.save
        end
      end

      it "changes depending on the phase" do
        Budget::Phase::PHASE_KINDS.each do |phase_kind|
          budget.phase = phase_kind
          expect(budget.description).to eq(phase_kind.humanize)
        end
      end
    end
  end

  describe "phase" do
    it "is validated" do
      Budget::Phase::PHASE_KINDS.each do |phase|
        budget.phase = phase
        expect(budget).to be_valid
      end

      budget.phase = 'inexisting'
      expect(budget).not_to be_valid
    end

    it "produces auxiliary methods" do
      budget.phase = "drafting"
      expect(budget).to be_drafting

      budget.phase = "accepting"
      expect(budget).to be_accepting

      budget.phase = "reviewing"
      expect(budget).to be_reviewing

      budget.phase = "selecting"
      expect(budget).to be_selecting

      budget.phase = "valuating"
      expect(budget).to be_valuating

      budget.phase = "publishing_prices"
      expect(budget).to be_publishing_prices

      budget.phase = "balloting"
      expect(budget).to be_balloting

      budget.phase = "reviewing_ballots"
      expect(budget).to be_reviewing_ballots

      budget.phase = "finished"
      expect(budget).to be_finished
    end

    it "balloting_or_later?" do
      budget.phase = "drafting"
      expect(budget).not_to be_balloting_or_later

      budget.phase = "accepting"
      expect(budget).not_to be_balloting_or_later

      budget.phase = "reviewing"
      expect(budget).not_to be_balloting_or_later

      budget.phase = "selecting"
      expect(budget).not_to be_balloting_or_later

      budget.phase = "valuating"
      expect(budget).not_to be_balloting_or_later

      budget.phase = "publishing_prices"
      expect(budget).not_to be_balloting_or_later

      budget.phase = "balloting"
      expect(budget).to be_balloting_or_later

      budget.phase = "reviewing_ballots"
      expect(budget).to be_balloting_or_later

      budget.phase = "finished"
      expect(budget).to be_balloting_or_later
    end
  end

  describe "#current" do

    it "returns nil if there is only one budget and it is still in drafting phase" do
      budget = create(:budget, phase: "drafting")

      expect(Budget.current).to eq(nil)
    end

    it "returns the budget if there is only one and not in drafting phase" do
      budget = create(:budget, phase: "accepting")

      expect(Budget.current).to eq(budget)
    end

    it "returns the last budget created that is not in drafting phase" do
      old_budget      = create(:budget, phase: "finished",  created_at: 2.years.ago)
      previous_budget = create(:budget, phase: "accepting", created_at: 1.year.ago)
      current_budget  = create(:budget, phase: "accepting", created_at: 1.month.ago)
      next_budget     = create(:budget, phase: "drafting",  created_at: 1.week.ago)

      expect(Budget.current).to eq(current_budget)
    end

  end

  describe "#open" do

    it "returns all budgets that are not in the finished phase" do
      phases = Budget::PHASES - ["finished"]
      phases.each do |phase|
        budget = create(:budget, phase: phase)
        expect(Budget.open).to include(budget)
      end
    end

  end

  describe "heading_price" do
    let(:group) { create(:budget_group, budget: budget) }

    it "returns the heading price if the heading provided is part of the budget" do
      heading = create(:budget_heading, price: 100, group: group)
      expect(budget.heading_price(heading)).to eq(100)
    end
<commit_msg>Fix conflicts with merged branch, Budget::PHASES have moved, and described_class usage is a must<commit_after>require 'rails_helper'

describe Budget do

  let(:budget) { create(:budget) }

  it_behaves_like "sluggable"

  describe "name" do
    before do
      create(:budget, name: 'object name')
    end

    it "is validated for uniqueness" do
      expect(build(:budget, name: 'object name')).not_to be_valid
    end
  end

  describe "description" do
    describe "Without Budget::Phase associated" do
      before do
        budget.phases.destroy_all
      end

      it "changes depending on the phase, falling back to budget description attributes" do
        Budget::Phase::PHASE_KINDS.each do |phase_kind|
          budget.phase = phase_kind
          expect(budget.description).to eq(budget.send("description_#{phase_kind}"))
          expect(budget.description).to be_html_safe
        end
      end
    end

    describe "With associated Budget::Phases" do
      before do
        budget.phases.each do |phase|
          phase.description = phase.kind.humanize
          phase.save
        end
      end

      it "changes depending on the phase" do
        Budget::Phase::PHASE_KINDS.each do |phase_kind|
          budget.phase = phase_kind
          expect(budget.description).to eq(phase_kind.humanize)
        end
      end
    end
  end

  describe "phase" do
    it "is validated" do
      Budget::Phase::PHASE_KINDS.each do |phase|
        budget.phase = phase
        expect(budget).to be_valid
      end

      budget.phase = 'inexisting'
      expect(budget).not_to be_valid
    end

    it "produces auxiliary methods" do
      budget.phase = "drafting"
      expect(budget).to be_drafting

      budget.phase = "accepting"
      expect(budget).to be_accepting

      budget.phase = "reviewing"
      expect(budget).to be_reviewing

      budget.phase = "selecting"
      expect(budget).to be_selecting

      budget.phase = "valuating"
      expect(budget).to be_valuating

      budget.phase = "publishing_prices"
      expect(budget).to be_publishing_prices

      budget.phase = "balloting"
      expect(budget).to be_balloting

      budget.phase = "reviewing_ballots"
      expect(budget).to be_reviewing_ballots

      budget.phase = "finished"
      expect(budget).to be_finished
    end

    it "balloting_or_later?" do
      budget.phase = "drafting"
      expect(budget).not_to be_balloting_or_later

      budget.phase = "accepting"
      expect(budget).not_to be_balloting_or_later

      budget.phase = "reviewing"
      expect(budget).not_to be_balloting_or_later

      budget.phase = "selecting"
      expect(budget).not_to be_balloting_or_later

      budget.phase = "valuating"
      expect(budget).not_to be_balloting_or_later

      budget.phase = "publishing_prices"
      expect(budget).not_to be_balloting_or_later

      budget.phase = "balloting"
      expect(budget).to be_balloting_or_later

      budget.phase = "reviewing_ballots"
      expect(budget).to be_balloting_or_later

      budget.phase = "finished"
      expect(budget).to be_balloting_or_later
    end
  end

  describe "#current" do

    it "returns nil if there is only one budget and it is still in drafting phase" do
      budget = create(:budget, phase: "drafting")

      expect(described_class.current).to eq(nil)
    end

    it "returns the budget if there is only one and not in drafting phase" do
      budget = create(:budget, phase: "accepting")

      expect(described_class.current).to eq(budget)
    end

    it "returns the last budget created that is not in drafting phase" do
      old_budget      = create(:budget, phase: "finished",  created_at: 2.years.ago)
      previous_budget = create(:budget, phase: "accepting", created_at: 1.year.ago)
      current_budget  = create(:budget, phase: "accepting", created_at: 1.month.ago)
      next_budget     = create(:budget, phase: "drafting",  created_at: 1.week.ago)

      expect(described_class.current).to eq(current_budget)
    end

  end

  describe "#open" do

    it "returns all budgets that are not in the finished phase" do
      (Budget::Phase::PHASE_KINDS - ["finished"]).each do |phase|
        budget = create(:budget, phase: phase)
        expect(described_class.open).to include(budget)
      end
    end

  end

  describe "heading_price" do
    let(:group) { create(:budget_group, budget: budget) }

    it "returns the heading price if the heading provided is part of the budget" do
      heading = create(:budget_heading, price: 100, group: group)
      expect(budget.heading_price(heading)).to eq(100)
    end

{'fraction_non_alphanumeric': 0.09027092245256114, 'fraction_numerical': 0.0021937040693210488, 'mean_word_length': 2.8007503126302624, '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': '2330413', 'n_tokens_mistral': 2945, 'n_tokens_neox': 2837, 'n_words': 723}