From 2a34e2bb42f45421d9e59de58c8f976e81538aec Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 2 Aug 2023 17:11:11 +0100 Subject: [PATCH] Call ImportReportService from rake task --- app/services/imports/import_report_service.rb | 8 ++-- lib/tasks/full_import.rake | 5 ++ spec/lib/tasks/full_import_spec.rb | 47 +++++++++++++++++++ .../imports/import_report_service_spec.rb | 8 ++-- 4 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 spec/lib/tasks/full_import_spec.rb diff --git a/app/services/imports/import_report_service.rb b/app/services/imports/import_report_service.rb index c4fe5a1b2..36e545710 100644 --- a/app/services/imports/import_report_service.rb +++ b/app/services/imports/import_report_service.rb @@ -8,11 +8,11 @@ module Imports BYTE_ORDER_MARK = "\uFEFF".freeze # Required to ensure Excel always reads CSV as UTF-8 - def create_report(report_directory) - write_missing_data_coordinators_report(report_directory) + def create_report(report_suffix) + generate_missing_data_coordinators_report(report_suffix) end - def write_missing_data_coordinators_report(report_directory) + def generate_missing_data_coordinators_report(report_suffix) csv_string = "Organisation ID,Old Organisation ID,Organisation Name\n" @old_organisation_ids.each do |old_organisation_id| organisation = Organisation.find_by(old_visible_id: old_organisation_id) @@ -21,7 +21,7 @@ module Imports end end - @storage_service.write_file("#{report_directory}/organisations_without_data_coordinators.csv", BYTE_ORDER_MARK + csv_string) + @storage_service.write_file("OrganisationsWithoutDataCoordinators_#{report_suffix}.csv", BYTE_ORDER_MARK + csv_string) end end end diff --git a/lib/tasks/full_import.rake b/lib/tasks/full_import.rake index 780fe46fc..b0b91c379 100644 --- a/lib/tasks/full_import.rake +++ b/lib/tasks/full_import.rake @@ -36,6 +36,8 @@ namespace :import do end Rails.logger.info("Finished initial imports") + reports_service = Imports::ImportReportService.new(s3_service, csv.map { |row| row[1] }) + Rails.logger.info("Running report generation") end desc "Run logs import steps" @@ -123,6 +125,9 @@ namespace :import do report_name = "MigratedLogsReport_#{institutions_csv_name}" s3_service.write_file(report_name, rep) + old_organisation_ids = csv.map { |row| Organisation.find_by(name: row[0]).old_visible_id }.compact + Imports::ImportReportService.new(s3_service, old_organisation_ids).generate_missing_data_coordinators_report(institutions_csv_name) + Rails.logger.info("Logs report available in s3 import bucket at #{report_name}") end diff --git a/spec/lib/tasks/full_import_spec.rb b/spec/lib/tasks/full_import_spec.rb new file mode 100644 index 000000000..142a50a29 --- /dev/null +++ b/spec/lib/tasks/full_import_spec.rb @@ -0,0 +1,47 @@ +require "rails_helper" +require "rake" + +describe "full import", type: :task do + let(:instance_name) { "paas_import_instance" } + let(:paas_config_service) { instance_double(Configuration::PaasConfigurationService) } + let(:storage_service) { instance_double(Storage::S3Service) } + let(:orgs_list) { "Institution name,Id,Old Completed lettings logs,Old In progress lettings logs,Old Completed sales logs,Old In progress sales logs\norg1,1.zip,0,0,0,0\norg2,2.zip,0,0,0,0" } + + before do + allow(Storage::S3Service).to receive(:new).and_return(storage_service) + allow(storage_service).to receive(:write_file).and_return(nil) + allow(storage_service).to receive(:get_file_io).and_return(orgs_list) + allow(Configuration::PaasConfigurationService).to receive(:new).and_return(paas_config_service) + allow(ENV).to receive(:[]) + allow(ENV).to receive(:[]).with("IMPORT_PAAS_INSTANCE").and_return(instance_name) + end + + describe "import:generate_report" do + subject(:task) { Rake::Task["import:generate_report"] } + + before do + Rake.application.rake_require("tasks/full_import") + Rake::Task.define_task(:environment) + task.reenable + + create(:organisation, old_visible_id: 1, name: "org1") + create(:organisation, old_visible_id: 2, name: "org2") + end + + context "when generating report" do + let(:import_report_service) { instance_double(Imports::ImportReportService) } + + before do + allow(Imports::ImportReportService).to receive(:new).and_return(import_report_service) + end + + it "creates an organisation from the given XML file" do + expect(Storage::S3Service).to receive(:new).with(paas_config_service, instance_name) + expect(Imports::ImportReportService).to receive(:new).with(storage_service, %w[1 2]) + expect(import_report_service).to receive(:generate_missing_data_coordinators_report).with("some_name") + + task.invoke("some_name") + end + end + end +end diff --git a/spec/services/imports/import_report_service_spec.rb b/spec/services/imports/import_report_service_spec.rb index 53416d8c9..dd50bde2e 100644 --- a/spec/services/imports/import_report_service_spec.rb +++ b/spec/services/imports/import_report_service_spec.rb @@ -14,9 +14,9 @@ RSpec.describe Imports::ImportReportService do end it "writes an empty organisations without a data coordinators report" do - expect(storage_service).to receive(:write_file).with("report_directory/organisations_without_data_coordinators.csv", "\uFEFFOrganisation ID,Old Organisation ID,Organisation Name\n") + expect(storage_service).to receive(:write_file).with("OrganisationsWithoutDataCoordinators_report_suffix.csv", "\uFEFFOrganisation ID,Old Organisation ID,Organisation Name\n") - report_service.create_report("report_directory") + report_service.create_report("report_suffix") end end @@ -31,9 +31,9 @@ RSpec.describe Imports::ImportReportService do end it "writes an empty organisations without a data coordinators report" do - expect(storage_service).to receive(:write_file).with("report_directory/organisations_without_data_coordinators.csv", "\uFEFFOrganisation ID,Old Organisation ID,Organisation Name\n#{organisation2.id},2,#{organisation2.name}\n#{organisation3.id},3,#{organisation3.name}\n") + expect(storage_service).to receive(:write_file).with("OrganisationsWithoutDataCoordinators_report_suffix.csv", "\uFEFFOrganisation ID,Old Organisation ID,Organisation Name\n#{organisation2.id},2,#{organisation2.name}\n#{organisation3.id},3,#{organisation3.name}\n") - report_service.create_report("report_directory") + report_service.create_report("report_suffix") end end end