improve csv generation for bilans bdf

This commit is contained in:
Christophe Robillard 2020-04-30 16:03:05 +02:00
parent 9873826dd4
commit 9de2c7c85a
2 changed files with 28 additions and 2 deletions

View file

@ -139,8 +139,10 @@ class Etablissement < ApplicationRecord
end
def entreprise_bilans_bdf_to_csv
headers = entreprise_bilans_bdf[0].keys
data = entreprise_bilans_bdf.map(&:values)
headers = entreprise_bilans_bdf.flat_map(&:keys).uniq
data = entreprise_bilans_bdf.map do |bilan|
headers.map { |h| bilan[h] }
end
SpreadsheetArchitect.to_csv(headers: headers, data: data)
end

View file

@ -35,4 +35,28 @@ describe Etablissement do
end
end
end
describe '.entreprise_bilans_bdf_to_csv' do
let(:etablissement) { build(:etablissement, entreprise_bilans_bdf: bilans) }
let(:bilans) do
[
{
"total_passif": "1200",
"chiffres_affaires_ht": "40000"
},
{
"total_passif": "0",
"evolution_total_dettes_stables": "30"
}
]
end
subject { etablissement.entreprise_bilans_bdf_to_csv.split("\n") }
it "build a csv with all keys" do
expect(subject[0].split(',').sort).to eq(["total_passif", "chiffres_affaires_ht", "evolution_total_dettes_stables"].sort)
expect(subject[1].split(',')).to eq(["1200", "40000"])
expect(subject[2].split(',')).to eq(["0", "", "30"])
end
end
end