From ea8f5db75e48e6869bdcb04b8364797e30d8fbb4 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Thu, 17 Aug 2023 14:24:51 +0100 Subject: [PATCH] feat: rename and update tests --- lib/tasks/data_import_field.rake | 4 +- spec/lib/tasks/date_import_field_spec.rb | 74 +++++++++++++++++++++++- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/lib/tasks/data_import_field.rake b/lib/tasks/data_import_field.rake index 69a6479b5..d16ae752e 100644 --- a/lib/tasks/data_import_field.rake +++ b/lib/tasks/data_import_field.rake @@ -17,7 +17,7 @@ namespace :core do Rails.logger.info("Imported") end else - raise "Field #{field} cannot be updated by data_import_field" + raise "Field #{field} cannot be updated by lettings_data_import_field" end end @@ -39,7 +39,7 @@ namespace :core do Rails.logger.info("Imported") end else - raise "Field #{field} cannot be updated by data_import_field" + raise "Field #{field} cannot be updated by sales_data_import_field" end end end diff --git a/spec/lib/tasks/date_import_field_spec.rb b/spec/lib/tasks/date_import_field_spec.rb index c34427e04..2c39fb4a8 100644 --- a/spec/lib/tasks/date_import_field_spec.rb +++ b/spec/lib/tasks/date_import_field_spec.rb @@ -1,8 +1,8 @@ require "rails_helper" require "rake" -describe "rake core:data_import_field", type: :task do - subject(:task) { Rake::Task["core:data_import_field"] } +describe "rake core:lettings_data_import_field", type: :task do + subject(:task) { Rake::Task["core:lettings_data_import_field"] } let(:instance_name) { "paas_import_instance" } let(:storage_service) { instance_double(Storage::S3Service) } @@ -100,7 +100,75 @@ describe "rake core:data_import_field", type: :task do end it "raises an exception if the field is not supported" do - expect { task.invoke("random_field", "my_path") }.to raise_error("Field random_field cannot be updated by data_import_field") + expect { task.invoke("random_field", "my_path") }.to raise_error("Field random_field cannot be updated by lettings_data_import_field") + end + end +end + +describe "rake core:sales_data_import_field", type: :task do + subject(:task) { Rake::Task["core:sales_data_import_field"] } + + let(:instance_name) { "paas_import_instance" } + let(:storage_service) { instance_double(Storage::S3Service) } + let(:env_config_service) { instance_double(Configuration::EnvConfigurationService) } + let(:paas_config_service) { instance_double(Configuration::PaasConfigurationService) } + + before do + Rake.application.rake_require("tasks/data_import_field") + Rake::Task.define_task(:environment) + task.reenable + + allow(Storage::S3Service).to receive(:new).and_return(storage_service) + allow(Configuration::EnvConfigurationService).to receive(:new).and_return(env_config_service) + 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) + allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return("dummy") + allow(Imports::SalesLogsFieldImportService).to receive(:new).and_return(import_service) + end + + context "when importing a sales log field" do + let(:import_service) { instance_double(Imports::SalesLogsFieldImportService) } + let(:fixture_path) { "spec/fixtures/imports/sales_logs" } + let(:archive_service) { instance_double(Storage::ArchiveService) } + + before do + allow(import_service).to receive(:update_field) + allow(Storage::ArchiveService).to receive(:new).and_return(archive_service) + allow(archive_service).to receive(:folder_present?).with("logs").and_return(true) + end + + context "and we update the owning_organisation_id field" do + let(:field) { "owning_organisation_id" } + + it "updates the logs from the given XML file when the VCAP_SERVICES environment variable exists" do + expect(Storage::S3Service).to receive(:new).with(paas_config_service, instance_name) + expect(storage_service).to receive(:get_file_io).with("spec/fixtures/imports/sales_logs") + expect(Imports::SalesLogsFieldImportService).to receive(:new).with(archive_service) + expect(import_service).to receive(:update_field).with(field, "logs") + task.invoke(field, fixture_path) + end + + it "updates the logs from the given XML file when the VCAP_SERVICES environment variable does not exist" do + allow(ENV).to receive(:[]).with("VCAP_SERVICES") + expect(Storage::S3Service).to receive(:new).with(env_config_service, instance_name) + expect(storage_service).to receive(:get_file_io).with("spec/fixtures/imports/sales_logs") + expect(Imports::SalesLogsFieldImportService).to receive(:new).with(archive_service) + expect(import_service).to receive(:update_field).with(field, "logs") + task.invoke(field, fixture_path) + end + end + + it "raises an exception if no parameters are provided" do + expect { task.invoke }.to raise_error(/Usage/) + end + + it "raises an exception if a single parameter is provided" do + expect { task.invoke("one_parameter") }.to raise_error(/Usage/) + end + + it "raises an exception if the field is not supported" do + expect { task.invoke("random_field", "my_path") }.to raise_error("Field random_field cannot be updated by sales_data_import_field") end end end