Skip to content

Commit

Permalink
[DL-82] Adding specs associated to task for import HS codes from flow…
Browse files Browse the repository at this point in the history
… using CSV file.
  • Loading branch information
sebastiandl committed Mar 22, 2021
1 parent d9be864 commit 11cd126
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 3 deletions.
44 changes: 44 additions & 0 deletions app/models/concerns/csv_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

class CSVUploader
def initialize(file)
timestamp = Time.current.strftime('%Y%m%d%H%M%S')
@filename = "#{timestamp}_#{file.original_filename}"
@file_content = CSV.parse(file.read)
end

def self.upload_csv(file_path, title, rows_content)
DIRECTORY.files.create(
key: file_path,
body: create_csv(title, rows_content),
public: false
)
end

def self.download_url(file_path, filename)
DIRECTORY.files.head(file_path).url(
Time.now.to_i + 18_000,
query: { "response-content-disposition": "attachment; filename=#{filename}.csv" }
)
end

def self.create_csv(title, rows_content)
CSV.generate do |csv|
csv << Array(title)
rows_content.each do |row_content|
csv << Array(row_content)
end
end
end
private_class_method :method

def self.file_csv?(file)
['text/csv', 'application/vnd.ms-excel'].include?(file.content_type) &&
File.extname(file.original_filename.downcase) == '.csv'
end

def upload(base_file_path)
CSVUploader.upload_csv("#{base_file_path}/#{@filename}", @file_content[0], @file_content[1..-1])
@filename
end
end
3 changes: 0 additions & 3 deletions app/services/flowcommerce_spree/import_items_hs_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ def run
@logger.info log_str
end

# @logger.info "\nUpdating Fulfil: #{@experience_key.green}"
# VariantService.new.update_classification

@logger.info "\nData for #{total.to_s.green} products was imported."
end

Expand Down
31 changes: 31 additions & 0 deletions lib/tasks/spree_variant.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

namespace :spree_variant do
desc 'Import Flow Hs Codes from CSV'
task import_flow_hs_code_from_csv: :environment do |t|
s3_file_path = CSVUploader.download_url('script/flow_hs_codes.csv', 'flow_hs_code')
csv = CSV.new(URI.parse(s3_file_path).open, headers: true, col_sep: ',')

not_found = []
updated_count = []

csv.each do |row|
hs_code = row['hs6']
next if hs_code.blank?

sku = row['item_number']
next not_found << sku unless (variant = Spree::Variant.find_by(sku: row['item_number']))

variant.flow_data ||= {}
variant.flow_data['hs_code'] = hs_code
variant.update_column(:meta, variant.meta.to_json)
updated_count << sku
end

puts "\n#{Time.zone.now} | Not found in the DB #{not_found.size}."
puts not_found.inspect

puts "\n#{Time.zone.now} | Updated #{updated_count.size}."
puts "Updated #{updated_count} variants."
end
end
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require 'support/database_cleaner.rb'
require 'support/flow.rb'
require 'support/controller_requests.rb'
require 'support/tasks.rb'

# Add additional requires below this line. Rails is not loaded until this point!

Expand Down
35 changes: 35 additions & 0 deletions spec/support/tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'rake'

# Task names should be used in the top-level describe, with an optional
# "rake "-prefix for better documentation. Both of these will work:
#
# 1) describe "foo:bar" do ... end
#
# 2) describe "rake foo:bar" do ... end
#
# Favor including "rake "-prefix as in the 2nd example above as it produces
# doc output that makes it clear a rake task is under test and how it is
# invoked.
module TaskFormat
extend ActiveSupport::Concern

included do
let(:task_name) { self.class.top_level_description.sub(/\Arake /, '') }
let(:tasks) { Rake::Task } # Make the Rake task available as `task` in your examples:
subject(:task) { tasks[task_name] }
end
end

RSpec.configure do |config| # Tag Rake specs with `:task` metadata or put them in the spec/tasks dir
config.define_derived_metadata(file_path: %r{/spec/tasks/}) do |metadata|
metadata[:type] = :task
end

config.include TaskFormat, type: :task

config.before(:suite) do
Rails.application.load_tasks
end
end
43 changes: 43 additions & 0 deletions spec/tasks/spree_variant_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require 'rails_helper'
require 'csv'
# require 'tasks/tasks_helper'

describe 'rake products:turn_on_version_for_region', type: :task do
let(:variant) { create(:base_variant, :with_flow_data) }

describe 'import_flow_hs_code_from_csv' do
let(:hs_code) { '711319' }
let(:stubed_csv_content) { [variant.sku, hs_code, variant.product.id, variant.product.name] }
let(:stubed_csv) { CSV.new("item_number,hs6,product_id,item_name\n#{stubed_csv_content.join(',')}", headers: true) }
let(:run_codes_rake_task) do
Rake::Task['spree_variant:import_flow_hs_code_from_csv'].reenable
Rake.application.invoke_task('spree_variant:import_flow_hs_code_from_csv')
end

before(:each) do
allow(CSVUploader).to(receive(:download_url).and_return('https://s3.amazonaws.com/test/script/flow_hs_codes.csv'))
allow_any_instance_of(URI::HTTPS).to(receive(:open))

# stubed_csv = CSV.new("#{title.join(',')}\n#{content.join(',')}", headers: true)
allow(CSV).to(receive(:new).and_return(stubed_csv))
end

it 'updates variant`s flow data with the hs_code' do
expect(variant.flow_data['hs_code']).to(be_blank)
run_codes_rake_task
expect(variant.reload.flow_data['hs_code']).to(eq(hs_code))
end

context 'when csv does not have hs_code from flow' do
let(:stubed_csv_content) { [variant.sku, '', variant.product.id, variant.product.name] }

it 'does not update variant`s flow hs_code' do
expect(variant.flow_data['hs_code']).to(be_blank)
run_codes_rake_task
expect(variant.reload.flow_data['hs_code']).to(be_blank)
end
end
end
end

0 comments on commit 11cd126

Please sign in to comment.