From fd69c7672daf5e49554d039f0bc2e040bae69911 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 26 Sep 2018 11:38:22 +0200 Subject: [PATCH] Add specs for ResetAbsent strategies This also fixes the case where there are no overrides to exclude. --- app/models/product_import/inventory_reset.rb | 16 ++--- app/models/product_import/products_reset.rb | 20 +++--- .../product_import/inventory_reset_spec.rb | 70 +++++++++++++++++++ .../product_import/products_reset_spec.rb | 60 ++++++++++++++++ 4 files changed, 147 insertions(+), 19 deletions(-) create mode 100644 spec/models/product_import/inventory_reset_spec.rb create mode 100644 spec/models/product_import/products_reset_spec.rb diff --git a/app/models/product_import/inventory_reset.rb b/app/models/product_import/inventory_reset.rb index a03a008592..9aec385da4 100644 --- a/app/models/product_import/inventory_reset.rb +++ b/app/models/product_import/inventory_reset.rb @@ -2,9 +2,9 @@ module ProductImport class InventoryReset attr_reader :supplier_ids - def initialize(updated_ids) + def initialize(excluded_items_ids) @supplier_ids = [] - @updated_ids = updated_ids + @excluded_items_ids = excluded_items_ids end def <<(values) @@ -17,15 +17,13 @@ module ProductImport private - attr_reader :updated_ids + attr_reader :excluded_items_ids def relation - VariantOverride.where( - 'variant_overrides.hub_id IN (?) ' \ - 'AND variant_overrides.id NOT IN (?)', - supplier_ids, - updated_ids - ) + relation = VariantOverride.where(hub_id: supplier_ids) + return relation if excluded_items_ids.blank? + + relation.where('id NOT IN (?)', excluded_items_ids) end end end diff --git a/app/models/product_import/products_reset.rb b/app/models/product_import/products_reset.rb index 219b656f12..ad15283283 100644 --- a/app/models/product_import/products_reset.rb +++ b/app/models/product_import/products_reset.rb @@ -2,9 +2,9 @@ module ProductImport class ProductsReset attr_reader :supplier_ids - def initialize(updated_ids) + def initialize(excluded_items_ids) @supplier_ids = [] - @updated_ids = updated_ids + @excluded_items_ids = excluded_items_ids end def <<(values) @@ -17,19 +17,19 @@ module ProductImport private - attr_reader :updated_ids + attr_reader :excluded_items_ids def relation - Spree::Variant + relation = Spree::Variant .joins(:product) .where( - 'spree_products.supplier_id IN (?) ' \ - 'AND spree_variants.id NOT IN (?) ' \ - 'AND spree_variants.is_master = false ' \ - 'AND spree_variants.deleted_at IS NULL', - supplier_ids, - updated_ids + spree_products: { supplier_id: supplier_ids }, + spree_variants: { is_master: false, deleted_at: nil } ) + + return relation if excluded_items_ids.blank? + + relation.where('spree_variants.id NOT IN (?)', excluded_items_ids) end end end diff --git a/spec/models/product_import/inventory_reset_spec.rb b/spec/models/product_import/inventory_reset_spec.rb new file mode 100644 index 0000000000..412739ae10 --- /dev/null +++ b/spec/models/product_import/inventory_reset_spec.rb @@ -0,0 +1,70 @@ +require 'spec_helper' + +describe ProductImport::InventoryReset do + let(:inventory_reset) { described_class.new(excluded_items_ids) } + + describe '#<<' do + let(:excluded_items_ids) { [] } + let(:supplier_ids) { 1 } + + it 'stores the specified supplier_ids' do + inventory_reset << supplier_ids + expect(inventory_reset.supplier_ids).to eq([supplier_ids]) + end + end + + describe '#reset' do + let(:supplier_ids) { enterprise.id } + let(:enterprise) { variant.product.supplier } + let(:variant) { create(:variant) } + + let!(:variant_override) do + create( + :variant_override, + count_on_hand: 10, + hub: enterprise, + variant: variant + ) + end + + before { inventory_reset << supplier_ids } + + context 'when there are excluded_items_ids' do + let(:excluded_items_ids) { [variant_override.id] } + + it 'does not update the count_on_hand of the excluded items' do + inventory_reset.reset + expect(variant_override.reload.count_on_hand).to eq(10) + end + + it 'updates the count_on_hand of the non-excluded items' do + non_excluded_variant_override = create( + :variant_override, + count_on_hand: 3, + hub: enterprise, + variant: variant + ) + inventory_reset.reset + expect(non_excluded_variant_override.reload.count_on_hand).to eq(0) + end + end + + context 'when there are no excluded_items_ids' do + let(:excluded_items_ids) { [] } + + it 'sets all count_on_hand to 0' do + inventory_reset.reset + expect(variant_override.reload.count_on_hand).to eq(0) + end + end + + context 'when excluded_items_ids is nil' do + let(:excluded_items_ids) { nil } + + it 'sets all count_on_hand to 0' do + inventory_reset.reset + expect(variant_override.reload.count_on_hand).to eq(0) + end + end + end +end diff --git a/spec/models/product_import/products_reset_spec.rb b/spec/models/product_import/products_reset_spec.rb new file mode 100644 index 0000000000..f84820ad81 --- /dev/null +++ b/spec/models/product_import/products_reset_spec.rb @@ -0,0 +1,60 @@ +require 'spec_helper' + +describe ProductImport::ProductsReset do + let(:products_reset) { described_class.new(excluded_items_ids) } + + describe '#<<' do + let(:excluded_items_ids) { [] } + let(:supplier_ids) { 1 } + + it 'stores the specified supplier_ids' do + products_reset << supplier_ids + expect(products_reset.supplier_ids).to eq([supplier_ids]) + end + end + + describe '#reset' do + let(:supplier_ids) { enterprise.id } + let(:enterprise) { variant.product.supplier } + let(:variant) { create(:variant, count_on_hand: 2) } + + before { products_reset << supplier_ids } + + context 'when there are excluded_items_ids' do + let(:excluded_items_ids) { [variant.id] } + + it 'does not update the count_on_hand of the excluded items' do + products_reset.reset + expect(variant.reload.count_on_hand).to eq(2) + end + + it 'updates the count_on_hand of the non-excluded items' do + non_excluded_variant = create( + :variant, + count_on_hand: 3, + product: variant.product + ) + products_reset.reset + expect(non_excluded_variant.reload.count_on_hand).to eq(0) + end + end + + context 'when there are no excluded_items_ids' do + let(:excluded_items_ids) { [] } + + it 'sets all count_on_hand to 0' do + products_reset.reset + expect(variant.reload.count_on_hand).to eq(0) + end + end + + context 'when excluded_items_ids is nil' do + let(:excluded_items_ids) { nil } + + it 'sets all count_on_hand to 0' do + products_reset.reset + expect(variant.reload.count_on_hand).to eq(0) + end + end + end +end