Browse Source

Backfill default household characteristics for completed log

pull/1331/head
Kat 3 years ago
parent
commit
cbb15be2da
  1. 24
      app/services/imports/sales_logs_import_service.rb
  2. 132
      spec/services/imports/sales_logs_import_service_spec.rb

24
app/services/imports/sales_logs_import_service.rb

@ -223,7 +223,7 @@ module Imports
return nil if attributes["hhmemb"].nil? || index > attributes["hhmemb"] return nil if attributes["hhmemb"].nil? || index > attributes["hhmemb"]
return nil if attributes["jointpur"] == 1 && index == 2 return nil if attributes["jointpur"] == 1 && index == 2
if attributes["age#{index}_known"] == 1 && if attributes["age#{index}_known"] != 0 &&
attributes["sex#{index}"] == "R" && attributes["sex#{index}"] == "R" &&
attributes["relat#{index}"] == "R" && attributes["relat#{index}"] == "R" &&
attributes["ecstat#{index}"] == 10 attributes["ecstat#{index}"] == 10
@ -400,6 +400,28 @@ module Imports
attributes["mscharge_known"] ||= 0 if attributes["ownershipsch"] == 3 attributes["mscharge_known"] ||= 0 if attributes["ownershipsch"] == 3
attributes["armedforcesspouse"] ||= 7 attributes["armedforcesspouse"] ||= 7
attributes["savingsnk"] ||= attributes["savings"].present? ? 0 : 1 attributes["savingsnk"] ||= attributes["savings"].present? ? 0 : 1
# buyer 1 characteristics
attributes["age1_known"] ||= 1
attributes["sex1"] ||= "R"
attributes["ethnic_group"] ||= 17
attributes["national"] ||= 13
attributes["ecstat1"] ||= 10
# buyer 2 characteristics
if attributes["jointpur"] == 1
attributes["age2_known"] ||= 1
attributes["sex2"] ||= "R"
attributes["ecstat2"] ||= 10
end
# other household members characteristics
(2..attributes["hhmemb"]).each do |index|
attributes["age#{index}_known"] ||= 1
attributes["sex#{index}"] ||= "R"
attributes["ecstat#{index}"] ||= 10
attributes["relat#{index}"] ||= "R"
end
end end
end end
end end

132
spec/services/imports/sales_logs_import_service_spec.rb

@ -253,6 +253,138 @@ RSpec.describe Imports::SalesLogsImportService do
expect(sales_log.mscharge_known).to eq(0) expect(sales_log.mscharge_known).to eq(0)
end end
end end
context "when inferring age known" do
let(:sales_log_id) { discounted_ownership_sales_log_id }
before do
sales_log_xml.at_xpath("//xmlns:HHMEMB").content = "3"
sales_log_xml.at_xpath("//xmlns:P1Age").content = ""
sales_log_xml.at_xpath("//xmlns:P2Age").content = ""
sales_log_xml.at_xpath("//xmlns:P3Age").content = "22"
allow(logger).to receive(:warn).and_return(nil)
sales_log_service.send(:create_log, sales_log_xml)
end
it "sets age known to no if age not answered" do
sales_log = SalesLog.find_by(old_id: sales_log_id)
expect(sales_log&.age1_known).to be(1) # unknown
expect(sales_log&.age2_known).to be(1) # unknown
end
it "sets age known to yes if age answered" do
sales_log = SalesLog.find_by(old_id: sales_log_id)
expect(sales_log&.age3_known).to be(0) # known
end
end
context "when inferring gender" do
let(:sales_log_id) { discounted_ownership_sales_log_id }
before do
sales_log_xml.at_xpath("//xmlns:HHMEMB").content = "3"
sales_log_xml.at_xpath("//xmlns:P1Sex").content = ""
sales_log_xml.at_xpath("//xmlns:P2Sex").content = ""
sales_log_xml.at_xpath("//xmlns:P3Sex").content = "Female"
allow(logger).to receive(:warn).and_return(nil)
sales_log_service.send(:create_log, sales_log_xml)
end
it "sets gender to prefers not to say if not answered" do
sales_log = SalesLog.find_by(old_id: sales_log_id)
expect(sales_log&.sex1).to eq("R")
expect(sales_log&.sex2).to eq("R")
end
it "sets the gender correctly if answered" do
sales_log = SalesLog.find_by(old_id: sales_log_id)
expect(sales_log&.sex3).to eq("F")
end
end
context "when inferring ethnic group" do
let(:sales_log_id) { discounted_ownership_sales_log_id }
before do
sales_log_xml.at_xpath("//xmlns:HHMEMB").content = "1"
sales_log_xml.at_xpath("//xmlns:P1Eth").content = ""
allow(logger).to receive(:warn).and_return(nil)
sales_log_service.send(:create_log, sales_log_xml)
end
it "sets ethnic group to prefers not to say if not answered" do
sales_log = SalesLog.find_by(old_id: sales_log_id)
expect(sales_log&.ethnic_group).to eq(17)
end
end
context "when inferring nationality" do
let(:sales_log_id) { discounted_ownership_sales_log_id }
before do
sales_log_xml.at_xpath("//xmlns:HHMEMB").content = "1"
sales_log_xml.at_xpath("//xmlns:P1Nat").content = ""
allow(logger).to receive(:warn).and_return(nil)
sales_log_service.send(:create_log, sales_log_xml)
end
it "sets nationality to prefers not to say if not answered" do
sales_log = SalesLog.find_by(old_id: sales_log_id)
expect(sales_log&.national).to eq(13)
end
end
context "when inferring economic status" do
let(:sales_log_id) { discounted_ownership_sales_log_id }
before do
sales_log_xml.at_xpath("//xmlns:HHMEMB").content = "3"
sales_log_xml.at_xpath("//xmlns:P1Eco").content = ""
sales_log_xml.at_xpath("//xmlns:P2Eco").content = ""
sales_log_xml.at_xpath("//xmlns:P3Eco").content = "3"
allow(logger).to receive(:warn).and_return(nil)
sales_log_service.send(:create_log, sales_log_xml)
end
it "sets economic status to prefers not to say if not answered" do
sales_log = SalesLog.find_by(old_id: sales_log_id)
expect(sales_log&.ecstat1).to eq(10)
expect(sales_log&.ecstat2).to eq(10)
end
it "sets the economic status correctly if answered" do
sales_log = SalesLog.find_by(old_id: sales_log_id)
expect(sales_log&.ecstat3).to eq(3)
end
end
context "when inferring relationship" do
let(:sales_log_id) { discounted_ownership_sales_log_id }
before do
sales_log_xml.at_xpath("//xmlns:HHMEMB").content = "3"
sales_log_xml.at_xpath("//xmlns:P2Rel").content = ""
sales_log_xml.at_xpath("//xmlns:P3Rel").content = "Partner"
allow(logger).to receive(:warn).and_return(nil)
sales_log_service.send(:create_log, sales_log_xml)
end
it "sets relationship to prefers not to say if not answered" do
sales_log = SalesLog.find_by(old_id: sales_log_id)
expect(sales_log&.relat2).to eq("R")
end
it "sets the relationship correctly if answered" do
sales_log = SalesLog.find_by(old_id: sales_log_id)
expect(sales_log&.relat3).to eq("P")
end
end
end end
end end
end end

Loading…
Cancel
Save