Browse Source

CLDC 3241: Part 2 - Explanation of variables on CSVs (#2610)

* Add variable definitions as the first row of csv downloads
pull/2614/head v0.4.70
Manny Dinssa 7 months ago committed by GitHub
parent
commit
92aeb737a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 17
      app/services/csv/lettings_log_csv_service.rb
  2. 20
      app/services/csv/sales_log_csv_service.rb
  3. 8
      spec/factories/csv_variable_definitions.rb
  4. 1
      spec/fixtures/files/lettings_log_csv_export_codes_23.csv
  5. 1
      spec/fixtures/files/lettings_log_csv_export_codes_24.csv
  6. 1
      spec/fixtures/files/lettings_log_csv_export_labels_23.csv
  7. 1
      spec/fixtures/files/lettings_log_csv_export_labels_24.csv
  8. 1
      spec/fixtures/files/lettings_log_csv_export_non_support_codes_23.csv
  9. 1
      spec/fixtures/files/lettings_log_csv_export_non_support_codes_24.csv
  10. 1
      spec/fixtures/files/lettings_log_csv_export_non_support_labels_23.csv
  11. 1
      spec/fixtures/files/lettings_log_csv_export_non_support_labels_24.csv
  12. 1
      spec/fixtures/files/sales_logs_csv_export_codes_23.csv
  13. 1
      spec/fixtures/files/sales_logs_csv_export_codes_24.csv
  14. 1
      spec/fixtures/files/sales_logs_csv_export_labels_23.csv
  15. 1
      spec/fixtures/files/sales_logs_csv_export_labels_24.csv
  16. 1
      spec/fixtures/files/sales_logs_csv_export_non_support_labels_24.csv
  17. 116
      spec/services/csv/lettings_log_csv_service_spec.rb
  18. 131
      spec/services/csv/sales_log_csv_service_spec.rb

17
app/services/csv/lettings_log_csv_service.rb

@ -5,10 +5,17 @@ module Csv
@export_type = export_type @export_type = export_type
@year = year @year = year
@attributes = lettings_log_attributes @attributes = lettings_log_attributes
@definitions = lettings_log_definitions
end end
def prepare_csv(logs) def prepare_csv(logs)
CSV.generate(headers: true) do |csv| CSV.generate(headers: true) do |csv|
if @year >= 2023
csv << @attributes.map do |attribute|
record = @definitions.find { |r| r.variable == attribute.downcase }
record&.tap { |r| r.update!(last_accessed: Time.zone.now) }&.definition
end
end
csv << @attributes csv << @attributes
logs.find_each do |log| logs.find_each do |log|
@ -258,6 +265,16 @@ module Csv
@user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES - soft_validations_attributes @user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES - soft_validations_attributes
end end
def lettings_log_definitions
CsvVariableDefinition.lettings.group_by { |record| [record.variable, record.definition] }
.map do |_, options|
exact_match = options.find { |definition| definition.year == @year }
next exact_match if exact_match
options.max_by(&:year)
end
end
def insert_derived_and_related_attributes(ordered_questions) def insert_derived_and_related_attributes(ordered_questions)
ordered_questions.flat_map do |question| ordered_questions.flat_map do |question|
if question.type == "checkbox" if question.type == "checkbox"

20
app/services/csv/sales_log_csv_service.rb

@ -5,11 +5,19 @@ module Csv
@export_type = export_type @export_type = export_type
@year = year @year = year
@attributes = sales_log_attributes @attributes = sales_log_attributes
@definitions = sales_log_definitions
end end
def prepare_csv(logs) def prepare_csv(logs)
CSV.generate(headers: true) do |csv| CSV.generate(headers: true) do |csv|
csv << formatted_attribute_headers formatted_attributes = formatted_attribute_headers
if @year >= 2023
csv << formatted_attributes.map do |attribute|
record = @definitions.find { |r| r.variable == attribute.downcase }
record&.tap { |r| r.update!(last_accessed: Time.zone.now) }&.definition
end
end
csv << formatted_attributes
logs.find_each do |log| logs.find_each do |log|
csv << @attributes.map { |attribute| value(attribute, log) } csv << @attributes.map { |attribute| value(attribute, log) }
@ -161,6 +169,16 @@ module Csv
@user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES @user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES
end end
def sales_log_definitions
CsvVariableDefinition.sales.group_by { |record| [record.variable, record.definition] }
.map do |_, options|
exact_match = options.find { |definition| definition.year == @year }
next exact_match if exact_match
options.max_by(&:year)
end
end
def insert_derived_and_related_attributes(ordered_questions) def insert_derived_and_related_attributes(ordered_questions)
ordered_questions.flat_map do |question| ordered_questions.flat_map do |question|
if question.type == "checkbox" if question.type == "checkbox"

8
spec/factories/csv_variable_definitions.rb

@ -0,0 +1,8 @@
FactoryBot.define do
factory :csv_variable_definition do
variable { "variable" }
definition { "definition" }
log_type { "lettings" }
year { 2024 }
end
end

1
spec/fixtures/files/lettings_log_csv_export_codes_23.csv vendored

File diff suppressed because one or more lines are too long

1
spec/fixtures/files/lettings_log_csv_export_codes_24.csv vendored

File diff suppressed because one or more lines are too long

1
spec/fixtures/files/lettings_log_csv_export_labels_23.csv vendored

File diff suppressed because one or more lines are too long

1
spec/fixtures/files/lettings_log_csv_export_labels_24.csv vendored

File diff suppressed because one or more lines are too long

1
spec/fixtures/files/lettings_log_csv_export_non_support_codes_23.csv vendored

File diff suppressed because one or more lines are too long

1
spec/fixtures/files/lettings_log_csv_export_non_support_codes_24.csv vendored

File diff suppressed because one or more lines are too long

1
spec/fixtures/files/lettings_log_csv_export_non_support_labels_23.csv vendored

File diff suppressed because one or more lines are too long

1
spec/fixtures/files/lettings_log_csv_export_non_support_labels_24.csv vendored

File diff suppressed because one or more lines are too long

1
spec/fixtures/files/sales_logs_csv_export_codes_23.csv vendored

File diff suppressed because one or more lines are too long

1
spec/fixtures/files/sales_logs_csv_export_codes_24.csv vendored

File diff suppressed because one or more lines are too long

1
spec/fixtures/files/sales_logs_csv_export_labels_23.csv vendored

File diff suppressed because one or more lines are too long

1
spec/fixtures/files/sales_logs_csv_export_labels_24.csv vendored

File diff suppressed because one or more lines are too long

1
spec/fixtures/files/sales_logs_csv_export_non_support_labels_24.csv vendored

File diff suppressed because one or more lines are too long

116
spec/services/csv/lettings_log_csv_service_spec.rb

@ -1,6 +1,16 @@
require "rails_helper" require "rails_helper"
require "rake"
RSpec.describe Csv::LettingsLogCsvService do RSpec.describe Csv::LettingsLogCsvService do
subject(:task) { Rake::Task["data_import:add_variable_definitions"] }
before do
Rake.application.rake_require("tasks/log_variable_definitions")
Rake::Task.define_task(:environment)
task.reenable
task.invoke("spec/fixtures/variable_definitions")
end
context "when downloading a csv" do context "when downloading a csv" do
let(:log) { create(:lettings_log) } let(:log) { create(:lettings_log) }
let(:user) { create(:user, :support, email: "s.port@jeemayle.com") } let(:user) { create(:user, :support, email: "s.port@jeemayle.com") }
@ -9,15 +19,21 @@ RSpec.describe Csv::LettingsLogCsvService do
let(:year) { 2024 } let(:year) { 2024 }
let(:csv) { CSV.parse(service.prepare_csv(LettingsLog.where(id: logs.map(&:id)))) } let(:csv) { CSV.parse(service.prepare_csv(LettingsLog.where(id: logs.map(&:id)))) }
let(:logs) { [log] } let(:logs) { [log] }
let(:headers) { csv.first } let(:definition_line) { csv.first }
let(:attribute_line) { csv.second }
let(:content_line) { csv.third }
it "returns a string" do it "returns a string" do
result = service.prepare_csv(LettingsLog.all) result = service.prepare_csv(LettingsLog.all)
expect(result).to be_a String expect(result).to be_a String
end end
it "returns a csv with headers" do it "returns a csv with definition headers on the first line" do
expect(csv.first.first).to eq "id" expect(definition_line.first).to eq "Log ID"
end
it "returns a csv with attribute headers on the second line" do
expect(attribute_line.first).to eq "id"
end end
context "when stubbing :ordered_questions_for_year" do context "when stubbing :ordered_questions_for_year" do
@ -50,13 +66,13 @@ RSpec.describe Csv::LettingsLogCsvService do
let(:question_ids) { %w[prevten startdate brent rent_type] } let(:question_ids) { %w[prevten startdate brent rent_type] }
it "includes log attributes related to questions to the headers" do it "includes log attributes related to questions to the headers" do
expect(headers).to include(*question_ids.first(3)) expect(attribute_line).to include(*question_ids.first(3))
end end
it "removes some log attributes related to questions from the headers and replaces them with their derived values in the correct order" do it "removes some log attributes related to questions from the headers and replaces them with their derived values in the correct order" do
expect(headers).not_to include "rent_type" expect(attribute_line).not_to include "rent_type"
expect(headers).to include(*%w[wrent renttype renttype_detail]) expect(attribute_line).to include(*%w[wrent renttype renttype_detail])
expect(headers).not_to include("rent_type_detail") expect(attribute_line).not_to include("rent_type_detail")
end end
end end
@ -70,23 +86,23 @@ RSpec.describe Csv::LettingsLogCsvService do
it "does not add the id of the checkbox question to the headers" do it "does not add the id of the checkbox question to the headers" do
question_ids = questions.map(&:id) question_ids = questions.map(&:id)
expect(headers).not_to include(*question_ids) expect(attribute_line).not_to include(*question_ids)
end end
it "adds the related log attributes from the answer options to the headers" do it "adds the related log attributes from the answer options to the headers" do
log_attributes = questions.flat_map { |q| q.answer_options.keys } log_attributes = questions.flat_map { |q| q.answer_options.keys }
expect(headers).to include(*log_attributes) expect(attribute_line).to include(*log_attributes)
end end
end end
end end
it "adds log attributes not related to questions to the headers" do it "adds log attributes not related to questions to the headers" do
expect(headers.first(5)).to eq %w[id status duplicate_set_id created_by assigned_to] expect(attribute_line.first(5)).to eq %w[id status duplicate_set_id created_by assigned_to]
end end
it "adds attributes related to associated schemes and locations to the headers" do it "adds attributes related to associated schemes and locations to the headers" do
expect(headers).to include(*%w[scheme_service_name scheme_confidential SCHTYPE scheme_registered_under_care_act]) expect(attribute_line).to include(*%w[scheme_service_name scheme_confidential SCHTYPE scheme_registered_under_care_act])
expect(headers.last(5)).to eq %w[location_units location_type_of_unit location_mobility_type location_local_authority location_startdate] expect(attribute_line.last(5)).to eq %w[location_units location_type_of_unit location_mobility_type location_local_authority location_startdate]
end end
context "when there are many logs" do context "when there are many logs" do
@ -94,7 +110,7 @@ RSpec.describe Csv::LettingsLogCsvService do
let(:log_count) { 30 } let(:log_count) { 30 }
it "creates a CSV with the correct number of logs" do it "creates a CSV with the correct number of logs" do
expected_row_count_with_headers = log_count + 1 expected_row_count_with_headers = log_count + 2
expect(csv.size).to be expected_row_count_with_headers expect(csv.size).to be expected_row_count_with_headers
end end
end end
@ -104,32 +120,32 @@ RSpec.describe Csv::LettingsLogCsvService do
let(:log) { create(:lettings_log, :setup_completed, hhmemb: 2, details_known_2: 0, relat2: "P", age1: 35, la: "E09000003", duplicate_set_id: 12_312) } let(:log) { create(:lettings_log, :setup_completed, hhmemb: 2, details_known_2: 0, relat2: "P", age1: 35, la: "E09000003", duplicate_set_id: 12_312) }
it "gives answer to radio questions as labels" do it "gives answer to radio questions as labels" do
relat2_column_index = csv.first.index("relat2") relat2_column_index = attribute_line.index("relat2")
relat2_value = csv.second[relat2_column_index] relat2_value = content_line[relat2_column_index]
expect(relat2_value).to eq "Partner" expect(relat2_value).to eq "Partner"
end end
it "gives answers to free input questions as the user input" do it "gives answers to free input questions as the user input" do
age1_column_index = csv.first.index("age1") age1_column_index = attribute_line.index("age1")
age1_value = csv.second[age1_column_index] age1_value = content_line[age1_column_index]
expect(age1_value).to eq 35.to_s expect(age1_value).to eq 35.to_s
end end
it "exports the code for the local authority under the heading 'la'" do it "exports the code for the local authority under the heading 'la'" do
la_column_index = csv.first.index("la") la_column_index = attribute_line.index("la")
la_value = csv.second[la_column_index] la_value = content_line[la_column_index]
expect(la_value).to eq "E09000003" expect(la_value).to eq "E09000003"
end end
it "exports the label for the local authority under the heading 'la_label'" do it "exports the label for the local authority under the heading 'la_label'" do
la_label_column_index = csv.first.index("la_label") la_label_column_index = attribute_line.index("la_label")
la_label_value = csv.second[la_label_column_index] la_label_value = content_line[la_label_column_index]
expect(la_label_value).to eq "Barnet" expect(la_label_value).to eq "Barnet"
end end
it "exports the id for under the heading 'duplicate_set_id'" do it "exports the id for under the heading 'duplicate_set_id'" do
duplicate_set_id_column_index = csv.first.index("duplicate_set_id") duplicate_set_id_column_index = attribute_line.index("duplicate_set_id")
duplicate_set_id_value = csv.second[duplicate_set_id_column_index] duplicate_set_id_value = content_line[duplicate_set_id_column_index]
expect(duplicate_set_id_value).to eq "12312" expect(duplicate_set_id_value).to eq "12312"
end end
end end
@ -139,32 +155,32 @@ RSpec.describe Csv::LettingsLogCsvService do
let(:log) { create(:lettings_log, :setup_completed, hhmemb: 2, details_known_2: 0, relat2: "P", age1: 35, la: "E09000003", duplicate_set_id: 12_312) } let(:log) { create(:lettings_log, :setup_completed, hhmemb: 2, details_known_2: 0, relat2: "P", age1: 35, la: "E09000003", duplicate_set_id: 12_312) }
it "gives answer to radio questions as labels" do it "gives answer to radio questions as labels" do
relat2_column_index = csv.first.index("relat2") relat2_column_index = attribute_line.index("relat2")
relat2_value = csv.second[relat2_column_index] relat2_value = content_line[relat2_column_index]
expect(relat2_value).to eq "P" expect(relat2_value).to eq "P"
end end
it "gives answers to free input questions as the user input" do it "gives answers to free input questions as the user input" do
age1_column_index = csv.first.index("age1") age1_column_index = attribute_line.index("age1")
age1_value = csv.second[age1_column_index] age1_value = content_line[age1_column_index]
expect(age1_value).to eq 35.to_s expect(age1_value).to eq 35.to_s
end end
it "exports the code for the local authority under the heading 'la'" do it "exports the code for the local authority under the heading 'la'" do
la_column_index = csv.first.index("la") la_column_index = attribute_line.index("la")
la_value = csv.second[la_column_index] la_value = content_line[la_column_index]
expect(la_value).to eq "E09000003" expect(la_value).to eq "E09000003"
end end
it "exports the label for the local authority under the heading 'la_label'" do it "exports the label for the local authority under the heading 'la_label'" do
la_label_column_index = csv.first.index("la_label") la_label_column_index = attribute_line.index("la_label")
la_label_value = csv.second[la_label_column_index] la_label_value = content_line[la_label_column_index]
expect(la_label_value).to eq "Barnet" expect(la_label_value).to eq "Barnet"
end end
it "exports the duplicate log reference under the heading 'duplicate_set_id'" do it "exports the duplicate log reference under the heading 'duplicate_set_id'" do
duplicate_set_id_column_index = csv.first.index("duplicate_set_id") duplicate_set_id_column_index = attribute_line.index("duplicate_set_id")
duplicate_set_id_value = csv.second[duplicate_set_id_column_index] duplicate_set_id_value = content_line[duplicate_set_id_column_index]
expect(duplicate_set_id_value).to eq "12312" expect(duplicate_set_id_value).to eq "12312"
end end
end end
@ -173,7 +189,7 @@ RSpec.describe Csv::LettingsLogCsvService do
let(:user) { create(:user, :data_coordinator, email: "choreographer@owtluk.com") } let(:user) { create(:user, :data_coordinator, email: "choreographer@owtluk.com") }
it "does not include certain attributes in the headers" do it "does not include certain attributes in the headers" do
expect(headers).not_to include(*%w[wrent wscharge wpschrge wsupchrg wtcharge]) expect(attribute_line).not_to include(*%w[wrent wscharge wpschrge wsupchrg wtcharge])
end end
end end
@ -316,8 +332,8 @@ RSpec.describe Csv::LettingsLogCsvService do
expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_labels_24.csv") expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_labels_24.csv")
values_to_delete = %w[id] values_to_delete = %w[id]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end
@ -330,8 +346,8 @@ RSpec.describe Csv::LettingsLogCsvService do
expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_non_support_labels_24.csv") expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_non_support_labels_24.csv")
values_to_delete = %w[id] values_to_delete = %w[id]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end
@ -348,8 +364,8 @@ RSpec.describe Csv::LettingsLogCsvService do
expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_codes_24.csv") expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_codes_24.csv")
values_to_delete = %w[id] values_to_delete = %w[id]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end
@ -362,8 +378,8 @@ RSpec.describe Csv::LettingsLogCsvService do
expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_non_support_codes_24.csv") expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_non_support_codes_24.csv")
values_to_delete = %w[id] values_to_delete = %w[id]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end
@ -504,8 +520,8 @@ RSpec.describe Csv::LettingsLogCsvService do
expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_labels_23.csv") expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_labels_23.csv")
values_to_delete = %w[id] values_to_delete = %w[id]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end
@ -518,8 +534,8 @@ RSpec.describe Csv::LettingsLogCsvService do
expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_non_support_labels_23.csv") expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_non_support_labels_23.csv")
values_to_delete = %w[id] values_to_delete = %w[id]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end
@ -536,8 +552,8 @@ RSpec.describe Csv::LettingsLogCsvService do
expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_codes_23.csv") expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_codes_23.csv")
values_to_delete = %w[id] values_to_delete = %w[id]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end
@ -550,8 +566,8 @@ RSpec.describe Csv::LettingsLogCsvService do
expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_non_support_codes_23.csv") expected_content = CSV.read("spec/fixtures/files/lettings_log_csv_export_non_support_codes_23.csv")
values_to_delete = %w[id] values_to_delete = %w[id]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end

131
spec/services/csv/sales_log_csv_service_spec.rb

@ -1,6 +1,8 @@
require "rails_helper" require "rails_helper"
RSpec.describe Csv::SalesLogCsvService do RSpec.describe Csv::SalesLogCsvService do
subject(:task) { Rake::Task["data_import:add_variable_definitions"] }
let(:form_handler_mock) { instance_double(FormHandler) } let(:form_handler_mock) { instance_double(FormHandler) }
let(:organisation) { create(:organisation) } let(:organisation) { create(:organisation) }
let(:fixed_time) { now } let(:fixed_time) { now }
@ -37,11 +39,18 @@ RSpec.describe Csv::SalesLogCsvService do
let(:service) { described_class.new(user:, export_type: "labels", year:) } let(:service) { described_class.new(user:, export_type: "labels", year:) }
let(:csv) { CSV.parse(service.prepare_csv(SalesLog.all)) } let(:csv) { CSV.parse(service.prepare_csv(SalesLog.all)) }
let(:year) { 2024 } let(:year) { 2024 }
let(:definition_line) { csv.first }
let(:attribute_line) { csv.second }
let(:content_line) { csv.third }
before do before do
Timecop.freeze(now) Timecop.freeze(now)
Singleton.__init__(FormHandler) Singleton.__init__(FormHandler)
log log
Rake.application.rake_require("tasks/log_variable_definitions")
Rake::Task.define_task(:environment)
task.reenable
task.invoke("spec/fixtures/variable_definitions")
end end
after do after do
@ -53,8 +62,12 @@ RSpec.describe Csv::SalesLogCsvService do
expect(result).to be_a String expect(result).to be_a String
end end
it "returns a csv with headers" do it "returns a csv with definition headers on first line" do
expect(csv.first.first).to eq "ID" expect(definition_line.first).to eq "Log ID"
end
it "returns a csv with attribute headers on second line" do
expect(attribute_line.first).to eq "ID"
end end
context "when stubbing :ordered_questions_for_year" do context "when stubbing :ordered_questions_for_year" do
@ -85,14 +98,14 @@ RSpec.describe Csv::SalesLogCsvService do
let(:question_ids) { %w[type age1 buy1livein exdate] } let(:question_ids) { %w[type age1 buy1livein exdate] }
it "includes log attributes related to questions to the headers" do it "includes log attributes related to questions to the headers" do
headers = csv.first attribute_line_before_2023 = csv.first
expect(headers).to include(*%w[TYPE AGE1 LIVEINBUYER1]) expect(attribute_line_before_2023).to include(*%w[TYPE AGE1 LIVEINBUYER1])
end end
it "removes some log attributes related to questions from the headers and replaces them with their derived values in the correct order" do it "removes some log attributes related to questions from the headers and replaces them with their derived values in the correct order" do
headers = csv.first attribute_line_before_2023 = csv.first
expect(headers).not_to include "EXDATE" expect(attribute_line_before_2023).not_to include "EXDATE"
expect(headers.last(4)).to eq %w[LIVEINBUYER1 EXDAY EXMONTH EXYEAR] expect(attribute_line_before_2023.last(4)).to eq %w[LIVEINBUYER1 EXDAY EXMONTH EXYEAR]
end end
end end
@ -109,28 +122,27 @@ RSpec.describe Csv::SalesLogCsvService do
end end
it "does not add questions for checks, whether some other attribute is known or whether something else was asked" do it "does not add questions for checks, whether some other attribute is known or whether something else was asked" do
headers = csv.first attribute_line_before_2023 = csv.first
expect(headers).not_to include "attribute_value_check" expect(attribute_line_before_2023).not_to include "attribute_value_check"
expect(headers).not_to include "something_or_other_known" expect(attribute_line_before_2023).not_to include "something_or_other_known"
expect(headers).not_to include "whatchamacallit_asked" expect(attribute_line_before_2023).not_to include "whatchamacallit_asked"
end end
it "does not add the id of checkbox questions, but adds the related attributes of the log in the correct order" do it "does not add the id of checkbox questions, but adds the related attributes of the log in the correct order" do
headers = csv.first attribute_line_before_2023 = csv.first
expect(headers.last(4)).to eq %w[OWNERSHIP PREGYRHA PREGOTHER TYPE] expect(attribute_line_before_2023.last(4)).to eq %w[OWNERSHIP PREGYRHA PREGOTHER TYPE]
end end
end end
end end
it "includes attributes not related to questions to the headers" do it "includes attributes not related to questions to the headers" do
headers = csv.first expect(attribute_line).to include(*%w[ID STATUS CREATEDDATE UPLOADDATE])
expect(headers).to include(*%w[ID STATUS CREATEDDATE UPLOADDATE])
end end
it "returns a csv with the correct number of logs" do it "returns a csv with the correct number of logs" do
create_list(:sales_log, 15) create_list(:sales_log, 15)
log_count = SalesLog.count log_count = SalesLog.count
expected_row_count_with_headers = log_count + 1 expected_row_count_with_headers = log_count + 2
expect(csv.size).to be expected_row_count_with_headers expect(csv.size).to be expected_row_count_with_headers
end end
@ -140,36 +152,36 @@ RSpec.describe Csv::SalesLogCsvService do
let(:now) { fixed_time } let(:now) { fixed_time }
it "gives answers to radio questions as their labels" do it "gives answers to radio questions as their labels" do
national_column_index = csv.first.index("NATIONAL") national_column_index = attribute_line.index("NATIONAL")
national_value = csv.second[national_column_index] national_value = content_line[national_column_index]
expect(national_value).to eq "United Kingdom" expect(national_value).to eq "United Kingdom"
relat2_column_index = csv.first.index("RELAT2") relat2_column_index = attribute_line.index("RELAT2")
relat2_value = csv.second[relat2_column_index] relat2_value = content_line[relat2_column_index]
expect(relat2_value).to eq "Partner" expect(relat2_value).to eq "Partner"
end end
it "gives answers to free input questions as the user input" do it "gives answers to free input questions as the user input" do
age1_column_index = csv.first.index("AGE1") age1_column_index = attribute_line.index("AGE1")
age1_value = csv.second[age1_column_index] age1_value = content_line[age1_column_index]
expect(age1_value).to eq 30.to_s expect(age1_value).to eq 30.to_s
postcode_part1, postcode_part2 = log.postcode_full.split postcode_part1, postcode_part2 = log.postcode_full.split
postcode_part1_column_index = csv.first.index("PCODE1") postcode_part1_column_index = attribute_line.index("PCODE1")
postcode_part1_value = csv.second[postcode_part1_column_index] postcode_part1_value = content_line[postcode_part1_column_index]
expect(postcode_part1_value).to eq postcode_part1 expect(postcode_part1_value).to eq postcode_part1
postcode_part2_column_index = csv.first.index("PCODE2") postcode_part2_column_index = attribute_line.index("PCODE2")
postcode_part2_value = csv.second[postcode_part2_column_index] postcode_part2_value = content_line[postcode_part2_column_index]
expect(postcode_part2_value).to eq postcode_part2 expect(postcode_part2_value).to eq postcode_part2
end end
it "exports the code for the local authority under the heading 'la'" do it "exports the code for the local authority under the heading 'la'" do
la_column_index = csv.first.index("LA") la_column_index = attribute_line.index("LA")
la_value = csv.second[la_column_index] la_value = content_line[la_column_index]
expect(la_value).to eq "E09000033" expect(la_value).to eq "E09000033"
end end
it "exports the label for the local authority under the heading 'la_label'" do it "exports the label for the local authority under the heading 'la_label'" do
la_label_column_index = csv.first.index("LANAME") la_label_column_index = attribute_line.index("LANAME")
la_label_value = csv.second[la_label_column_index] la_label_value = content_line[la_label_column_index]
expect(la_label_value).to eq "Westminster" expect(la_label_value).to eq "Westminster"
end end
@ -186,8 +198,8 @@ RSpec.describe Csv::SalesLogCsvService do
expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_labels_24.csv") expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_labels_24.csv")
values_to_delete = %w[ID] values_to_delete = %w[ID]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end
@ -201,8 +213,8 @@ RSpec.describe Csv::SalesLogCsvService do
expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_labels_23.csv") expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_labels_23.csv")
values_to_delete = %w[ID] values_to_delete = %w[ID]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end
@ -214,8 +226,8 @@ RSpec.describe Csv::SalesLogCsvService do
end end
it "exports the id for under the heading 'duplicate_set_id'" do it "exports the id for under the heading 'duplicate_set_id'" do
duplicate_set_id_column_index = csv.first.index("DUPLICATESET") duplicate_set_id_column_index = attribute_line.index("DUPLICATESET")
duplicate_set_id_value = csv.second[duplicate_set_id_column_index] duplicate_set_id_value = content_line[duplicate_set_id_column_index]
expect(duplicate_set_id_value).to eq "12312" expect(duplicate_set_id_value).to eq "12312"
end end
end end
@ -228,36 +240,36 @@ RSpec.describe Csv::SalesLogCsvService do
let(:now) { fixed_time } let(:now) { fixed_time }
it "gives answers to radio questions as their codes" do it "gives answers to radio questions as their codes" do
national_column_index = csv.first.index("NATIONAL") national_column_index = attribute_line.index("NATIONAL")
national_value = csv.second[national_column_index] national_value = content_line[national_column_index]
expect(national_value).to eq 18.to_s expect(national_value).to eq 18.to_s
relat2_column_index = csv.first.index("RELAT2") relat2_column_index = attribute_line.index("RELAT2")
relat2_value = csv.second[relat2_column_index] relat2_value = content_line[relat2_column_index]
expect(relat2_value).to eq "P" expect(relat2_value).to eq "P"
end end
it "gives answers to free input questions as the user input" do it "gives answers to free input questions as the user input" do
age1_column_index = csv.first.index("AGE1") age1_column_index = attribute_line.index("AGE1")
age1_value = csv.second[age1_column_index] age1_value = content_line[age1_column_index]
expect(age1_value).to eq 30.to_s expect(age1_value).to eq 30.to_s
postcode_part1, postcode_part2 = log.postcode_full.split postcode_part1, postcode_part2 = log.postcode_full.split
postcode_part1_column_index = csv.first.index("PCODE1") postcode_part1_column_index = attribute_line.index("PCODE1")
postcode_part1_value = csv.second[postcode_part1_column_index] postcode_part1_value = content_line[postcode_part1_column_index]
expect(postcode_part1_value).to eq postcode_part1 expect(postcode_part1_value).to eq postcode_part1
postcode_part2_column_index = csv.first.index("PCODE2") postcode_part2_column_index = attribute_line.index("PCODE2")
postcode_part2_value = csv.second[postcode_part2_column_index] postcode_part2_value = content_line[postcode_part2_column_index]
expect(postcode_part2_value).to eq postcode_part2 expect(postcode_part2_value).to eq postcode_part2
end end
it "exports the code for the local authority under the heading 'la'" do it "exports the code for the local authority under the heading 'la'" do
la_column_index = csv.first.index("LA") la_column_index = attribute_line.index("LA")
la_value = csv.second[la_column_index] la_value = content_line[la_column_index]
expect(la_value).to eq "E09000033" expect(la_value).to eq "E09000033"
end end
it "exports the label for the local authority under the heading 'la_label'" do it "exports the label for the local authority under the heading 'la_label'" do
la_label_column_index = csv.first.index("LANAME") la_label_column_index = attribute_line.index("LANAME")
la_label_value = csv.second[la_label_column_index] la_label_value = content_line[la_label_column_index]
expect(la_label_value).to eq "Westminster" expect(la_label_value).to eq "Westminster"
end end
@ -270,8 +282,8 @@ RSpec.describe Csv::SalesLogCsvService do
expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_codes_24.csv") expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_codes_24.csv")
values_to_delete = %w[ID] values_to_delete = %w[ID]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end
@ -285,8 +297,8 @@ RSpec.describe Csv::SalesLogCsvService do
expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_codes_23.csv") expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_codes_23.csv")
values_to_delete = %w[ID] values_to_delete = %w[ID]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end
@ -298,8 +310,8 @@ RSpec.describe Csv::SalesLogCsvService do
end end
it "exports the id for under the heading 'duplicate_set_id'" do it "exports the id for under the heading 'duplicate_set_id'" do
duplicate_set_id_column_index = csv.first.index("DUPLICATESET") duplicate_set_id_column_index = attribute_line.index("DUPLICATESET")
duplicate_set_id_value = csv.second[duplicate_set_id_column_index] duplicate_set_id_value = content_line[duplicate_set_id_column_index]
expect(duplicate_set_id_value).to eq "12312" expect(duplicate_set_id_value).to eq "12312"
end end
end end
@ -307,10 +319,9 @@ RSpec.describe Csv::SalesLogCsvService do
context "when the user is not a support user" do context "when the user is not a support user" do
let(:user) { create(:user, email: "billyboy@eyeklaud.com") } let(:user) { create(:user, email: "billyboy@eyeklaud.com") }
let(:headers) { csv.first }
it "does not include certain attributes in the headers" do it "does not include certain attributes in the headers" do
expect(headers).not_to include(*%w[address_line1_as_entered address_line2_as_entered town_or_city_as_entered county_as_entered postcode_full_as_entered la_as_entered created_by value_value_check monthly_charges_value_check]) expect(attribute_line).not_to include(*%w[address_line1_as_entered address_line2_as_entered town_or_city_as_entered county_as_entered postcode_full_as_entered la_as_entered created_by value_value_check monthly_charges_value_check])
end end
context "and the requested form is 2024" do context "and the requested form is 2024" do
@ -329,8 +340,8 @@ RSpec.describe Csv::SalesLogCsvService do
expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_non_support_labels_24.csv") expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_non_support_labels_24.csv")
values_to_delete = %w[id] values_to_delete = %w[id]
values_to_delete.each do |attribute| values_to_delete.each do |attribute|
index = csv.first.index(attribute) index = attribute_line.index(attribute)
csv.second[index] = nil content_line[index] = nil
end end
expect(csv).to eq expected_content expect(csv).to eq expected_content
end end

Loading…
Cancel
Save