<commit_before>require 'spec_helper'

describe ProductImport::ResetAbsent do
  let(:importer) { double(:importer) }
  let(:validator) { double(:validator) }
  let(:spreadsheet_data) { double(:spreadsheet_data) }
  let(:editable_enterprises) { double(:editable_enterprises) }
  let(:import_time) { double(:import_time) }
  let(:updated_ids) { double(:updated_ids) }
  let(:import_settings) { double(:import_settings) }

  let(:entry_processor) do
    ProductImport::EntryProcessor.new(
      importer,
      validator,
      import_settings,
      spreadsheet_data,
      editable_enterprises,
      import_time,
      updated_ids
    )
  end

  let(:reset_absent) { described_class.new(entry_processor, settings) }

  describe '#call' do
    context 'when there are no settings' do
      let(:settings) do
        instance_double(
          ProductImport::Settings,
          settings: nil,
          updated_ids: [],
          enterprises_to_reset: []
        )
      end

      it 'returns nil' do
        expect(reset_absent.call).to be_nil
      end
    end

    context 'when there are no updated_ids' do
      let(:settings) do
        instance_double(
          ProductImport::Settings,
          settings: [],
          updated_ids: nil,
          enterprises_to_reset: []
        )
      end

      it 'returns nil' do
        expect(reset_absent.call).to be_nil
      end
    end

    context 'when there are no enterprises_to_reset' do
      let(:settings) do
        instance_double(
          ProductImport::Settings,
          settings: [],
          updated_ids: [],
          enterprises_to_reset: nil
        )
      end

      it 'returns nil' do
        expect(reset_absent.call).to be_nil
      end
    end

    context 'when there are settings, updated_ids and enterprises_to_reset' do
      let(:settings) do
        instance_double(
          ProductImport::Settings,
          settings: { 'reset_all_absent' => true },
          updated_ids: [0],
          enterprises_to_reset: [enterprise.id.to_s]
        )
      end

      before do
        allow(entry_processor)
          .to receive(:permission_by_id?).with(enterprise.id.to_s) { true }
      end

      context 'and not importing into inventory' do
        let(:variant) { create(:variant) }
        let(:enterprise) { variant.product.supplier }

        before do
          allow(entry_processor)
            .to receive(:importing_into_inventory?) { false }
        end

        it 'returns the number of products reset' do
          expect(reset_absent.call).to eq(2)
        end
      end

      context 'and importing into inventory' do
        let(:variant) { create(:variant) }
        let(:enterprise) { variant.product.supplier }
        let(:variant_override) do
          create(:variant_override, variant: variant, hub: enterprise)
        end

        before do
          variant_override

          allow(entry_processor)
            .to receive(:permission_by_id?).with(enterprise.id.to_s) { true }
        end

        before do
          allow(entry_processor)
            .to receive(:importing_into_inventory?) { true }
        end

        it 'returns nil' do
          expect(reset_absent.call).to be_nil
        end
      end
    end

    context 'when reset_all_absent is not set' do
      let(:settings) do
        instance_double(
          ProductImport::Settings,
          settings: { 'reset_all_absent' => false },
          updated_ids: [0],
          enterprises_to_reset: ['1']
        )
      end

      before do
        allow(entry_processor)
          .to receive(:permission_by_id?).with('1') { true }
      end

      it 'does not reset anything' do
        reset_absent.call<commit_msg>Test that variants or overrides are actually reset<commit_after>require 'spec_helper'

describe ProductImport::ResetAbsent do
  let(:importer) { double(:importer) }
  let(:validator) { double(:validator) }
  let(:spreadsheet_data) { double(:spreadsheet_data) }
  let(:editable_enterprises) { double(:editable_enterprises) }
  let(:import_time) { double(:import_time) }
  let(:updated_ids) { double(:updated_ids) }
  let(:import_settings) { double(:import_settings) }

  let(:entry_processor) do
    ProductImport::EntryProcessor.new(
      importer,
      validator,
      import_settings,
      spreadsheet_data,
      editable_enterprises,
      import_time,
      updated_ids
    )
  end

  let(:reset_absent) { described_class.new(entry_processor, settings) }

  describe '#call' do
    context 'when there are no settings' do
      let(:settings) do
        instance_double(
          ProductImport::Settings,
          settings: nil,
          updated_ids: [],
          enterprises_to_reset: []
        )
      end

      it 'returns nil' do
        expect(reset_absent.call).to be_nil
      end
    end

    context 'when there are no updated_ids' do
      let(:settings) do
        instance_double(
          ProductImport::Settings,
          settings: [],
          updated_ids: nil,
          enterprises_to_reset: []
        )
      end

      it 'returns nil' do
        expect(reset_absent.call).to be_nil
      end
    end

    context 'when there are no enterprises_to_reset' do
      let(:settings) do
        instance_double(
          ProductImport::Settings,
          settings: [],
          updated_ids: [],
          enterprises_to_reset: nil
        )
      end

      it 'returns nil' do
        expect(reset_absent.call).to be_nil
      end
    end

    context 'when there are settings, updated_ids and enterprises_to_reset' do
      let(:settings) do
        instance_double(
          ProductImport::Settings,
          settings: { 'reset_all_absent' => true },
          updated_ids: [0],
          enterprises_to_reset: [enterprise.id.to_s]
        )
      end

      before do
        allow(entry_processor)
          .to receive(:permission_by_id?).with(enterprise.id.to_s) { true }
      end

      context 'and not importing into inventory' do
        let(:variant) { create(:variant) }
        let(:enterprise) { variant.product.supplier }

        before do
          allow(entry_processor)
            .to receive(:importing_into_inventory?) { false }
        end

        it 'returns the number of products reset' do
          expect(reset_absent.call).to eq(2)
        end

        it 'resets the products of the specified suppliers' do
          suppliers_to_reset_products = reset_absent
            .instance_variable_get('@suppliers_to_reset_products')

          reset_absent.call
          expect(suppliers_to_reset_products).to eq([enterprise.id])
        end
      end

      context 'and importing into inventory' do
        let(:variant) { create(:variant) }
        let(:enterprise) { variant.product.supplier }
        let(:variant_override) do
          create(:variant_override, variant: variant, hub: enterprise)
        end

        before do
          variant_override

          allow(entry_processor)
            .to receive(:permission_by_id?).with(enterprise.id.to_s) { true }
        end

        before do
          allow(entry_processor)
            .to receive(:importing_into_inventory?) { true }
        end

        it 'returns nil' do
          expect(reset_absent.call).to be_nil
        end

        it 'resets the inventories of the specified suppliers' do
          suppliers_to_reset_inventories = reset_absent
            .instance_variable_get('@suppliers_to_reset_inventories')

          reset_absent.call
          expect(suppliers_to_reset_inventories).to eq([enterprise.id])
        end
      end
    end

    context 'when reset_all_absent is not set' do
      let(:settings) do
        instance_double(
          ProductImport::Settings,
          settings: { 'reset_all_absent' => false },
          updated_ids: [0],
          enterprises_to_reset: ['1']
        )
      end

      before do
        allow(entry_processor)
          .to receive(:permission_by_id?).with('1') { true }
      end

      it 'does not reset anything' do
        reset_absent.call
{'fraction_non_alphanumeric': 0.0889330517948081, 'fraction_numerical': 0.0012420817289777667, 'mean_word_length': 2.1700787401574804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3099620', 'n_tokens_mistral': 2333, 'n_tokens_neox': 2271, 'n_words': 520}