<commit_before>require "rails_helper"

describe "Ballots" do

  let(:user)        { create(:user, :level_two) }
  let!(:budget)     { create(:budget, :balloting) }
  let!(:states)     { create(:budget_group, budget: budget, name: "States") }
  let!(:california) { create(:budget_heading, group: states, name: "California", price: 1000) }
  let!(:new_york)   { create(:budget_heading, group: states, name: "New York", price: 1000000) }

  context "Load" do
    let(:user) do
      create(:user, :level_two, ballot_lines: [create(:budget_investment, :selected, heading: california)])
    end

    before do
      budget.update(slug: "budget_slug")
      login_as(user)
    end

    scenario "finds ballot using budget slug" do
      visit budget_ballot_path("budget_slug")

      expect(page).to have_content("You have voted one investment")
    end

    scenario "raises an error if budget slug is not found" do
      expect do
        visit budget_ballot_path("wrong_budget")
      end.to raise_error ActiveRecord::RecordNotFound
    end

    scenario "raises an error if budget id is not found" do
      expect do
        visit budget_ballot_path(0)
      end.to raise_error ActiveRecord::RecordNotFound
    end

  end

  context "Lines Load" do
    before do
      create(:budget_investment, :selected, heading: california, title: "More rain")
      budget.update(slug: "budget_slug")
      login_as(user)
    end

    scenario "finds ballot lines using budget slug", :js do
      visit budget_investments_path("budget_slug", states, california)
      add_to_ballot("More rain")

      within("#sidebar") { expect(page).to have_content "More rain" }
    end

  end

  context "Voting" do

    before do
      login_as(user)
      visit budget_path(budget)
    end

    let!(:city) { create(:budget_group, budget: budget, name: "City") }
    let!(:districts) { create(:budget_group, budget: budget, name: "Districts") }

    context "Group and Heading Navigation" do

      scenario "Groups" do
        visit budget_path(budget)

        expect(page).to have_link "City"
        expect(page).to have_link "Districts"
      end

      scenario "Headings" do
        create(:budget_heading, group: city,      name: "Investments Type1")
        create(:budget_heading, group: city,      name: "Investments Type2")
        create(:budget_heading, group: districts, name: "District 1")
        create(:budget_heading, group: districts, name: "District 2")

        visit budget_path(budget)
        click_link "City"

        expect(page).to have_link "Investments Type1"
        expect(page).to have_link "Investments Type2"

        visit budget_path(budget)
        click_link "Districts"

        expect(page).to have_link "District 1"
        expect(page).to have_link "District 2"

      end

      scenario "Investments" do
        city_heading1     = create(:budget_heading, group: city,      name: "Above the city")
        city_heading2     = create(:budget_heading, group: city,      name: "Under the city")
        district_heading1 = create(:budget_heading, group: districts, name: "District 1")
        district_heading2 = create(:budget_heading, group: districts, name: "District 2")

        create(:budget_investment, :selected, heading: city_heading1, title: "Solar panels")
        create(:budget_investment, :selected, heading: city_heading1, title: "Observatory")
        create(:budget_investment, :selected, heading: district_heading1, title: "New park")
        create(:budget_investment, :selected, heading: district_heading1, title: "Zero-emission zone")
        create(:budget_investment, :selected, heading: district_heading2, title: "Climbing wall")

        visit budget_path(budget)
        click_link "City"
        click_link "Above the city"

        expect(page).to have_css(".budget-investment", count: 2)
        expect(page).to have_content "Solar panels"
        expect(page).to have_content "Observatory"

        visit budget_path(budget)

        click_link "Districts"
        click_link "District 1"

        expect(page).to have_css(".budget-investment", count: 2)
        expect(page).to have_content "New park"
        expect(page).to have_content "Zero-emission zone"

        visit budget_path(budget)
        click_link "Districts"
        click_link "District 2"
<commit_msg>Use factory bot blocks to create complex data<commit_after>require "rails_helper"

describe "Ballots" do

  let(:user)        { create(:user, :level_two) }
  let!(:budget)     { create(:budget, :balloting) }
  let!(:states)     { create(:budget_group, budget: budget, name: "States") }
  let!(:california) { create(:budget_heading, group: states, name: "California", price: 1000) }
  let!(:new_york)   { create(:budget_heading, group: states, name: "New York", price: 1000000) }

  context "Load" do
    let(:user) do
      create(:user, :level_two, ballot_lines: [create(:budget_investment, :selected, heading: california)])
    end

    before do
      budget.update(slug: "budget_slug")
      login_as(user)
    end

    scenario "finds ballot using budget slug" do
      visit budget_ballot_path("budget_slug")

      expect(page).to have_content("You have voted one investment")
    end

    scenario "raises an error if budget slug is not found" do
      expect do
        visit budget_ballot_path("wrong_budget")
      end.to raise_error ActiveRecord::RecordNotFound
    end

    scenario "raises an error if budget id is not found" do
      expect do
        visit budget_ballot_path(0)
      end.to raise_error ActiveRecord::RecordNotFound
    end

  end

  context "Lines Load" do
    before do
      create(:budget_investment, :selected, heading: california, title: "More rain")
      budget.update(slug: "budget_slug")
      login_as(user)
    end

    scenario "finds ballot lines using budget slug", :js do
      visit budget_investments_path("budget_slug", states, california)
      add_to_ballot("More rain")

      within("#sidebar") { expect(page).to have_content "More rain" }
    end

  end

  context "Voting" do

    before do
      login_as(user)
      visit budget_path(budget)
    end

    let!(:city) { create(:budget_group, budget: budget, name: "City") }
    let!(:districts) { create(:budget_group, budget: budget, name: "Districts") }

    context "Group and Heading Navigation" do

      scenario "Groups" do
        visit budget_path(budget)

        expect(page).to have_link "City"
        expect(page).to have_link "Districts"
      end

      scenario "Headings" do
        create(:budget_heading, group: city,      name: "Investments Type1")
        create(:budget_heading, group: city,      name: "Investments Type2")
        create(:budget_heading, group: districts, name: "District 1")
        create(:budget_heading, group: districts, name: "District 2")

        visit budget_path(budget)
        click_link "City"

        expect(page).to have_link "Investments Type1"
        expect(page).to have_link "Investments Type2"

        visit budget_path(budget)
        click_link "Districts"

        expect(page).to have_link "District 1"
        expect(page).to have_link "District 2"

      end

      scenario "Investments" do
        create(:budget_heading, group: city, name: "Under the city")

        create(:budget_heading, group: city, name: "Above the city") do |heading|
          create(:budget_investment, :selected, heading: heading, title: "Solar panels")
          create(:budget_investment, :selected, heading: heading, title: "Observatory")
        end

        create(:budget_heading, group: districts, name: "District 1") do |heading|
          create(:budget_investment, :selected, heading: heading, title: "New park")
          create(:budget_investment, :selected, heading: heading, title: "Zero-emission zone")
        end

        create(:budget_heading, group: districts, name: "District 2") do |heading|
          create(:budget_investment, :selected, heading: heading, title: "Climbing wall")
        end

        visit budget_path(budget)
        click_link "City"
        click_link "Above the city"

        expect(page).to have_css(".budget-investment", count: 2)
        expect(page).to have_content "Solar panels"
        expect(page).to have_content "Observatory"

        visit budget_path(budget)

        click_link "Districts"
        click_link "District 1"

        expect(page).to have_css(".budget-investment", count: 2)
        expect(page).to have_content "New park"
        expect(page).to have_content "Zero-emission zone"

        visit budget_path(budget)
        click_link "Districts"
        click_link "District 2"

{'fraction_non_alphanumeric': 0.10659251535163944, 'fraction_numerical': 0.007067547213532615, 'mean_word_length': 3.14402304368699, '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': '2633174', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2571, 'n_words': 712}