diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cac1468c67..0e938c3ff4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -40,7 +40,7 @@ Push your changes to a branch on your fork: ## Submitting a Pull Request -Use the GitHub UI to submit a [new pull request][pr] against upstream/master. To increase the chances that your pull request is swiftly accepted please have a look at our guide to [making a great pull request][great-pr]. +Use the GitHub UI to submit a [new pull request][pr] against upstream/master. To increase the chances that your pull request is swiftly accepted please have a look at our guide to [making a great pull request][great-pr]. TL;DR: * Write tests diff --git a/app/models/product_import/entry_validator.rb b/app/models/product_import/entry_validator.rb index f563b90cd0..34eed64fdf 100644 --- a/app/models/product_import/entry_validator.rb +++ b/app/models/product_import/entry_validator.rb @@ -253,7 +253,7 @@ module ProductImport products.flat_map(&:variants).each do |existing_variant| unit_scale = existing_variant.product.variant_unit_scale - unscaled_units = entry.unscaled_units || 0 + unscaled_units = entry.unscaled_units.to_f || 0 entry.unit_value = unscaled_units * unit_scale unless unit_scale.nil? if entry_matches_existing_variant?(entry, existing_variant) diff --git a/spec/features/admin/product_import_spec.rb b/spec/features/admin/product_import_spec.rb index 63d8e57035..d29cb6946b 100644 --- a/spec/features/admin/product_import_spec.rb +++ b/spec/features/admin/product_import_spec.rb @@ -297,6 +297,31 @@ feature "Product Import", js: true do end end + it "handles a unit of kg for inventory import" do + product = create(:simple_product, supplier: enterprise, on_hand: 100, name: 'Beets', unit_value: '1000', variant_unit_scale: 1000) + csv_data = CSV.generate do |csv| + csv << ["name", "distributor", "producer", "category", "on_hand", "price", "unit_type", "units", "on_demand"] + csv << ["Beets", "Another Enterprise", "User Enterprise", "Vegetables", nil, "3.20", "kg", "1", "true"] + end + + 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' + + proceed_to_validation + + expect(page).to have_selector '.item-count', text: "1" + expect(page).to have_no_selector '.invalid-count' + expect(page).to have_selector '.inv-create-count', text: '1' + + save_data + + expect(page).to have_selector '.inv-created-count', text: '1' + end + it "handles on_demand and on_hand validations with inventory" do csv_data = CSV.generate do |csv| csv << ["name", "distributor", "producer", "category", "on_hand", "price", "units", "on_demand"] diff --git a/spec/models/product_import/entry_validator_spec.rb b/spec/models/product_import/entry_validator_spec.rb new file mode 100644 index 0000000000..ff8075f690 --- /dev/null +++ b/spec/models/product_import/entry_validator_spec.rb @@ -0,0 +1,116 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe ProductImport::EntryValidator do + let(:current_user) { double(:current_user) } + let(:import_time) { double(:import_time) } + let(:spreadsheet_data) { double(:spreadsheet_data) } + let(:editable_enterprises) { double(:editable_enterprises) } + let(:inventory_permissions) { double(:inventory_permissions) } + let(:reset_counts) { double(:reset_counts) } + let(:import_settings) { double(:import_settings) } + let(:all_entries) { double(:all_entries) } + + let(:entry_validator) do + described_class.new( + current_user, + import_time, + spreadsheet_data, + editable_enterprises, + inventory_permissions, + reset_counts, + import_settings, + all_entries + ) + end + + let(:enterprise) { create(:enterprise, name: "User Enterprise") } + + let(:entry_g) do + ProductImport::SpreadsheetEntry.new( + unscaled_units: "500", + units: "500", + unit_type: "g", + name: 'Tomato', + enterprise: enterprise, + enterprise_id: enterprise.id, + producer: enterprise, + producer_id: enterprise.id, + distributor: enterprise + ) + end + + let(:entry_kg) do + ProductImport::SpreadsheetEntry.new( + unscaled_units: "1", + units: "1", + unit_type: "kg", + name: 'Potatoes', + enterprise: enterprise, + enterprise_id: enterprise.id, + producer: enterprise, + producer_id: enterprise.id, + distributor: enterprise + ) + end + + describe "inventory validation" do + before do + allow(entry_validator).to receive(:import_into_inventory?) { true } + allow(entry_validator).to receive(:enterprise_validation) {} + allow(entry_validator).to receive(:producer_validation) {} + allow(entry_validator).to receive(:variant_of_product_validation) {} + end + + context "products exist" do + let!(:product_g) { + create( + :simple_product, + supplier: enterprise, + on_hand: '100', + name: 'Tomato', + unit_value: 500, + variant_unit_scale: 1, + variant_unit: 'weight' + ) + } + + let!(:product_kg) { + create( + :simple_product, + supplier: enterprise, + on_hand: '100', + name: 'Potatoes', + unit_value: 1000, + variant_unit_scale: 1000, + variant_unit: 'weight' + ) + } + + it "validates a spreadsheet entry in g" do + entries = [entry_g] + entry_validator.validate_all(entries) + expect(entries.first.errors.count).to eq(0) + end + + it "validates a spreadsheet entry in kg" do + entries = [entry_kg] + entry_validator.validate_all(entries) + expect(entries.first.errors.count).to eq(0) + end + end + + context "products do not exist" do + # stub + end + end + + describe "enterprise validation" do + # stub + end + + describe "producer_validation" do + # stub + end +end