<commit_before>require 'rails_helper'

describe CertificateSearchService do
  describe 'certificate search' do
    around do |example|
      TimeMachine.now { example.run }
    end

    let!(:certificate_1) { create :certificate }
    let!(:certificate_description_1) {
      create :certificate_description,
        :with_period,
        certificate_type_code: certificate_1.certificate_type_code,
        certificate_code: certificate_1.certificate_code
    }
    let!(:measure_1) { create :measure }
    let!(:goods_nomenclature_1) { measure_1.goods_nomenclature }
    let!(:measure_condition_1) { 
      create :measure_condition,
        certificate_type_code: certificate_1.certificate_type_code,
        certificate_code: certificate_1.certificate_code,
        measure_sid: measure_1.measure_sid
    }
    
    let!(:certificate_2) { create :certificate }
    let!(:certificate_description_2) {
      create :certificate_description,
        :with_period,
        certificate_type_code: certificate_2.certificate_type_code,
        certificate_code: certificate_2.certificate_code
    }
    let!(:measure_2) { create :measure }
    let!(:goods_nomenclature_2) { measure_2.goods_nomenclature }
    let!(:measure_condition_2) { 
      create :measure_condition,
        certificate_type_code: certificate_2.certificate_type_code,
        certificate_code: certificate_2.certificate_code,
        measure_sid: measure_2.measure_sid
    }
    let(:current_page) { 1 }
    let(:per_page) { 20 }

    before do
      Sidekiq::Testing.inline! do
        TradeTariffBackend.cache_client.reindex
        sleep(1)
      end
    end

    context 'by certificate code' do
      it 'should find certificate by code' do
        result = described_class.new({
          'code' => certificate_1.certificate_code
        }, current_page, per_page).perform
        expect(result.map(&:id)).to include(certificate_1.id)
      end

      it 'should not find additional code by wrong code' do
        result = described_class.new({
          'code' => certificate_1.certificate_code
        }, current_page, per_page).perform
        expect(result.map(&:id)).not_to include(certificate_2.id)
      end
    end

    context 'by certificate type' do
      it 'should find certificate by type' do
        result = described_class.new({
          'type' => certificate_1.certificate_type_code
        }, current_page, per_page).perform
        expect(result.map(&:id)).to include(certificate_1.id)
      end

      it 'should not find additional code by wrong type' do
        result = described_class.new({
          'type' => certificate_1.certificate_type_code
        }, current_page, per_page).perform
        expect(result.map(&:id)).not_to include(certificate_2.id)
      end
    end

    context 'by description' do
      it 'should find certificate by description' do
        result = described_class.new({
          'description' => certificate_1.description
        }, current_page, per_page).perform
        expect(result.map(&:id)).to include(certificate_1.id)
      end

      it 'should not find certificate by wrong description' do
        result = described_class.new({
          'description' => certificate_1.description
        }, current_page, per_page).perform
        expect(result.map(&:id)).not_to include(certificate_2.id)
      end
    end

    context 'by description first word' do
      it 'should find certificate by description first word' do
        result = described_class.new({
          'description' => certificate_1.description.split(' ').first
        }, current_page, per_page).perform
        expect(result.map(&:id)).to include(certificate_1.id)
      end

      it 'should not find certificate by wrong description first word' do
        result = described_class.new({
          'description' => certificate_1.description.split(' ').first
        }, current_page, per_page).perform
        expect(result.map(&:id)).not_to include(certificate_2.id)
      end
    end
  end
end
<commit_msg>added 4-digits specs for certificate search.<commit_after>require 'rails_helper'

describe CertificateSearchService do
  describe 'certificate search' do
    around do |example|
      TimeMachine.now { example.run }
    end

    let!(:certificate_1) { create :certificate }
    let!(:certificate_description_1) {
      create :certificate_description,
        :with_period,
        certificate_type_code: certificate_1.certificate_type_code,
        certificate_code: certificate_1.certificate_code
    }
    let!(:measure_1) { create :measure }
    let!(:goods_nomenclature_1) { measure_1.goods_nomenclature }
    let!(:measure_condition_1) { 
      create :measure_condition,
        certificate_type_code: certificate_1.certificate_type_code,
        certificate_code: certificate_1.certificate_code,
        measure_sid: measure_1.measure_sid
    }
    
    let!(:certificate_2) { create :certificate }
    let!(:certificate_description_2) {
      create :certificate_description,
        :with_period,
        certificate_type_code: certificate_2.certificate_type_code,
        certificate_code: certificate_2.certificate_code
    }
    let!(:measure_2) { create :measure }
    let!(:goods_nomenclature_2) { measure_2.goods_nomenclature }
    let!(:measure_condition_2) { 
      create :measure_condition,
        certificate_type_code: certificate_2.certificate_type_code,
        certificate_code: certificate_2.certificate_code,
        measure_sid: measure_2.measure_sid
    }
    let(:current_page) { 1 }
    let(:per_page) { 20 }

    before do
      Sidekiq::Testing.inline! do
        TradeTariffBackend.cache_client.reindex
        sleep(1)
      end
    end

    context 'by certificate code' do
      it 'should find certificate by code' do
        result = described_class.new({
          'code' => certificate_1.certificate_code
        }, current_page, per_page).perform
        expect(result.map(&:id)).to include(certificate_1.id)
      end

      it 'should not find additional code by wrong code' do
        result = described_class.new({
          'code' => certificate_1.certificate_code
        }, current_page, per_page).perform
        expect(result.map(&:id)).not_to include(certificate_2.id)
      end

      context 'when user enter 4-digits code' do
        it 'should find certificate by code' do
          result = described_class.new({
            'code' => "#{rand(9)}#{certificate_1.certificate_code}"
          }, current_page, per_page).perform
          expect(result.map(&:id)).to include(certificate_1.id)
        end

        it 'should ignore first digit' do
          service = described_class.new({
            'code' => "#{rand(9)}#{certificate_1.certificate_code}"
          }, current_page, per_page)
          service.perform
          expect(service.code).to eq(certificate_1.certificate_code)
        end
      end
    end

    context 'by certificate type' do
      it 'should find certificate by type' do
        result = described_class.new({
          'type' => certificate_1.certificate_type_code
        }, current_page, per_page).perform
        expect(result.map(&:id)).to include(certificate_1.id)
      end

      it 'should not find additional code by wrong type' do
        result = described_class.new({
          'type' => certificate_1.certificate_type_code
        }, current_page, per_page).perform
        expect(result.map(&:id)).not_to include(certificate_2.id)
      end
    end

    context 'by description' do
      it 'should find certificate by description' do
        result = described_class.new({
          'description' => certificate_1.description
        }, current_page, per_page).perform
        expect(result.map(&:id)).to include(certificate_1.id)
      end

      it 'should not find certificate by wrong description' do
        result = described_class.new({
          'description' => certificate_1.description
        }, current_page, per_page).perform
        expect(result.map(&:id)).not_to include(certificate_2.id)
      end
    end

    context 'by description first word' do
      it 'should find certificate by description first word' do
        result = described_class.new({
          'description' => certificate_1.description.split(' ').first
        }, current_page, per_page).perform
        expect(result.map(&:id)).to include(certificate_1.id)
      end

      it 'should not find certificate by wrong description first word' do
        result = described_class.new({
          'description' => certificate_1.description.split(' ').first
        }, current_page, per_page).perform
        expect(result.map(&:id)).not_to include(certificate_2.id)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.09395124195032199, 'fraction_numerical': 0.010579576816927323, 'mean_word_length': 2.8636161705908485, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '3219111', 'n_tokens_mistral': 2530, 'n_tokens_neox': 2502, 'n_words': 539}