implements some parts of the spending proposals importer

This commit is contained in:
kikito
2016-09-07 11:53:24 +02:00
parent 14bb9eef84
commit bdfd326907
2 changed files with 118 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
class SpendingProposalsImporter
def import(sp)
# feasibility
# unfeasibility_explanation
# heading_id
# valuator_assignments_count
# hidden_at
# comments_count
# group_id
# budget_id
# duration
# comments
budget = Budget.last || Budget.create!(name: Date.today.year.to_s, currency_symbol: "")
group = nil
heading = nil
if sp.geozone_id.present?
group = budget.groups.find_or_create_by!(name: "Barrios")
heading = group.headings.find_or_create_by!(name: sp.geozone.name, price: 10000000)
else
group = budget.groups.find_or_create_by!(name: "Toda la ciudad")
heading = group.headings.find_or_create_by!(name: "Toda la ciudad", price: 10000000)
end
feasibility = case sp.feasible
when FalseClass
'unfeasible'
when TrueClass
'feasible'
else
'undecided'
end
Budget::Investment.create!(
heading_id: heading.id,
author_id: sp.author_id,
administrator_id: sp.administrator_id,
title: sp.title,
description: sp.description,
external_url: sp.external_url,
price: sp.price,
price_explanation: sp.price_explanation,
internal_comments: sp.internal_comments,
feasibility: feasibility,
valuation_finished: sp.valuation_finished,
price_first_year: sp.price_first_year,
cached_votes_up: sp.cached_votes_up,
physical_votes: sp.physical_votes,
created_at: sp.created_at,
updated_at: sp.updated_at,
responsible_name: sp.responsible_name,
terms_of_service: "1"
)
end
end

View File

@@ -0,0 +1,57 @@
require 'rails_helper'
describe SpendingProposalsImporter do
let(:importer) { SpendingProposalsImporter.new }
describe '#import' do
it "Imports a city spending proposal" do
sp = create(:spending_proposal)
expect { importer.import(sp) }.to change{ Budget::Investment.count }.from(0).to(1)
inv = Budget::Investment.last
expect(inv.author).to eq(sp.author)
expect(inv.title).to eq(sp.title)
expect(inv.heading.name).to eq("Toda la ciudad")
expect(inv.heading.group.name).to eq("Toda la ciudad")
end
it "Imports a city spending proposal" do
sp = create(:spending_proposal, geozone: create(:geozone, name: "Bel Air"))
expect { importer.import(sp) }.to change{ Budget::Investment.count }.from(0).to(1)
inv = Budget::Investment.last
expect(inv.author).to eq(sp.author)
expect(inv.title).to eq(sp.title)
expect(inv.heading.name).to eq("Bel Air")
expect(inv.heading.group.name).to eq("Barrios")
end
it "Uses existing budgets, headings and groups instead of creating new ones" do
sp1 = create(:spending_proposal, geozone: create(:geozone, name: "Bel Air"))
sp2 = create(:spending_proposal, geozone: create(:geozone, name: "Bel Air"))
expect { importer.import(sp1) }.to change{ Budget::Investment.count }.from(0).to(1)
expect { importer.import(sp2) }.to change{ Budget::Investment.count }.from(1).to(2)
inv1 = Budget::Investment.first
inv2 = Budget::Investment.last
expect(inv2.heading).to eq(inv1.heading)
end
it "Imports feasibility correctly" do
sp = create(:spending_proposal)
feasible = create(:spending_proposal, feasible: true)
unfeasible = create(:spending_proposal, feasible: false)
expect(importer.import(sp).feasibility).to eq('undecided')
expect(importer.import(feasible).feasibility).to eq('feasible')
expect(importer.import(unfeasible).feasibility).to eq('unfeasible')
end
end
end