Fix offense constant definition in block in import_product_images_rake.rb

Add a test to import product images rake
This commit is contained in:
Ana Nunes da Silva
2024-03-26 10:45:38 +00:00
parent f4cc5d5917
commit 5415fa2db8
3 changed files with 87 additions and 4 deletions

View File

@@ -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'

View File

@@ -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]}"

View File

@@ -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