From 5415fa2db85ca30758c24c9eea940a52a262646f Mon Sep 17 00:00:00 2001 From: Ana Nunes da Silva Date: Tue, 26 Mar 2024 10:45:38 +0000 Subject: [PATCH] Fix offense constant definition in block in import_product_images_rake.rb Add a test to import product images rake --- .rubocop_todo.yml | 1 - lib/tasks/import_product_images.rake | 6 +- .../tasks/import_product_images_rake_spec.rb | 84 +++++++++++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 spec/lib/tasks/import_product_images_rake_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5709efec0e..62292766ca 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -11,7 +11,6 @@ # AllowedMethods: enums Lint/ConstantDefinitionInBlock: Exclude: - - 'lib/tasks/import_product_images.rake' - 'lib/tasks/users.rake' - 'spec/controllers/spree/admin/base_controller_spec.rb' - 'spec/helpers/serializer_helper_spec.rb' diff --git a/lib/tasks/import_product_images.rake b/lib/tasks/import_product_images.rake index e82d1e55d1..6b1ab14741 100644 --- a/lib/tasks/import_product_images.rake +++ b/lib/tasks/import_product_images.rake @@ -4,15 +4,15 @@ namespace :ofn do namespace :import do desc "Importing images for products from CSV" task :product_images, [:filename] => [:environment] do |_task, args| - COLUMNS = [:producer, :name, :image_url].freeze - puts "Warning: use only with trusted URLs. This script will download whatever it can, " \ "including local secrets, and expose the file as an image file." raise "Filename required" if args[:filename].blank? + columns = %i[producer name image_url].freeze + csv = CSV.read(args[:filename], headers: true, header_converters: :symbol) - raise "CSV columns reqired: #{COLUMNS.map(&:to_s)}" if (COLUMNS - csv.headers).present? + raise "CSV columns reqired: #{columns.map(&:to_s)}" if (columns - csv.headers).present? csv.each.with_index do |entry, index| puts "#{index} #{entry[:producer]}, #{entry[:name]}" diff --git a/spec/lib/tasks/import_product_images_rake_spec.rb b/spec/lib/tasks/import_product_images_rake_spec.rb new file mode 100644 index 0000000000..bfb6dcd571 --- /dev/null +++ b/spec/lib/tasks/import_product_images_rake_spec.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'rake' + +RSpec.describe 'ofn:import:product_images' do + before do + Rake.application.load_rakefile + Rake::Task.define_task(:environment) + Rake::Task['ofn:import:product_images'].reenable + end + + after do + Rake::Task['ofn:import:product_images'].clear + end + + describe 'task' do + context "filename is blank" do + it 'raises an error' do + expect { + Rake::Task['ofn:import:product_images'].invoke('') + }.to raise_error(RuntimeError, + 'Filename required') + end + end + + context "invalid CSV format" do + it 'raises an error if CSV columns are missing' do + allow(CSV).to receive(:read).and_return(CSV::Table.new([])) + Rake::Task['ofn:import:product_images'].reenable + + expect { + Rake::Task['ofn:import:product_images'].invoke('path/to/csv/file.csv') + }.to raise_error(RuntimeError, 'CSV columns reqired: ["producer", "name", "image_url"]') + end + end + + context "valid CSV" do + it 'imports images for each product in the CSV that exists and does not have images' do + filename = 'path/to/csv/file.csv' + + csv_data = [ + { producer: 'Producer 1', name: 'Product 1', image_url: 'http://example.com/image1.jpg' }, + { producer: 'Producer 2', name: 'Product 2', image_url: 'http://example.com/image2.jpg' }, + { producer: 'Producer 3', name: 'Product 3', image_url: 'http://example.com/image3.jpg' } + ] + + csv_rows = csv_data.map do |hash| + CSV::Row.new(hash.keys, hash.values) + end + + csv_table = CSV::Table.new(csv_rows) + + allow(CSV).to receive(:read).and_return(csv_table) + + allow(Enterprise).to receive(:find_by!).with(name: 'Producer 1').and_return(double) + allow(Enterprise).to receive(:find_by!).with(name: 'Producer 2').and_return(double) + allow(Enterprise).to receive(:find_by!).with(name: 'Producer 3').and_return(double) + + allow(Spree::Product).to receive(:where).and_return( + class_double('Spree::Product', first: nil), + class_double('Spree::Product', first: instance_double('Spree::Product', image: nil)), + class_double('Spree::Product', first: instance_double('Spree::Product', image: true)) + ) + + allow_any_instance_of(ImageImporter).to receive(:import).and_return(true) + + expected_output = <<~OUTPUT + Warning: use only with trusted URLs. This script will download whatever it can, including local secrets, and expose the file as an image file. + 0 Producer 1, Product 1 + product not found. + 1 Producer 2, Product 2 + image added. + 2 Producer 3, Product 3 + image exists, not updated. + OUTPUT + + expect { + Rake::Task['ofn:import:product_images'].invoke('path/to/csv/file.csv') + }.to output(expected_output).to_stdout + end + end + end +end