Merge pull request #2474 from Matt-Yorkley/pi/import_options

PI: import options UI
This commit is contained in:
Maikel
2018-07-24 10:34:14 +10:00
committed by GitHub
12 changed files with 123 additions and 310 deletions

View File

@@ -195,6 +195,39 @@ feature "Product Import", js: true do
expect(page).to_not have_field "product_name", with: product2.name
end
it "can reset product stock to zero for products not present in the CSV" do
csv_data = CSV.generate do |csv|
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type"]
csv << ["Carrots", "User Enterprise", "Vegetables", "500", "3.20", "500", "g"]
end
File.write('/tmp/test.csv', csv_data)
visit main_app.admin_product_import_path
attach_file 'file', '/tmp/test.csv'
check "settings_reset_all_absent"
click_button 'Upload'
expect(page).to have_selector 'a.button.proceed', visible: true
click_link 'Proceed'
import_data
expect(page).to have_selector 'a.button.proceed', visible: true
click_link 'Proceed'
save_data
expect(page).to have_selector '.created-count', text: '1'
expect(page).to have_selector '.reset-count', text: '3'
expect(Spree::Product.find_by_name('Carrots').on_hand).to eq 500
expect(Spree::Product.find_by_name('Cabbage').on_hand).to eq 0
expect(Spree::Product.find_by_name('Beans').on_hand).to eq 0
end
it "can import items into inventory" do
csv_data = CSV.generate do |csv|
csv << ["name", "supplier", "producer", "category", "on_hand", "price", "units"]
@@ -205,14 +238,10 @@ feature "Product Import", js: true do
File.write('/tmp/test.csv', csv_data)
visit main_app.admin_product_import_path
select2_select I18n.t('admin.product_import.index.inventories'), from: "settings_import_into"
attach_file 'file', '/tmp/test.csv'
click_button 'Upload'
within 'div.import-settings' do
find('div.header-description').click # Import settings tab
select 'Inventories', from: "settings_#{enterprise2.id.to_s}_import_into", visible: false
end
expect(page).to have_selector 'a.button.proceed', visible: true
click_link 'Proceed'
@@ -259,76 +288,6 @@ feature "Product Import", js: true do
expect(page).to have_content 'Cabbage'
end
end
it "can override import fields via the import settings tab" do
csv_data = CSV.generate do |csv|
csv << ["name", "supplier", "category", "on_hand", "price", "units", "unit_type", "tax_category", "shipping_category"]
csv << ["Carrots", "User Enterprise", "Vegetables", "5", "3.20", "500", "g", tax_category.name, shipping_category.name]
csv << ["Pumpkin", "User Enterprise", "Vegetables", "3", "3.50", "1", "kg", tax_category.name, ""]
csv << ["Spinach", "User Enterprise", "Vegetables", "7", "3.60", "1", "kg", "", shipping_category.name]
end
File.write('/tmp/test.csv', csv_data)
visit main_app.admin_product_import_path
expect(page).to have_content "Select a spreadsheet to upload"
attach_file 'file', '/tmp/test.csv'
click_button 'Upload'
within 'div.import-settings' do
find('div.panel-header').click
within 'tr.stock-level.productlist' do
find('input[type="checkbox"]').click
select 'Overwrite all', from: "settings_#{enterprise.id}_defaults_on_hand_mode", visible: false
fill_in "settings_#{enterprise.id}_defaults_on_hand_value", with: 9000
end
within 'tr.tax-category' do
find('input[type="checkbox"]').click
select 'Overwrite if empty', from: "settings_#{enterprise.id}_defaults_tax_category_id_mode", visible: false
select tax_category2.name, from: "settings_#{enterprise.id}_defaults_tax_category_id_value", visible: false
end
within 'tr.shipping-category' do
find('input[type="checkbox"]').click
select 'Overwrite all', from: "settings_#{enterprise.id}_defaults_shipping_category_id_mode", visible: false
select shipping_category.name, from: "settings_#{enterprise.id}_defaults_shipping_category_id_value", visible: false
end
end
expect(page).to have_selector 'a.button.proceed', visible: true
click_link 'Proceed'
import_data
expect(page).to have_selector '.item-count', text: "3"
expect(page).to have_selector '.create-count', text: "3"
expect(page).to_not have_selector '.update-count'
expect(page).to have_selector 'a.button.proceed', visible: true
click_link 'Proceed'
save_data
expect(page).to have_selector '.created-count', text: '3'
expect(page).to_not have_selector '.updated-count'
carrots = Spree::Product.find_by_name('Carrots')
expect(carrots.tax_category).to eq tax_category
expect(carrots.shipping_category).to eq shipping_category
expect(carrots.count_on_hand).to eq 9000
pumpkin = Spree::Product.find_by_name('Pumpkin')
expect(pumpkin.tax_category).to eq tax_category
expect(pumpkin.shipping_category).to eq shipping_category
expect(pumpkin.count_on_hand).to eq 9000
spinach = Spree::Product.find_by_name('Spinach')
expect(spinach.tax_category).to eq tax_category2
expect(spinach.shipping_category).to eq shipping_category
expect(spinach.count_on_hand).to eq 9000
end
end
describe "when dealing with uploaded files" do

View File

@@ -44,7 +44,7 @@ describe ProductImport::ProductImporter do
end
File.write('/tmp/test-m.csv', csv_data)
file = File.new('/tmp/test-m.csv')
settings = {enterprise.id.to_s => {'import_into' => 'product_list'}}
settings = {'import_into' => 'product_list'}
@importer = ProductImport::ProductImporter.new(file, admin, start: 1, end: 100, settings: settings)
end
after { File.delete('/tmp/test-m.csv') }
@@ -131,7 +131,7 @@ describe ProductImport::ProductImporter do
end
File.write('/tmp/test-m.csv', csv_data)
file = File.new('/tmp/test-m.csv')
settings = {enterprise.id.to_s => {'import_into' => 'product_list'}}
settings = {'import_into' => 'product_list'}
@importer = ProductImport::ProductImporter.new(file, admin, start: 1, end: 100, settings: settings)
end
after { File.delete('/tmp/test-m.csv') }
@@ -303,7 +303,7 @@ describe ProductImport::ProductImporter do
end
File.write('/tmp/test-m.csv', csv_data)
file = File.new('/tmp/test-m.csv')
settings = {enterprise2.id.to_s => {'import_into' => 'inventories'}}
settings = {'import_into' => 'inventories'}
@importer = ProductImport::ProductImporter.new(file, admin, start: 1, end: 100, settings: settings)
end
after { File.delete('/tmp/test-m.csv') }
@@ -341,54 +341,6 @@ describe ProductImport::ProductImporter do
end
end
describe "importing items into inventory and product list simultaneously" do
before do
csv_data = CSV.generate do |csv|
csv << ["name", "supplier", "producer", "category", "on_hand", "price", "units", "unit_type"]
csv << ["Beans", "Another Enterprise", "User Enterprise", "Vegetables", "5", "3.20", "500", ""]
csv << ["Sprouts", "Another Enterprise", "User Enterprise", "Vegetables", "6", "6.50", "500", ""]
csv << ["Garbanzos", "User Enterprise", "", "Vegetables", "2001", "1.50", "500", "g"]
end
File.write('/tmp/test-m.csv', csv_data)
file = File.new('/tmp/test-m.csv')
settings = {enterprise.id.to_s => {'import_into' => 'product_list'}, enterprise2.id.to_s => {'import_into' => 'inventories'}}
@importer = ProductImport::ProductImporter.new(file, admin, start: 1, end: 100, settings: settings)
end
after { File.delete('/tmp/test-m.csv') }
it "validates entries" do
@importer.validate_entries
entries = JSON.parse(@importer.entries_json)
expect(filter('valid', entries)).to eq 3
expect(filter('invalid', entries)).to eq 0
expect(filter('create_inventory', entries)).to eq 2
expect(filter('create_product', entries)).to eq 1
end
it "saves and updates inventory" do
@importer.save_entries
expect(@importer.inventory_created_count).to eq 2
expect(@importer.products_created_count).to eq 1
expect(@importer.updated_ids).to be_a(Array)
expect(@importer.updated_ids.count).to eq 3
beans_override = VariantOverride.where(variant_id: product2.variants.first.id, hub_id: enterprise2.id).first
sprouts_override = VariantOverride.where(variant_id: product3.variants.first.id, hub_id: enterprise2.id).first
garbanzos = Spree::Product.where(name: "Garbanzos").first
expect(Float(beans_override.price)).to eq 3.20
expect( beans_override.count_on_hand).to eq 5
expect(Float(sprouts_override.price)).to eq 6.50
expect(sprouts_override.count_on_hand).to eq 6
expect(Float(garbanzos.price)).to eq 1.50
expect(garbanzos.count_on_hand).to eq 2001
end
end
describe "handling enterprise permissions" do
after { File.delete('/tmp/test-m.csv') }
@@ -400,7 +352,7 @@ describe ProductImport::ProductImporter do
end
File.write('/tmp/test-m.csv', csv_data)
file = File.new('/tmp/test-m.csv')
settings = {enterprise.id.to_s => {'import_into' => 'product_list'}, enterprise2.id.to_s => {'import_into' => 'product_list'}}
settings = {'import_into' => 'product_list'}
@importer = ProductImport::ProductImporter.new(file, user, start: 1, end: 100, settings: settings)
@importer.validate_entries
@@ -427,7 +379,7 @@ describe ProductImport::ProductImporter do
end
File.write('/tmp/test-m.csv', csv_data)
file = File.new('/tmp/test-m.csv')
settings = {enterprise2.id.to_s => {'import_into' => 'inventories'}}
settings = {'import_into' => 'inventories'}
@importer = ProductImport::ProductImporter.new(file, user2, start: 1, end: 100, settings: settings)
@importer.validate_entries
@@ -455,7 +407,7 @@ describe ProductImport::ProductImporter do
end
File.write('/tmp/test-m.csv', csv_data)
file = File.new('/tmp/test-m.csv')
settings = {enterprise.id.to_s => {'import_into' => 'inventories'}}
settings = {'import_into' => 'inventories'}
@importer = ProductImport::ProductImporter.new(file, user2, start: 1, end: 100, settings: settings)
@importer.validate_entries
@@ -484,7 +436,7 @@ describe ProductImport::ProductImporter do
end
File.write('/tmp/test-m.csv', csv_data)
file = File.new('/tmp/test-m.csv')
settings = {enterprise.id.to_s => {'import_into' => 'product_list', 'reset_all_absent' => true}}
settings = {'import_into' => 'product_list', 'reset_all_absent' => true}
@importer = ProductImport::ProductImporter.new(file, admin, start: 1, end: 100, settings: settings)
@importer.validate_entries
@@ -502,7 +454,9 @@ describe ProductImport::ProductImporter do
expect(@importer.updated_ids).to be_a(Array)
expect(@importer.updated_ids.count).to eq 2
@importer.reset_absent(@importer.updated_ids)
updated_ids = @importer.updated_ids
@importer = ProductImport::ProductImporter.new(file, admin, start: 1, end: 100, updated_ids: updated_ids, enterprises_to_reset: [enterprise.id], settings: settings)
@importer.reset_absent(updated_ids)
expect(@importer.products_reset_count).to eq 2
@@ -521,7 +475,7 @@ describe ProductImport::ProductImporter do
end
File.write('/tmp/test-m.csv', csv_data)
file = File.new('/tmp/test-m.csv')
settings = {enterprise2.id.to_s => {'import_into' => 'inventories', 'reset_all_absent' => true}}
settings = {'import_into' => 'inventories', 'reset_all_absent' => true}
@importer = ProductImport::ProductImporter.new(file, admin, start: 1, end: 100, settings: settings)
@importer.validate_entries
@@ -537,7 +491,9 @@ describe ProductImport::ProductImporter do
expect(@importer.updated_ids).to be_a(Array)
expect(@importer.updated_ids.count).to eq 2
@importer.reset_absent(@importer.updated_ids)
updated_ids = @importer.updated_ids
@importer = ProductImport::ProductImporter.new(file, admin, start: 1, end: 100, updated_ids: updated_ids, enterprises_to_reset: [enterprise2.id], settings: settings)
@importer.reset_absent(updated_ids)
# expect(@importer.products_reset_count).to eq 1
@@ -614,53 +570,6 @@ describe ProductImport::ProductImporter do
expect(potatoes.shipping_category_id).to eq shipping_category.id
expect(potatoes.available_on).to be_within(1.day).of(Time.zone.local(2020, 1, 1))
end
it "can overwrite fields with selected defaults when importing to inventory" do
csv_data = CSV.generate do |csv|
csv << ["name", "producer", "supplier", "on_hand", "price", "units", "unit_type"]
csv << ["Beans", "User Enterprise", "Another Enterprise", "", "3.20", "500", "g"]
csv << ["Sprouts", "User Enterprise", "Another Enterprise", "7", "6.50", "500", "g"]
csv << ["Cabbage", "User Enterprise", "Another Enterprise", "", "1.50", "500", "g"]
end
File.write('/tmp/test-m.csv', csv_data)
file = File.new('/tmp/test-m.csv')
import_settings = {enterprise2.id.to_s => {
'import_into' => 'inventories',
'defaults' => {
'count_on_hand' => {
'active' => true,
'mode' => 'overwrite_empty',
'value' => '9000'
}
}
}}
@importer = ProductImport::ProductImporter.new(file, admin, start: 1, end: 100, import_into: 'inventories', settings: import_settings)
@importer.validate_entries
entries = JSON.parse(@importer.entries_json)
expect(filter('valid', entries)).to eq 3
expect(filter('invalid', entries)).to eq 0
expect(filter('create_inventory', entries)).to eq 2
expect(filter('update_inventory', entries)).to eq 1
@importer.save_entries
expect(@importer.inventory_created_count).to eq 2
expect(@importer.inventory_updated_count).to eq 1
expect(@importer.updated_ids).to be_a(Array)
expect(@importer.updated_ids.count).to eq 3
beans_override = VariantOverride.where(variant_id: product2.variants.first.id, hub_id: enterprise2.id).first
sprouts_override = VariantOverride.where(variant_id: product3.variants.first.id, hub_id: enterprise2.id).first
cabbage_override = VariantOverride.where(variant_id: product4.variants.first.id, hub_id: enterprise2.id).first
expect(beans_override.count_on_hand).to eq 9000
expect(sprouts_override.count_on_hand).to eq 7
expect(cabbage_override.count_on_hand).to eq 9000
end
end
end