diff --git a/app/models/case_log.rb b/app/models/case_log.rb index a8250056d..ef77ea5a5 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -411,10 +411,10 @@ class CaseLog < ApplicationRecord def self.to_csv CSV.generate(headers: true) do |csv| - csv << attribute_names + csv << attribute_names + %w[unittype_sh] all.find_each do |record| - csv << record.attributes.map do |att, val| + csv << record.attributes.merge({ "unittype_sh" => record.unittype_sh }).map do |att, val| record.form.get_question(att, record)&.label_from_value(val) || val end end diff --git a/spec/fixtures/files/case_logs_download.csv b/spec/fixtures/files/case_logs_download.csv new file mode 100644 index 000000000..0f7c45365 --- /dev/null +++ b/spec/fixtures/files/case_logs_download.csv @@ -0,0 +1,2 @@ +id,status,created_at,updated_at,tenancycode,age1,sex1,ethnic,national,prevten,ecstat1,hhmemb,age2,sex2,ecstat2,age3,sex3,ecstat3,age4,sex4,ecstat4,age5,sex5,ecstat5,age6,sex6,ecstat6,age7,sex7,ecstat7,age8,sex8,ecstat8,homeless,underoccupation_benefitcap,leftreg,reservist,illness,preg_occ,startertenancy,tenancylength,tenancy,ppostcode_full,rsnvac,unittype_gn,beds,offered,wchair,earnings,incfreq,benefits,period,layear,waityear,postcode_full,reasonpref,cbl,chr,cap,reasonother,housingneeds_a,housingneeds_b,housingneeds_c,housingneeds_f,housingneeds_g,housingneeds_h,illness_type_1,illness_type_2,illness_type_3,illness_type_4,illness_type_8,illness_type_5,illness_type_6,illness_type_7,illness_type_9,illness_type_10,rp_homeless,rp_insan_unsat,rp_medwel,rp_hardship,rp_dontknow,tenancyother,net_income_value_check,property_owner_organisation,property_manager_organisation,sale_or_letting,irproduct_other,purchaser_code,reason,propcode,majorrepairs,la,prevloc,hb,hbrentshortfall,property_relet,mrcdate,incref,sale_completion_date,startdate,armedforces,first_time_property_let_as_social_housing,unitletas,builtype,voiddate,owning_organisation_id,managing_organisation_id,renttype,needstype,lettype,postcode_known,is_la_inferred,totchild,totelder,totadult,net_income_known,nocharge,is_carehome,household_charge,referral,brent,scharge,pscharge,supcharg,tcharge,tshortfall,chcharge,declaration,ppcodenk,previous_la_known,is_previous_la_inferred,age1_known,age2_known,age3_known,age4_known,age5_known,age6_known,age7_known,age8_known,ethnic_group,ethnic_other,letting_allocation_unknown,details_known_2,details_known_3,details_known_4,details_known_5,details_known_6,details_known_7,details_known_8,rent_type,has_benefits,renewal,wrent,wscharge,wpschrge,wsupchrg,wtcharge,wtshortfall,refused,housingneeds,wchchrg,newprop,relat2,relat3,relat4,relat5,relat6,relat7,relat8,rent_value_check,old_form_id,lar,irproduct,old_id,joint,created_by_id,illness_type_0,retirement_value_check,tshortfall_known,sheltered,pregnancy_value_check,hhtype,new_old,vacdays,scheme_id,location_id,unittype_sh +{id},in_progress,2022-02-08 16:52:15 +0000,2022-02-08 16:52:15 +0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,DLUHC,{owning_org_id},,Supported housing,,,false,0,0,0,,0,,,,,,,,,,,,,,false,,,,,,,,,,,,,,,,,,,,0,,,,,,,,0,,,,,,,,,,,,,,,,,Danny Rojas,,,,,,9,,,{scheme_id},SE11TE,6 diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index ae91b6b0c..877925dff 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -2235,4 +2235,23 @@ RSpec.describe CaseLog do end end end + + describe "csv download" do + let(:csv_export_file) { File.open("spec/fixtures/files/case_logs_download.csv", "r:UTF-8") } + let(:scheme) { FactoryBot.create(:scheme) } + let(:location) { FactoryBot.create(:location, scheme:, type_of_unit: 6, postcode: "SE11TE") } + + before do + Timecop.freeze(Time.utc(2022, 6, 5)) + end + + it "generates a correct csv from a case log" do + case_log = FactoryBot.create(:case_log, needstype: 2, scheme:, location:) + expected_content = csv_export_file.read + expected_content.sub!(/\{id\}/, case_log["id"].to_s) + expected_content.sub!(/\{owning_org_id\}/, case_log["owning_organisation_id"].to_s) + expected_content.sub!(/\{scheme_id\}/, scheme["service_name"].to_s) + expect(described_class.to_csv).to eq(expected_content) + end + end end