<commit_before>require "rails_helper"

RSpec.describe Trade::InclusionValidationRule, type: :model do

  include_context :validation_rules_helpers

  let(:canis_lupus) {
    FactoryGirl.create(
      :taxon_concept,
      taxon_name: FactoryGirl.create(:taxon_name, scientific_name: 'Canis lupus')
    )
  }

  let(:aru){
    aru = FactoryGirl.create(:annual_report_upload)
    aru.sandbox.send(:create_target_table)
    aru
  }

  let(:sandbox_klass) {
    aru.sandbox.ar_klass
  }

  describe :refresh_errors_if_needed do
    let(:validation_rule) {
      create_taxon_concept_validation
    }
    before(:each) do
      @shipment1 = sandbox_klass.create(
        taxon_name: canis_lupus.full_name
      )
      @shipment2 = sandbox_klass.create(
        taxon_name: 'Caniis lupus'
      )
      @shipment3 = sandbox_klass.create(
        taxon_name: 'Caniis lupus'
      )
      @validation_error = FactoryGirl.create(
        :validation_error,
        annual_report_upload_id: aru.id,
        validation_rule_id: validation_rule.id,
        matching_criteria: {taxon_name: 'Caniis lupus'},
        is_ignored: false,
        is_primary: true,
        error_message: "taxon_name Caniis lupus is invalid",
        error_count: 2
      )
    end

    context "when no updates" do
      specify do
        expect {
          validation_rule.refresh_errors_if_needed(aru)
        }.not_to change { Trade::ValidationError.count }
      end
    end

    context "when updates and error fixed for all records" do
      specify "error record is destroyed" do
        Timecop.travel(Time.now + 1)
        @shipment2.update_attributes(taxon_name: 'Canis lupus')
        @shipment3.update_attributes(taxon_name: 'Canis lupus')
        expect {
          validation_rule.refresh_errors_if_needed(aru)
        }.to change { Trade::ValidationError.count }.by(-1)
      end
    end

    context "when updates and error fixed for some records" do
      specify "error record is updated to reflect new error_count" do
        Timecop.travel(Time.now + 1)
        @shipment2.update_attributes(taxon_name: 'Canis lupus')
        expect {
          validation_rule.refresh_errors_if_needed(aru)
        }.to change { @validation_error.reload.error_count }.by(-1)
      end
    end

  end

end<commit_msg>spec for matching_errors<commit_after>require "rails_helper"

RSpec.describe Trade::InclusionValidationRule, type: :model do

  include_context :validation_rules_helpers

  let!(:canis_lupus) {
    FactoryGirl.create(
      :taxon_concept,
      taxon_name: FactoryGirl.create(:taxon_name, scientific_name: 'Canis lupus')
    )
  }

  let(:aru){
    aru = FactoryGirl.create(:annual_report_upload)
    aru.sandbox.send(:create_target_table)
    aru
  }

  let(:sandbox_klass) {
    aru.sandbox.ar_klass
  }

  describe :refresh_errors_if_needed do
    let(:validation_rule) {
      create_taxon_concept_validation
    }
    before(:each) do
      @shipment1 = sandbox_klass.create(
        taxon_name: canis_lupus.full_name
      )
      @shipment2 = sandbox_klass.create(
        taxon_name: 'Caniis lupus'
      )
      @shipment3 = sandbox_klass.create(
        taxon_name: 'Caniis lupus'
      )
      @validation_error = FactoryGirl.create(
        :validation_error,
        annual_report_upload_id: aru.id,
        validation_rule_id: validation_rule.id,
        matching_criteria: {taxon_name: 'Caniis lupus'},
        is_ignored: false,
        is_primary: true,
        error_message: "taxon_name Caniis lupus is invalid",
        error_count: 2
      )
    end

    context "when no updates" do
      specify do
        expect {
          validation_rule.refresh_errors_if_needed(aru)
        }.not_to change { Trade::ValidationError.count }
      end
    end

    context "when updates and error fixed for all records" do
      specify "error record is destroyed" do
        Timecop.travel(Time.now + 1)
        @shipment2.update_attributes(taxon_name: 'Canis lupus')
        @shipment3.update_attributes(taxon_name: 'Canis lupus')
        expect {
          validation_rule.refresh_errors_if_needed(aru)
        }.to change { Trade::ValidationError.count }.by(-1)
      end
    end

    context "when updates and error fixed for some records" do
      specify "error record is updated to reflect new error_count" do
        Timecop.travel(Time.now + 1)
        @shipment2.update_attributes(taxon_name: 'Canis lupus')
        expect {
          validation_rule.refresh_errors_if_needed(aru)
        }.to change { @validation_error.reload.error_count }.by(-1)
      end
    end
  end

  describe :matching_records_for_aru_and_error do
    let(:validation_rule) {
      create_taxon_concept_validation
    }
    before(:each) do
      @shipment1 = sandbox_klass.create(
        taxon_name: canis_lupus.full_name
      )
      @shipment2 = sandbox_klass.create(
        taxon_name: 'Caniis lupus'
      )
      @validation_error = FactoryGirl.create(
        :validation_error,
        annual_report_upload_id: aru.id,
        validation_rule_id: validation_rule.id,
        matching_criteria: {taxon_name: 'Caniis lupus'}.to_json
      )
    end
    specify {
      expect(
        validation_rule.matching_records_for_aru_and_error(
          aru,
          @validation_error
        )
      ).to eq([@shipment2])
    }
  end

end
{'fraction_non_alphanumeric': 0.0848189153687371, 'fraction_numerical': 0.0046913116907487335, 'mean_word_length': 2.6783988957902003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4734667', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1697, 'n_words': 337}